【发布时间】:2016-10-26 20:15:23
【问题描述】:
如何使用观察者更新全局变量?
我在开始时有一个空物种:
speciesChoices <- c()
但我想在 ui 上发生更改时向其中添加值,所以它变成:
speciesChoices <- c(
'PM2.5' = 'particles'
)
有可能吗?
到目前为止我的代码...
ui.r:
speciesOptions <- selectInput(
inputId = "species",
label = "Species:",
choices = c(
# 'PM2.5' = 'particles',
# 'Humidity %' = 'humidity'
)
)
server.r:
speciesChoices <- c() # global variable
# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {
observe({
key <- input$streams1
stream <- fromJSON(paste(server, "/output/stream?public_key=", key, sep=""),flatten=TRUE)
species <- as.data.frame(stream$species)
# Set choices of title and code for select input.
speciesChoices <- setNames(species$code_name, species$public_name)
updateSelectInput(session, "species", choices = speciesChoices)
})
})
它只更新 ui 上的输入,而不更新服务器上的 speciesChoices。
有什么想法吗?
【问题讨论】:
标签: r shiny shiny-server