情境:在服务器环境中,我定义了一个列表。我想在UI环境中调用此列表。
以下是代码:
library(shiny)
ui=fluidPage(
selectizeInput(
'chooser', 'Choose an Item', choices = mylist, multiple = TRUE
)
)
server=function(input,output){
mylist=c("Fork", "Tree", "Truck", "Spoon", "Rocket")
}
shinyApp(ui, server)
不幸的是,这不起作用。我明白了:
Error in lapply(obj, function(val) { : object 'mylist' not found
问题:我需要更改代码才能使其正常工作?
答案 0 :(得分:0)
您可以在服务器中使用updateSelectizeInput(session, "chooser", choices = mylist)
,如下所示:
library(shiny)
ui=fluidPage(
selectizeInput(
'chooser', 'Choose an Item', choices = NULL, multiple = TRUE
)
)
server=function(input,output, session){
mylist=c("Fork", "Tree", "Truck", "Spoon", "Rocket")
updateSelectizeInput(session, "chooser", choices = mylist)
}
shinyApp(ui, server)