在R Shiny中选择“所有”单选按钮选项

时间:2019-12-31 03:26:15

标签: r shiny radio-button

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

我在下面发布了完整的代码,这是指向current live app的链接。

如在实时应用程序中所见,我希望用户能够选择季度以下的“全部”单选按钮,该按钮将显示所有数据,而不考虑季度。目前,季度计划中的其他选项按计划进行。

找到其他类似的帖子后,我尝试在代码的服务器部分的过滤中设置其他功能,并在代码的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)

1 个答案:

答案 0 :(得分:0)

按如下所示更改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条件下的过滤条件。