本文介绍了过滤一项选择基于从另一项选择中选择的内容进行输入是否要输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个selectInput
,我希望第一个(Brand)中的选择更改第二个(Candy)中可能的选择。例如,如果有人在第一个输入框中选择了"雀巢",那么第二个框中只会显示雀巢糖果。我的数据表有一列是Brand,一列是Candy bar type。我要启动以下代码,但这将显示所有选项,而不考虑选择。
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"))
})
并将以下内容添加到server
:
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"))
})
这篇关于过滤一项选择基于从另一项选择中选择的内容进行输入是否要输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!