R 闪亮的反应单选按钮
Posted
技术标签:
【中文标题】R 闪亮的反应单选按钮【英文标题】:R Shiny Reactive Radio Buttons 【发布时间】:2021-05-01 05:12:23 【问题描述】:在以下示例中,我有两个静态单选按钮,分别代表 mtcars 和 iris 数据集。做出选择后,会根据每个数据集中的数据向用户呈现第二组按钮。对于 mtcars 数据集,用户可以通过从唯一的化油器列表中选择进行过滤,或者在 iris 数据集的情况下选择物种。现在,我需要另一组基于 carb/species 按钮的按钮来进一步过滤数据。比如说,对于 mtcars 数据集,是与化油器选择相关的唯一齿轮选择列表,对于 Iris,是一组独特的花瓣长度。鉴于我正在尝试完成的实际应用,需要第三组反应式单选按钮是不可避免的。我只是不知道如何进行下一步。
ui.R
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "My DFS Dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("MTCARS", tabName = "dashboard", icon = icon("dashboard")),
menuItem("IRIS", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
fluidRow (
column(width = 3,
box(title = "Select Dataset", width = NULL, status = "primary", background = "aqua",
radioButtons ("mydataset",
"",
inline = TRUE,
c("mtcars", "iris"),
selected = "mtcars"))),
column(width = 3,
box(title="Select Filter One", width = NULL, status = "primary", background = "aqua",
uiOutput("filter1"))),
column(width = 3,
box(title = "Select Fitler Two", width = NULL, status = "primary", background = "aqua",
uiOutput("filter2")))
)
)
)
server.R
library(tidyverse)
server <- function(input, output, session)
data("mtcars")
data("iris")
cars <- mtcars
flowers <- iris
carbs <- cars %>%
dplyr::select(carb)
carbs <- carbs$carb
carbs <- as.data.frame(carbs)
carbs <- unique(carbs$carb)
spec <- flowers %>%
dplyr::select(Species)
spec <- unique(spec$Species)
vards <- reactive (
switch(input$mydataset,
"mtcars" = carbs,
"iris" = spec,
)
)
output$filter1 <- renderUI(
radioButtons("fil1","", choices=vards())
)
【问题讨论】:
【参考方案1】:也许这可能会有所帮助。您可以添加另一个 reactive
表达式来过滤您的数据集并获得第三组单选按钮的选择。我包含了isolate
,以便第三组按钮不会对数据集的更改做出反应(仅第二个单选按钮的更改已经依赖于数据集)。请让我知道您是否考虑过这种行为。
server <- function(input, output, session)
data("mtcars")
data("iris")
cars <- mtcars
flowers <- iris
vards1 <- reactive(
switch(input$mydataset,
"mtcars" = unique(cars$carb),
"iris" = unique(flowers$Species),
)
)
vards2 <- reactive(
req(input$fil1)
if (isolate(input$mydataset) == "mtcars")
cars %>%
filter(carb == input$fil1) %>%
pull(gear) %>%
unique()
else
flowers %>%
filter(Species == input$fil1) %>%
pull(Petal.Length) %>%
unique()
)
output$filter1 <- renderUI(
radioButtons("fil1","", choices=vards1())
)
output$filter2 <- renderUI(
radioButtons("fil2","", choices=vards2())
)
【讨论】:
正是我想要的!以上是关于R 闪亮的反应单选按钮的主要内容,如果未能解决你的问题,请参考以下文章