gpt4 book ai didi

r - SelectInput Value update based on previous SelectInput in R shiny

转载 作者:行者123 更新时间:2023-12-05 02:17:04 26 4
gpt4 key购买 nike

下面的 R shiny 脚本创建了三个 selectInput,每个 selectInput 的值都取决于前一个 selectInput 的输入值。例如。在脚本中的数据中,“value”列的值取决于“Candy”列,而“Candy”列的值取决于“Brand”。问题是,无论我在“品牌”列中选择“Mars”还是“Netle”值,两者对应的“Candy”值“100Grand”都是相同的,因此我看不到值列中的变化selectInput 正在读取相同的值。请帮我解决这个问题,也请确保脚本不会变慢。谢谢。

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 issue

最佳答案

您只需在 observeEvent 中添加 input$Select1 即可更新 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]))
})
}

如果需要,可以将 observeEvent 转换为 observe :

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

关于r - SelectInput Value update based on previous SelectInput in R shiny,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com