Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

Select "All" radio button option in R Shiny

$
0
0

I am trying to create a shiny app which has radio buttons and allows a user to select an "All" button which shows all of the data prior to this filter.

I have posted my full code below and here is a link to the current live app.

As seen in the live app, I would like to have users be able to select the "All" radio button under quarter which would show all of the data, regardless of quarter. Currently the other options under quarter work as planned.

After finding a few other similar posts, I tried to set up if else functions within the filtering in the server portion of the code and created the choice within the radio button in the ui portion of the code, but had no luck with the output. I am also struggling to find out if there are other changes I need to make to the "choices" part of the ui code.

Any advice would be greatly appreciated!

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)

Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>