【问题标题】:R shiny fileInput large filesR闪亮文件输入大文件
【发布时间】:2018-07-05 12:46:45
【问题描述】:

我对 R Shiny 的 fileInput 有一些问题。默认情况下,大小限制设置为 5MB。 由于我必须处理的文件非常大(> 50GB),我只需要文件的数据路径和/或名称。不幸的是 fileInput 想要上传完整的文件,或者至少它正在以某种方式加载文件并告诉我在我达到 5MB 限制后文件太大了。

我怎样才能只交出我的应用程序的路径而不上传文件?

ui.R

library(shiny)

# Define UI ----
shinyUI(fluidPage(
h1("SAS Toolbox"),

tabsetPanel(
  tabPanel("SASFat",
     sidebarPanel(h2("Input:"),
        actionButton("runSASFat","Run Job",width="100%",icon("paper-plane"), 
        style="color: #fff; background-color: #337ab7; border-color: #2e6da4"),       

        wellPanel(
           #tags$style(".shiny-file-input-progress {display: none}"),
           fileInput("FEInp","Pfad FE input Deck:"), 
           fileInput("FERes","Pfad FE Results:") 
        ),
        wellPanel(
           checkboxGroupInput("options1","Auswertung:",c("Grundmaterial","Schweissnähte")),
           conditionalPanel(condition="$.inArray('Schweissnähte',input.options1) > -1", 
           sliderInput("filter", "Filter:", 0.75, min = 0, max = 1))
        ),
        wellPanel(
           radioButtons("solver", "Solver:", c("Ansys","Abaqus", "Optistruct")),
           conditionalPanel(condition="input.solver == 'Ansys'",selectInput("lic", "Lizenz",c("preppost","stba","meba"))) 
        ),
        wellPanel(
           checkboxGroupInput("options2","Optionen:",c("Schreibe LCFiles"))
        )
     ),
     mainPanel(br(),h2("Output:"),width="30%")
  ), 
  tabPanel("Nietauswertung"),
  tabPanel("Spannungskonzept EN12663")
  )
))

server.R

# Define server logic ----
shinyServer(function(input, output) {
  observeEvent(input$runSASFat, {
    FEInp <- input$FEInp
    FERes <- input$FERes
    opt1 <- input$options1 
    opt2 <- input$options2
    filter <- input$filter
    solver <- input$solver
    lic <- input$lic

    write(c(FEInp$datapath,FERes$datapath,opt1,opt2,filter,solver,lic),"ghhh.inp")
    })
})

提前致谢

迈克尔

【问题讨论】:

  • 我没有用闪亮测试过这个,但也许file.choose()会有用吗?

标签: r file-io shiny


【解决方案1】:

感谢@MichaelBird 的示例。我修改了您的代码,让用户在不选择文件的情况下取消请求(取消后您的应用程序崩溃):

顺便说一句,这仅适用于托管闪亮应用的 PC。

library(shiny)

ui <- fluidPage(
  titlePanel("Choosing a file example"),
  sidebarLayout(
    sidebarPanel(
      actionButton("filechoose",label = "Pick a file")
    ),
    mainPanel(
      textOutput("filechosen")
    )
  )
)

server <- function(input, output) {

  path <- reactiveVal(value = NULL)

  observeEvent(input$filechoose, {

    tryPath <- tryCatch(
      file.choose()
      , error = function(e){e}
    )

  if(inherits(tryPath, "error")){
    path(NULL)
  } else {
    path(tryPath)
  }

  })

  output$filechosen <- renderText({
    if(is.null(path())){
      "Nothing selected"
    } else {
      path()
    }
  })

}

shinyApp(ui = ui, server = server)

另一种方法是增加上传的最大文件大小:

默认情况下,Shiny 将文件上传限制为每个文件 5MB。你可以修改 通过使用 shiny.maxRequestSize 选项进行此限制。例如, 将 options(shiny.maxRequestSize = 30*1024^2) 添加到 app.R 的顶部 会将限制增加到 30MB。

请参阅此 RStudio article

【讨论】:

  • 感谢您的改进,并注意这仅适用于本地计算机。
【解决方案2】:

这是在闪亮的应用程序中使用file.choose() 来获取文件的本地路径(以及文件名)的示例:

library(shiny)


ui <- fluidPage(

   # Application title
   titlePanel("Choosing a file example"),


   sidebarLayout(
      sidebarPanel(
        actionButton("filechoose",label = "Pick a file")
      ),

      mainPanel(
         textOutput("filechosen")
      )
   )
)


server <- function(input, output) {

  path <- reactiveValues(
    pth=NULL
  )


  observeEvent(input$filechoose,{
    path$pth <- file.choose()
  })

   output$filechosen <- renderText({

      if(is.null(path$pth)){
        "Nothing selected"
      }else{
        path$pth
      }
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

这就是你所追求的吗?

【讨论】:

  • 是的,就是这样,感谢您的快速回答。我个人更喜欢fileInput 的设计,但这对我来说也可以。我只是想知道如果提取的唯一值是“名称”、“大小”、“类型”和“数据路径”,为什么fileInput 会上传任何文件。对我来说没有意义......
  • 上传后使用fileInput可以访问文件内容。有关示例,请参见 this
猜你喜欢
  • 2020-10-18
  • 1970-01-01
  • 2014-11-12
  • 2021-01-14
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多