【问题标题】:Read a file after taking user input(selection) in Shiny在 Shiny 中获取用户输入(选择)后读取文件
【发布时间】:2021-05-02 20:05:48
【问题描述】:

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

   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)

【问题讨论】:

  • observe 块不输出任何内容,我认为您的 file.path(..) 不会去任何地方。也许您应该将该块转换为myfile &lt;- reactive({...; file.path(.);}),然后再打开myfile() 或类似的。 (在尝试读取文件之前,您可能会从 req(file.exists(myfile())) 中受益。)
  • 它一直工作到output$fileselected,因为我可以看到“您已选择...”的选择,但无法显示内容。在file.path() 上,Copied_Source_Files_loc 不是从中获取价值吗?
  • observe 块不返回任何东西,所以当块确实评估file.path(.)时,它无处可去
  • 我取出观察器并更新代码如下,但仍然无法让它工作` output$selected_table
  • edit您的问题和更改以反映您所做的更改。谢谢!

标签: r shiny


【解决方案1】:
  1. (自更改以来)将包含 file.path(.) 的块创建为 reactive 块并将其分配给某个东西,以便其他反应组件可以使用它。在您的情况下,您将其更改为Output_loc,因此其他块将其称为Output_loc()

  2. 同样,您不能将output$... &lt;- 分配或render* 调用放在observeobserveEvent 块内。因此,我们会将您的 output$fileselection_statement 移到 observeEvent 之外。

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

  4. 这种情况不需要selectfile &lt;- 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)
  }) 
}

【讨论】:

  • 非常感谢您的详细解释。我可以看到很多我自己看不到的变化,非常感谢!
  • 对不起,那是我的错误,所以我把那部分删掉了。
猜你喜欢
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2020-06-27
相关资源
最近更新 更多