gpt4 book ai didi

r - 根据另一个 selectInput 的选择来过滤一个 selectInput?

转载 作者:行者123 更新时间:2023-12-02 14:20:20 25 4
gpt4 key购买 nike

我有两个 selectInput,我希望第一个(品牌)中的选择更改第二个(糖果)中的可能选择。因此,例如,如果有人在第一个输入框中选择“雀巢”,那么第二个框中只会显示雀巢糖果。我的数据表有一列“品牌”和一列“糖果棒类型”。

我有以下代码开始,但这显示了所有选择,无论选择如何。

selectInput(inputId="brand", 
label="Brand:",
choices=as.character
(unique(candyData$Brand)),
selected = "Nestle"
),
selectInput(inputId="candy",
label="Candy:",
choices=as.character
(unique(candyData$Candy)),
selected = "100Grand"

数据集如下所示:

Brand       Candy
Nestle 100Grand
Netle Butterfinger
Nestle Crunch
Hershey's KitKat
Hershey's Reeses
Hershey's Mounds
Mars Snickers
Mars Twix
Mars M&Ms

更新的问题如何根据后续过滤更新仪表板中的 ValueBox?

output$count <- renderValueBox({

valueBox(
value = nrow(candyData),
subtitle = "Number of Candy Bars",
icon = icon("table")
)
})

最佳答案

这是一种方法:

library(shiny)
library(shinydashboard)
##
ui <- shinyUI({
sidebarPanel(

htmlOutput("brand_selector"),
htmlOutput("candy_selector"))

})
##
server <- shinyServer(function(input, output) {
candyData <- read.table(
text = "Brand Candy
Nestle 100Grand
Netle Butterfinger
Nestle Crunch
Hershey's KitKat
Hershey's Reeses
Hershey's Mounds
Mars Snickers
Mars Twix
Mars M&Ms",
header = TRUE,
stringsAsFactors = FALSE)

output$brand_selector <- renderUI({

selectInput(
inputId = "brand",
label = "Brand:",
choices = as.character(unique(candyData$Brand)),
selected = "Nestle")

})

output$candy_selector <- renderUI({

available <- candyData[candyData$Brand == input$brand, "Candy"]

selectInput(
inputId = "candy",
label = "Candy:",
choices = unique(available),
selected = unique(available)[1])

})

})
##
shinyApp(ui = ui, server = server)
<小时/>

更新:

您可以将ui定义修改为

ui <- shinyUI({
sidebarPanel(

htmlOutput("brand_selector"),
htmlOutput("candy_selector"),
valueBoxOutput("count"))

})

并将以下内容添加到服务器:

output$count <- renderValueBox({

available <- candyData[candyData$Brand == input$brand, ]

valueBox(
value = nrow(available),
subtitle = sprintf("Number of %s Candy Bars", input$brand),
icon = icon("table"))

})

关于r - 根据另一个 selectInput 的选择来过滤一个 selectInput?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33086987/

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