【问题标题】:Shiny - create list of numeric inputs based on variable names loaded from a fileShiny - 根据从文件加载的变量名称创建数字输入列表
【发布时间】:2016-11-26 06:49:03
【问题描述】:

我想加载一个包含变量名称列表的 CSV 文件,例如

"var_A", "var_B", "var_C"

并在 GUI 中为每个变量名称创建一个数字输入列表。我想我需要通过 uiOutput 函数,但不知道这样做。这是我正在尝试做的一些草稿

ui <- bootstrapPage(
    fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv'))

    # list of numeric inputs
    #uiOutput("list_numeric_inputs")
   )

server <- function(input,output) {

  data_set <- reactive({
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    data_set<-read.csv(inFile$datapath, header=F)
  })

  # # list of numeric inputs
  # output$list_numeric_inputs <- renderUI({
  #   # If missing input, return to avoid error later in function
  #   if(is.null(input$data_set()))
  #     return()
  #   
  #   # Get the data set value for variable name
  #   for (i in 1:nrow(data_set)) {
  #     numericInput("...", paste0(data_set[i]), value = 0.)
  #   }
  # })

}

shinyApp(ui, server)

【问题讨论】:

    标签: user-interface shiny user-input


    【解决方案1】:

    1) 您的示例不起作用(header=input$header,sep=input$sep, quote=input$quote 没有输入)

    2)你没有input$dataset只有data_set &lt;- reactive

    3) 所以工作一个:

    library(shiny)
    ui <- bootstrapPage(
      fileInput('file1', 'Choose CSV File', accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
    
      # list of numeric inputs
      uiOutput("list_numeric_inputs")
    )
    
    server <- function(input,output) {
    
      data_set <- reactive({
        inFile <- input$file1
    
        if (is.null(inFile))
          return(NULL)
    
        data_set<-read.csv(inFile$datapath,header = F)
      })
    
       # list of numeric inputs
       output$list_numeric_inputs <- renderUI({
         # If missing input, return to avoid error later in function
         if(is.null(data_set()))
           return()
    
       # Get the data set value for variable name
         lapply(data_set(),function(i){
           numericInput(paste0(i,"_ID"), i, value = 0.)
         }
       ) 
       })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 1) 你是对的对不起,我从一个类似问题的复制和粘贴开始 2) 对 3) 太好了!我现在正在研究它并尝试应用我的代码 4) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-09-29
    • 2013-11-16
    • 2013-06-01
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多