R shiny fileInput large files

你离开我真会死。 提交于 2020-01-14 01:43:29

问题


i have some problem with fileInput for R Shiny. Size limit is set to 5MB per default. Since the files i have to work with are very large (>50GB), I only need the datapath and or name of the file. Unfortunatly fileInput wants to upload the complete file or at least it is loading the file somehow and tells me that the file is too big after i have reached the 5MB limit.

How can I only hand over the path to my app without uploading the file?

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")
    })
})

Thanks in advance

Michael


回答1:


Here is an example of using file.choose() in a shiny app to obtain the local path of the file (and hence the file name):

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)

Is this what you're after?




回答2:


Thanks for the example @MichaelBird. I adapted your code to let users cancel the request without choosing a file (your app crashed after canceling):

This by the way only works on the PC hosting the shiny app.

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)

An alternative way would be to increase the maximum file size for uploads:

By default, Shiny limits file uploads to 5MB per file. You can modify this limit by using the shiny.maxRequestSize option. For example, adding options(shiny.maxRequestSize = 30*1024^2) to the top of app.R would increase the limit to 30MB.

See this RStudio article.



来源:https://stackoverflow.com/questions/51191701/r-shiny-fileinput-large-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!