英文:
Dropdown value reset to the first value in R shiny dashboard
问题
我正在创建一个带有四个下拉菜单的仪表板。区域、市场和子市场下拉菜单是依赖关系下拉菜单。我正在按照以下方式创建仪表板。市场下拉菜单重置为下拉菜单中的第一个值。如何解决这个问题?
例如,对于Region1,当我选择MarketC时,它会重置回MarketA。
我非常感激您提供的任何帮助。
region <- c("Region1", "Region2", "Region1", "Region3")
market <- c("MarketA", "MarketB", "MarketC", "MarketA")
submarket <- c("SubmarketX", "SubmarketY", "SubmarketZ", "SubmarketX")
mod.selection <- data.table(
Region = region,
market_name = market,
submarket_name = submarket
)
server <- function(input, output, session) {
values <- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observe({
markets <- mod.selection$market_name[mod.selection$Region == input$select_region]
updateSelectInput(session, "select_market", choices = markets)
submarkets <- mod.selection$submarket_name[mod.selection$market_name == input$select_market]
updateSelectInput(session, "select_submarket", choices = submarkets)
})
}
# UI function
ui <- fluidPage(theme = shinytheme("cerulean"),
fluidRow(
column(3,
selectInput("select1", label = h4("Data Type"),
choices = list("Unit" = 1),
selected = 1),
verbatimTextOutput("value1")
),
column(3,
selectInput("select_region", label = h4("Region"),
choices = mod.selection$Region,
selected = 1),
verbatimTextOutput("value2")
),
column(3,
selectInput("select_market", label = h4("Market"),
choices = unique(mod.selection$market_name),
verbatimTextOutput("value3")
)
),
column(3,
selectInput("select_submarket", label = h4("Sub Market"),
choices = NULL),
verbatimTextOutput("value4")
),
),
sidebarLayout(
sidebarPanel(),
mainPanel()
)
)
shinyApp(ui = ui, server = server)
英文:
I am creating a dashboard with four dropdowns. The region, market and submarket dropdowns are dependent dropdowns. I'm creating the dashboard as follows. Drop down for market reset to the first value in the dropdown. How to solve this?
For e.g for Region1 when I select MarketC it reset back to MarketA.
I really appreciate any help you can provide.
region <- c("Region1", "Region2", "Region1", "Region3")
market <- c("MarketA", "MarketB", "MarketC", "MarketA")
submarket <- c("SubmarketX", "SubmarketY", "SubmarketZ", "SubmarketX")
mod.selection <- data.table(
Region = region,
market_name = market,
submarket_name = submarket
)
server <- function(input, output, session) {
values <- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observe({
markets <- mod.selection$market_name[mod.selection$Region == input$select_region]
updateSelectInput(session, "select_market", choices = markets)
submarkets <- mod.selection$submarket_name[mod.selection$market_name == input$select_market]
updateSelectInput(session, "select_submarket", choices = submarkets)
})
}
#Ui function----
ui <-
fluidPage(theme = shinytheme("cerulean"),
fluidRow(
column(3,
selectInput("select1", label = h4("Data Type"),
choices = list("Unit" = 1),
selected = 1),
verbatimTextOutput("value1")
),
column(3,
selectInput("select_region", label = h4("Region"),
choices = mod.selection$Region,
# choices = list("Central" = 1, "West" = 2, "East" = 3),
# choices = NULL,
selected = 1),
verbatimTextOutput("value2")
),
column(3,
selectInput("select_market", label = h4("Market"),
choices = unique(mod.selection$market_name),
verbatimTextOutput("value3")
)),
column(3,
selectInput("select_submarket", label = h4("Sub Market"),
choices = NULL),
verbatimTextOutput("value4")
),
),
sidebarLayout(
sidebarPanel(
),
mainPanel(
)
)
)
shinyApp(ui = ui, server = server)
答案1
得分: 1
以下是您要翻译的部分:
这是一个经过修改的server
函数,我相信它实现了您想要的功能。我根据我上面的评论修改了您的代码,并将您的observe
分成了两个observeEvent
,因为我认为这样更合乎逻辑。
server <- function(input, output, session) {
values <- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observeEvent(input$select_region, {
markets <- mod.selection$market_name[which(mod.selection$Region == input$select_region)]
updateSelectInput(session, "select_market", choices = markets, selected = markets[1])
})
observeEvent(input$select_market, {
submarkets <- mod.selection$submarket_name[which(mod.selection$market_name == input$select_market)]
updateSelectInput(session, "select_submarket", choices = submarkets, selected = submarkets[1])
})
}
下次,请包括您的代码所需的library
。
英文:
Here's a revised server
function that I believe does what you want. I've adapted your code as per my comment above and separated your observe
into two observeEvent
s as I believe this is more logical.
server <- function(input, output, session) {
values <- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observeEvent(input$select_region, {
markets <- mod.selection$market_name[which(mod.selection$Region == input$select_region)]
updateSelectInput(session, "select_market", choices = markets, selected = markets[1])
})
observeEvent(input$select_market, {
submarkets <- mod.selection$submarket_name[which(mod.selection$market_name == input$select_market)]
updateSelectInput(session, "select_submarket", choices = submarkets, selected = submarkets[1])
})
}
Next time, please include the library
s your code requires.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论