在 Shiny 中获取用户输入(选择)后读取文件

时间:2021-05-02 20:05:48

标签: r shiny

我在接受用户输入后尝试读取文件 - 首先是日期,然后是文件选择,但我无法让代码读取任何文件并对其执行任何操作。错误消息如下。还有没有办法让这段代码更高效?

   ui <- fluidPage(
    titlePanel("Select the execution date")
    ,dateInput("ME_DATE",label=h3("Execution Date Input"), value="2020-05-29")
    ,hr()
    ,fluidRow(column(3,verbatimTextOutput("Output_path")))
    ,hr()
    ,selectInput('selectfile','Select File in Above Location', choices=NULL)
    ,textOutput('fileselection_statement')
    ,tableOutput('selected_table')

)

server <- function(input, output, session) {
    # Location for Outputs
    Output_DIR <- "K:/Outputs/"
    Output_loc <- reactive({
                                    
    year_N_ME_DATE <- format(input$ME_DATE,"%Y")
    month_N_ME_DATE <- format(input$ME_DATE,"%m")
    month_T_ME_DATE <- months(input$ME_DATE)

    file.path(paste(Output_DIR,month_N_ME_DATE,". ",month_T_ME_DATE, " ",year_N_ME_DATE,"/",sep=""))
    })
    
    # Output Path
    output$Output_path <- renderPrint({ Output_loc() })

    # files list
    Updated_Output_files_list <- reactive({ list.files(Output_loc()) })

    observeEvent(input$selectfile, {    
    updateSelectInput(session, "selectfile", choices=Updated_Output_files_list())
    output$fileselection_statement <- renderText({paste0('You have selected: ', input$selectfile) })

    })
    selectfile <- reactive(get(input$selectfile))
    output$selected_table <- renderTable({  read.csv(paste0(renderPrint({ Output_loc() }),renderPrint({ selectfile() }),sep="")) }) 
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

  1. (自更改后)使包含 file.path(.) 的块成为 reactive 块并将其分配给某些东西,以便其他反应组件可以使用它。在您的情况下,您将其更改为 Output_loc,因此其他块将其称为 Output_loc()

  2. 同样,您不能将 output$... <- 赋值或 render* 调用放在 observeobserveEvent 块中。因此,我们会将您的 output$fileselection_statement 移出 observeEvent

  3. renderPrint 是它自己的渲染函数,与 renderTable 处于同一级别。你不能嵌套它们。在这种情况下,我只是将它们从 renderTable 调用中删除,它们在那里没有意义。

  4. 这个案例不需要selectfile <- reactive(get(input$selectfile)),在那个间接中没有明显的收益。只需使用 input$selectfile。已移除。

  5. 在解决上述所有问题后,每次更改 selectInput 时都会更新 selectInput,这是不正确的(并且完全不允许任何实际使用这)。相反,您希望在 Updated_Output_files_list() 更改时更新它。

  6. 此外,我使用 list.files(..., full.names=TRUE),而不是重复地将路径连接在一起以创建要读取的文件。这将是一个命名向量,其中值是完整路径和文件名,但名称将只是文件名(没有前导路径)。这很有用,因为 selectInput 显示名称但返回值(完整路径)。很少有时间我认为指定 full.names=TRUE 是正确的(我现在想不出任何)。

这是一个工作副本。这不是非常棒,仍有一些地方可能需要进行一些梳理。

server <- function(input, output, session) {
  # Location for Outputs
  Output_DIR <- "K:/Outputs/"
  Output_loc <- reactive({
    year_N_ME_DATE <- format(input$ME_DATE, "%Y")
    month_N_ME_DATE <- format(input$ME_DATE, "%m")
    month_T_ME_DATE <- months(input$ME_DATE)
    file.path(Output_DIR,
              paste0(month_N_ME_DATE, ". ", month_T_ME_DATE, " ", year_N_ME_DATE),
              "/")
  })
  # alternative
  # Output_loc <- reactive({
  #   file.path(Output_DIR, format(Sys.Date(), format = "%m. %b %Y"))
  # })
  
  # Output Path
  output$Output_path <- renderPrint({ req(Output_loc()) })

  # files list
  Updated_Output_files_list <- reactive({
    lf <- list.files(Output_loc(), full.names = TRUE)
    names(lf) <- basename(lf)
    # in this example, 'lf' is now:
    # c(iris.csv = "K:/Outputs/05. May 2020/iris.csv", mtcars.csv = "K:/Outputs/05. May 2020/mtcars.csv")
    # ... the *name* will be displayed in the selectInput, but the
    # *full path* will be the value of the selection
    lf
  })

  output$fileselection_statement <- renderText({
    paste0('You have selected: ', input$selectfile)
  })

  observeEvent(Updated_Output_files_list(), {
    updateSelectInput(session, "selectfile", choices = Updated_Output_files_list())
  })

  output$selected_table <- renderTable({
    req(input$selectfile)
    read.csv(input$selectfile)
  }) 
}
相关问题