【问题标题】:Cannot populate drop down menu dynamically in R shiny无法在 R Shiny 中动态填充下拉菜单
【发布时间】:2015-03-25 14:02:48
【问题描述】:

我正在尝试使用 R Shiny 创建一个 APP。我想上传数据(.csv 文件)。然后我想在下拉菜单中填充 CSV 文件中的列名。我做不到。

请参考以下代码:

---- server.r -----

library(shiny)
options(shiny.maxRequestSize = 32*1024^2)


shinyServer(


function(input, output){ 

data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath,head=TRUE,sep=",")

})

output$sum <- renderTable({
  if(is.null(data())){return ()}
  summary(data())

})

output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})

# the following renderUI is used to dynamically generate the tabsets when the file is loaded. Until the file is loaded, app will not show the tabset.
output$tb <- renderUI({
  if(is.null(data()))
    h5("no file loaded")
  else
    tabsetPanel(tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
})

   output$col <- renderUI({

  selectInput("phenomena", "Select the Phenomena",  names(data))
  })

})  

----- ui.R -----

 library(shiny)


 shinyUI(fluidPage(

 titlePanel("Hotspot Analysis of EnviroCar Data"),
 sidebarLayout(
 sidebarPanel(

  # uploading the file 
   fileInput("file","Upload *.csv file"), # fileinput() function is used to get the file upload contorl option

  uiOutput("col")


),

mainPanel(  uiOutput("tb") )

)

))

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    我猜问题出在server.R

    selectInput("phenomena", "Select the Phenomena",  names(data))
    

    这里,你使用的是不带括号的data,所以你实际得到的是函数data的源代码,而names(data)NULL。我认为您只需将names(data) 替换为names(data())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2012-09-03
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多