首页 > 解决方案 > 在 R Shiny 中选择“全部”单选按钮选项

问题描述

我正在尝试创建一个具有单选按钮的闪亮应用程序,并允许用户选择一个“全部”按钮,该按钮显示此过滤器之前的所有数据。

我在下面发布了我的完整代码,这里是当前实时应用程序的链接。

正如在实时应用程序中看到的那样,我希望用户能够选择季度下的“全部”单选按钮,这将显示所有数据,无论季度如何。目前,季度下的其他选项按计划工作。

在找到了一些其他类似的帖子后,我尝试在代码的服务器部分的过滤中设置 if else 函数,并在代码的 ui 部分的单选按钮中创建了选择,但输出没有运气。我也在努力找出是否需要对 ui 代码的“选择”部分进行其他更改。

任何建议将不胜感激!

library(shiny)
library(tidyverse)
library(hrbrthemes)

score_data <- read_csv("https://raw.githubusercontent.com/mikemaieli/superbowlsquares/master/superbowlscores.csv")

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            sliderInput("yearInput",
                        "Year",
                        min = 1960,
                        max = 2020,
                        value = c(1967, 2019),
                        sep = "",
                        ticks = 10),
            radioButtons("quarterInput", "Quarter",
                         choices = c("All" = "???",
                                     "1st quarter" = "1",
                                     "2nd quarter" = "2",
                                     "3rd quarter" = "3",
                                     "4th quarter" = "4",
                                     "Overtime" = "5"),
                         selected = "1"),
            ),
        mainPanel(
          plotOutput("heatmap"),
        )
    )
)

server <- function(input, output) {
  output$heatmap <- 
    renderPlot({
      digit_counts <- score_data %>% 
        mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>% 
        select(year, superbowl, quarter, afc_digit, nfc_digit) %>% 
        mutate_all(as.character) %>% 
        filter(year >= input$yearInput[1],
               year <= input$yearInput[2],
               quarter == input$quarterInput) %>% 
        group_by(afc_digit, nfc_digit) %>% 
        summarize(occurances = n())

      ggplot(digit_counts, aes( x = afc_digit,
                                y = nfc_digit)) +
        geom_tile(aes(fill = occurances), color = "black") +
        geom_text(aes(label = scales::percent((occurances/sum(digit_counts$occurances)))),
                  color = "white") +
        scale_fill_gradient(low = "gray75", high = "dodgerblue4", na.value = "white") +
        scale_x_discrete(position = "top",
                         limits = c("0","1","2","3","4","5","6","7","8","9")) +
        scale_y_discrete(limits = rev(c("0","1","2","3","4","5","6","7","8","9"))) +
        labs(x = "AFC",
             y = "NFC") +
        theme_minimal() + 
        theme(panel.grid.major = element_blank(),
              legend.position = "none") +
        geom_vline(xintercept=c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color="black", size = .3) +
        geom_hline(yintercept=c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color="black", size = .3)
    })
}

shinyApp(ui = ui, server = server)

标签: rshinyradio-button

解决方案


如下更改您的server功能。

  server <- function(input, output, session) { digit_counts <- reactive({
    if (input$quarterInput == "???") {
    score_data %>%
      mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>%
      select(year, superbowl, quarter, afc_digit, nfc_digit) %>%
      mutate_all(as.character) %>%
      group_by(afc_digit, nfc_digit) %>%
      summarize(occurances = n())
    } else {
   score_data %>%
      mutate(afc_digit = afc_total_score %% 10, nfc_digit = nfc_total_score %% 10) %>%
      select(year, superbowl, quarter, afc_digit, nfc_digit) %>%
      mutate_all(as.character) %>%
      filter(year >= input$yearInput[1],
             year <= input$yearInput[2],
             quarter == input$quarterInput) %>%
      group_by(afc_digit, nfc_digit) %>%
      summarize(occurances = n())
}})

  output$heatmap <- renderPlot({
  ggplot(digit_counts(), aes( x = afc_digit,
                            y = nfc_digit)) +
    geom_tile(aes(fill = occurances), color = "black") +
    geom_text(aes(label = scales::percent((occurances/sum(digit_counts()$occurances)))),
              color = "white") +
    scale_fill_gradient(low = "gray75", high = "dodgerblue4", na.value = "white") +
    scale_x_discrete(position = "top",
                     limits = c("0","1","2","3","4","5","6","7","8","9")) +
    scale_y_discrete(limits = rev(c("0","1","2","3","4","5","6","7","8","9"))) +
    labs(x = "AFC",
         y = "NFC") +
    theme_minimal() +
    theme(panel.grid.major = element_blank(),
          legend.position = "none") +
    geom_vline(xintercept = c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color = "black", size = .3) +
    geom_hline(yintercept = c(.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5), color = "black", size = .3)
})}

在这里,我的理解是,当您选择All作为您的选择时,不应对数据应用任何过滤器。所以我已经删除了条件中的过滤if条件。


推荐阅读