Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

R Shiny Reactive Radio Buttons

In the following example I have two static radio buttons representing the mtcars and iris datasets. Upon making a selection, the user is presented with a second set of buttons based on data in each dataset. For the mtcars dataset, the user can filter by selecting from the unique list of carburetors or in the case of the iris dataset, the species. Now, I require another set of buttons based on the carb/species buttons to further filter the data. Say, for the mtcars dataset the list of unique gear selections associated with the carburetor selection and for the Iris the unique set of petal lengths. Given the real world application of what I'm trying to accomplish, there is no getting away from requiring a third set of reactive radio buttons. I just have no clue how to approach the next step.

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())
        })

}

question from:https://stackoverflow.com/questions/65917948/r-shiny-reactive-radio-buttons

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps this may be helpful. You can add another reactive expression to filter your dataset and obtain choices for the third set of radio buttons. I included isolate so that the third set of buttons does not react to changes in the dataset (only changes in the second radio buttons, which is dependent already on the dataset). Please let me know if this is what you had in mind for behavior.

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())
  })
  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...