问题描述
我的server.R具有以下代码,我希望我的Shiny应用程序可以根据国家/地区选择"来选择州,但是它在第二行中显示了所有州.我想在这里,observe
只是观察不到执行任何操作.
My server.R has the following code, I would like my Shiny app to chose States based on the Country Selection, but it shows all states in the second line. I guess here, observe
is only observing not performing any action.
shinyServer(function(input, output, session) {
observe({
ddf = df[df$Date>=input$daterange[1] & df$Date<=input$daterange[2],]
updateSelectInput(session, "Country", choices = ddf$Country)
ddf1 = subset(ddf, grepl(input$Country, ddf$State))
updateSelectInput(session, "State", choices = ddf1$State)
})
}
基于上述选择,我想传递一些数据框进行绘图.当我选择其他国家/地区时,它会一秒钟更改状态列表,然后返回所有第一国家/地区的状态列表.如果有人可以在这里举一个例子,我真的很感激.我的ui.R代码在下面
Based on above selection, I want to pass some data frame for plotting. When I select different country it is changing states list for a second and going back to all First country's state list. I really appreciate if some one can show an example here.My ui.R code is below
sidebarPanel(
wellPanel(dateRangeInput("daterange", "Date range:",
Sys.Date()-10,
Sys.Date()+10)),
wellPanel(selectInput("Country", "Select a Country:",
'')),
wellPanel(selectInput("State", "Select a State:",
'')))
推荐答案
我认为您的观察者中有一个冲突,因为它包含input$Country
和Country
输入的更新程序.然后,我尝试将其拆分为两个观察者,并使用电抗性导体仅制作一次ddf
.
I think there's like a conflict in your observer cause it contains input$Country
as well as an updater for the Country
input. Then I'd try to split it into two observers, and I'd use a reactive conductor to make ddf
only once.
get_ddf <- reactive({
df[df$Date>=input$daterange[1] & df$Date<=input$daterange[2],]
})
observe({
updateSelectInput(session, "Country", choices = get_ddf()$Country)
})
observe({
ddf1 = subset(get_ddf(), grepl(input$Country, get_ddf()$State))
updateSelectInput(session, "State", choices = ddf1$State)
})
此外,您不应该在choices
参数中使用列的级别而不是列本身吗?
Moreover, shouldn't you use the levels of the column rather than the column itself in the choices
argument ?
observe({
updateSelectInput(session, "Country", choices = levels(droplevels(get_ddf()$Country)))
})
observe({
ddf1 = droplevels(subset(get_ddf(), grepl(input$Country, get_ddf()$State)))
updateSelectInput(session, "State", choices = levels(ddf1$State))
})
如果Country
和State
列不是因素而是字符,请使用unique()
而不是levels(droplevels())
.
If the Country
and State
columns are not factors but characters, use unique()
instead of levels(droplevels())
.
这篇关于观察基于第一个选择的updateSelectInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!