当multiple = T时,Selectinput未更新

时间:2017-09-03 22:29:37

标签: r shiny reactive

我开发了一个小型闪亮的应用程序,但我希望它与multiple =T一起选择时显示两个数据集的变量。有什么想法吗?

ui.r

library(shiny)

  bootstrapPage(
    selectInput('dataset', 'Choose data set', c('mtcars', 'iris'),multiple = T),
    selectInput('columns', 'Choose variable', "")
  )

server.r

function(input, output, session){

  # updates variable names based on selected dataset 
  outVar = reactive({
    names(get(input$dataset))
  })

  # create separate observeEvents to 
  observeEvent(input$dataset, {
    updateSelectInput(session, "columns", choices = outVar())
  })
}

1 个答案:

答案 0 :(得分:1)

您需要将所选输入作为列表传递给lapply函数,并迭代地阅读input$dataset中的数据集以获取names()

您可以尝试以下方法:

  # updates variable names based on selected dataset 
  outVar = reactive({
    unlist(lapply(as.list(input$dataset), function(x){data <- get(x);names(data)}))
  })

完整的工作示例:

library(shiny)

ui <- bootstrapPage(
  selectInput('dataset', 'Choose data set', c('mtcars', 'iris'),multiple = T),
  selectInput('columns', 'Choose variable', "")
)

server <- function(input, output, session){

  # updates variable names based on selected dataset 
  outVar = reactive({
    unlist(lapply(as.list(input$dataset), function(x){data <- get(x);names(data)}))
  })

  # create separate observeEvents to 
  observeEvent(input$dataset, {
    updateSelectInput(session, "columns", choices = outVar())
  })
}


shinyApp(ui, server)