【问题标题】:How can I use the column names of a csv file as input selection?如何使用 csv 文件的列名作为输入选择?
【发布时间】:2019-06-03 07:17:00
【问题描述】:

我创建了一个 Shiny App,它接受 Excel 和 csv 文件作为输入。此外,它应该预测可以在上传文件中找到的指标。为了能够在文件中定义正确的列,用户应该能够选择应该预测的列。这就是为什么我想要一个选择输入区域,其中显示文件的所有列名。但我没有找到正确的解决方案。

到目前为止,我的应用如下:

用户界面:

 ui <- fluidPage(  

  #definition which file input is allowed
  fileInput(
    'file',
     label = h4("Welche Datei soll hochgeladen werden?"),
     multiple= FALSE,
     accept = c(
      'text/csv',
      'text/comma-separated-values,text/plain',
      '.csv',
      '.xlsx'
   )
  ),

服务器:

server <- function(input, output) {

#read data 
data <- reactive({

  validate(need(input$file, ""))
  infile <- input$file

  if (input$type == "1") {
   read.csv(infile$datapath,
           header = input$header,
           sep = input$sep,
           stringsAsFactors = FALSE)
  } else {
  read_xlsx(infile$datapath)
  }    

})

我在服务器上想过这样的事情,但最终无法解决问题:

names <- reactive({
 df <- data()
 if (is.null(df)) return(NULL)

 items=names(df)
 names(items)=items

 return(names(items))

})

感谢您的帮助!

【问题讨论】:

    标签: r shiny selectinput


    【解决方案1】:

    在 UI 内部,您应该添加:

    selectInput("columnid","Column to forecast",choices=c())

    您的服务器应该这样启动: server &lt;- function(session,input, output) {

    在服务器内部,您可以在反应式内部的else 之后添加:

    updateSelectInput(session,"columnid",choices=colnames(mydata))

    记得将读取的数据称为“mydata”,并将其命名为return,如下所示:

    data <- reactive({
    
      validate(need(input$file, ""))
      infile <- input$file
    
      if (input$type == "1") {
       mydata=read.csv(infile$datapath,
               header = input$header,
               sep = input$sep,
               stringsAsFactors = FALSE)
      } else {
       mydata=read_xlsx(infile$datapath)
      }
      updateSelectInput(session,"columnid",choices=colnames(mydata))
    return(mydata)
    })
    

    【讨论】:

    • 感谢您的快速回答。你最后一句话是什么意思?我在哪里以及如何调用 mydata?
    • 我还有一个问题。我刚刚意识到,现在该应用程序确实为我提供了所需的下拉可能性。但是,当我尝试切换到第二个或第三个条目时,它总是会回到第一个。所以我真的不能切换到另一个条目。你知道这里出了什么问题吗?
    • 这意味着每次尝试选择列时都会调用 updateSelectInput。您需要将它放在仅在读取文件时运行的代码块中。我猜你是在一些 observeEvent 或 eventReactive 中调用 data() ,这导致 updateSelectInput 被一遍又一遍地重新评估。
    【解决方案2】:

    您也可以在服务器中使用observeobserve 不返回任何内容。与reactive 不同,它会立即响应(而不是懒惰)。它最适合用于 ip/op 操作。

    ui <- fluidPage (
     selectInput("select_input_id", "INPUT COLUMNS", choices = c())
    )
    
    
    
    server <- function(input, output) {
    
      #read data 
      data <- reactive({
    
        infile <- input$file
    
        if (input$type == "1") {
         df <- read.csv(infile$datapath,
                 header = input$header,
                 sep = input$sep,
                 stringsAsFactors = FALSE)
    
         return (df)
        }
      })
    
    observe({
      updateSelectInput(
        session,
        "select_input_id",
        choices = names(data())
      )
    )}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 2013-02-04
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 2015-04-10
      相关资源
      最近更新 更多