【问题标题】:Shiny - use column headers from read in file as selectInput choicesShiny - 使用从文件中读取的列标题作为 selectInput 选项
【发布时间】:2015-09-03 13:24:25
【问题描述】:

我正在尝试创建一个应用程序,人们可以在其中上传 CSV,然后与数据进行交互。 具体问题是我未能将读入的文件中的列标题传递给 selectInput 函数。

如果您注释掉观察功能的最后几行,则应用可以正常工作。尝试了许多选项,使用响应式而不是 renderTable 等。关于解决更改选择输入的问题有一些类似的问题,但我无法从读入的文件中看到。似乎“内容”中缺少任何内容问题?

require(shiny)
runApp(
list(
ui = fluidPage(
  sidebarPanel(fileInput('file1', 'Choose CSV File',
                         accept=c('text/csv', 
                                  'text/comma-separated-values,text/plain', 
                                  '.csv')),
               selectInput("inSelect", "Select something", choices = "Pending Upload")
  ),
  mainPanel(
    tableOutput("contents"))
),

server = function(input, output) {
    output$contents <- renderTable({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    read.csv(inFile$datapath)
  })

  observe({
    updateSelectInput(session, "inSelect", choices = names(contents()))
  })

}
)

测试 CSV

Col1,Col2,Col3
2,3,4
5,6,7

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您的代码存在一些问题。为了使updateSelectInput 工作,您需要将session 对象传递给您的shinyServer 函数。

    然后,您必须创建一个读取输入文件的reactive 上下文。这是必需的,因为此操作的结果既用于创建输出表,也用于更改 selectInput 的值。

    这应该可行:

    server = function(input, output, session) {
      contentsrea <- reactive({
         inFile <- input$file1
         if (is.null(inFile))
            return(NULL)
         read.csv(inFile$datapath)
      })
      output$contents<-renderTable({contentsrea()})
      observe({
         updateSelectInput(session, "inSelect", choices = names(contentsrea()))
      })
    } 
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2019-06-21
      • 2017-04-22
      • 2017-12-02
      • 2019-03-28
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多