R闪亮文件输入大文件

时间:2018-07-05 12:46:46

标签: r file-io shiny

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

预先感谢

迈克尔

2 个答案:

答案 0 :(得分:1)

以下是在闪亮的应用程序中使用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)

这是你的追求吗?

答案 1 :(得分: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选项可以达到此限制。例如,   在app.R的顶部添加选项(shiny.maxRequestSize = 30 * 1024 ^ 2)   会将限制增加到30MB。

请参阅此RStudio article