简体   繁体   English

SelectInput基于R Shiny中先前的SelectInput更新值

[英]SelectInput Value update based on previous SelectInput in R shiny

The R shiny script below creates three selectInputs such that the values of each selectInput depend upon the input value of the previous selectInput. 下面的R闪亮脚本创建了三个selectInputs,以便每个selectInput的值取决于前一个selectInput的输入值。 Eg in the data in the script, "value" column values depend on "Candy" column and "Candy" column values depend on the "Brand". 例如,在脚本中的数据中,“值”列值取决于“糖果”列,而“糖果”列值取决于“品牌”。 The issue is that, whether I select "Mars" or "Netle" value in the "Brand" column, The corresponding "Candy" value "100Grand" is same for both, hence I do not see a change in the value column as the selectInput is reading the same value. 问题是,无论我在“品牌”列中选择“火星”还是“ Netle”值,两者对应的“糖果”值“ 100Grand”都是相同的,因此我看不到值列中的变化selectInput正在读取相同的值。 Kindly help me to fix this, also please ensure the script does not become slow. 请帮助我解决此问题,也请确保脚本不会变慢。 Thanks. 谢谢。

candyData <- read.table(
text = "
Brand       Candy           value
Mars        100Grand        Choc1
Netle       100Grand        Choc2
Nestle      Crunch          Choc3",
header = TRUE,
stringsAsFactors = FALSE)
library(shiny)
library(shinydashboard)
submenuUI <- function(id) {
ns <- NS(id)
tagList(
box(title = "Data", status = "primary", solidHeader = T, width = 12,
    fluidPage(
      fluidRow(

        column(2,offset = 0, style='padding:1px;',

selectInput(ns("Select1"),"select1",unique(candyData$Brand))),
        column(2,offset = 0,

style='padding:1px;',selectInput(ns("Select2"),"select2",choices = NULL)),
        column(2, offset = 0,

style='padding:1px;',selectInput(ns("Select3"),"select3",choices=NULL ))
      )))
)
}
submenuServ <- function(input, output, session){
observeEvent(input$Select1,{
updateSelectInput(session,'Select2',
choices=unique(candyData$Candy[candyData$Brand==input$Select1]))
})
observeEvent(input$Select2,{
updateSelectInput(session,'Select3',
choices=unique(candyData$value[candyData$Brand==input$Select1 &
candyData$Candy==input$Select2]))
})
}
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
  shinyjs::useShinyjs(),
  id = "tabs",
  menuItem("Charts", icon = icon("bar-chart-o"),
           shinyjs::hidden(menuSubItem("dummy", tabName = "dummy")),
           menuSubItem("Sub-item 1", tabName = "subitem1")
  ))),
dashboardBody(
tabItems(tabItem("dummy"),
         tabItem("subitem1", submenuUI('submenu1'))
)
))
server <- function(input, output,session) {
callModule(submenuServ,"submenu1")
}
shinyApp(ui = ui, server = server)

SelectInput问题

You can just add input$Select1 in your observeEvent to update select3 您只需在observeEvent添加input$Select1 observeEvent以更新select3

submenuServ <- function(input, output, session){
    observeEvent(input$Select1,{
        updateSelectInput(session,'Select2',
                          choices=unique(candyData$Candy[candyData$Brand==input$Select1]))
    })
    observeEvent(c(input$Select1, input$Select2),{
        updateSelectInput(session,'Select3',
                          choices=unique(candyData$value[candyData$Brand==input$Select1 &
                                                             candyData$Candy==input$Select2]))
    })
}

If you want, you can convert the observeEvent to observe : 如果需要,可以将observeEvent转换为observe

observe({
    updateSelectInput(
        session, 'Select3',
        choices= unique(candyData$value[candyData$Brand==input$Select1 & candyData$Candy==input$Select2])
    )
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 闪亮:基于先前过滤器的自动选择输入值更新 - Shiny: Automatic SelectInput Value Update Based on Previous Filter Shiny: selectInput 根据之前的selectInput 重置选择值 - Shiny: selectInput based on previous selectInput resetting the selected value 基于R shiny中常见服务器功能下的前一个selectInput更新selectInput - Updating a selectInput based on previous selectInput under common server function in R shiny 在R Shiny中,使用由先前选择填充的SelectInput中的值 - In R Shiny, use the value from an SelectInput that was populated by a previous choice 根据 R Shiny 中的其他选择动态更新两个 selectInput 框 - Dynamically update two selectInput boxes based on the others selection in R Shiny 根据 R Shiny 中的 observeEvent 的链接选择触发 selectInput 的更新 - trigger an update of selectInput based on link selection from observeEvent in R Shiny R,Shiny:selectInput的下一个/上一个按钮 - R, Shiny: next/previous button for selectInput r闪亮地使textInput以先前的selectInput为条件 - r Shiny make textInput conditional on a previous selectInput 根据闪亮的R中的另一个selectInput值更改默认值 - change default values based on another selectInput value in shiny R 基于 R shiny 中的另一个的多个反应选择输入 - Multiple reactive selectinput based on another in R shiny
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM