在R闪亮仪表板中,下拉菜单的值被重置为第一个值。

huangapple go评论105阅读模式
英文:

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 &lt;- c(&quot;Region1&quot;, &quot;Region2&quot;, &quot;Region1&quot;, &quot;Region3&quot;)
market &lt;- c(&quot;MarketA&quot;, &quot;MarketB&quot;, &quot;MarketC&quot;, &quot;MarketA&quot;)
submarket &lt;- c(&quot;SubmarketX&quot;, &quot;SubmarketY&quot;, &quot;SubmarketZ&quot;, &quot;SubmarketX&quot;)
mod.selection &lt;- data.table(
Region = region,
market_name = market,
submarket_name = submarket
)
server &lt;- function(input, output, session) {
values &lt;- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observe({
markets &lt;- mod.selection$market_name[mod.selection$Region == input$select_region]
updateSelectInput(session, &quot;select_market&quot;, choices = markets)
submarkets &lt;- mod.selection$submarket_name[mod.selection$market_name == input$select_market]
updateSelectInput(session, &quot;select_submarket&quot;, choices = submarkets)
})
}
#Ui function----
ui &lt;- 
fluidPage(theme = shinytheme(&quot;cerulean&quot;),
fluidRow(
column(3,
selectInput(&quot;select1&quot;, label = h4(&quot;Data Type&quot;),
choices = list(&quot;Unit&quot; = 1),
selected = 1),
verbatimTextOutput(&quot;value1&quot;)
),
column(3,
selectInput(&quot;select_region&quot;, label = h4(&quot;Region&quot;),
choices = mod.selection$Region,
# choices = list(&quot;Central&quot; = 1, &quot;West&quot; = 2, &quot;East&quot; = 3),
# choices = NULL,
selected = 1),
verbatimTextOutput(&quot;value2&quot;)
),
column(3,
selectInput(&quot;select_market&quot;, label = h4(&quot;Market&quot;),
choices = unique(mod.selection$market_name),
verbatimTextOutput(&quot;value3&quot;)
)),
column(3,
selectInput(&quot;select_submarket&quot;, label = h4(&quot;Sub Market&quot;),
choices = NULL),
verbatimTextOutput(&quot;value4&quot;)
),
),
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 observeEvents as I believe this is more logical.

server &lt;- function(input, output, session) {
values &lt;- reactiveValues(data = data.frame(Feature = character(0), Value = numeric(0)))
observeEvent(input$select_region, {
markets &lt;- mod.selection$market_name[which(mod.selection$Region == input$select_region)]
updateSelectInput(session, &quot;select_market&quot;, choices = markets, selected = markets[1])
})
observeEvent(input$select_market, {
submarkets &lt;- mod.selection$submarket_name[which(mod.selection$market_name == input$select_market)]
updateSelectInput(session, &quot;select_submarket&quot;, choices = submarkets, selected = submarkets[1])
})
}

Next time, please include the librarys your code requires.

huangapple
  • 本文由 发表于 2023年8月10日 22:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定