0

I am trying to create a barplot for COVID data. A portion of the main data frame and the data frame the code entered below generated.

Sample data frame:
dfa <- data.frame("Region" = c("US", "UK", "Brazil", "Italy", "Ireland", "Mexico", "US", "UK", "Brazil", "Italy", "Ireland", "Mexico"),
                  "Date" = c("11/30/20", "11/30/20", "11/30/20", "11/30/20", "11/30/20", "11/30/20", "11/29/20", "11/29/20", "11/29/20", "11/29/20", "11/29/20", "11/29/20"),
                  "Confirmed" = c("1322135", "621332", "52133", "9762", "6332", "77231", "1319135", "620332", "52142", "9562", "6132", "77200"))
library("shiny")
library("DT")
library("dplyr)")

my_ui <- fluidPage(
  selectizeInput(inputId = "region_select_input", 
                 label = "Region(s)", 
                 choices = c("All", sort(unique(df$region))),
                 selected = "All",
                 multiple = TRUE),
  
  dateRangeInput(inputId = "date_range_input",
                 label = "Dates From January-November 2020",
                 start = min(df$date),
                 end = max(df$date),
                 min = min(df$date),
                 max = max(df$date)),

  plotOutput(outputId = "core_3_bar_plot_output")
)
my_server <- function(input, output) {
  output$core_3_bar_plot_output <- renderPlot({
    #This code will generate the picture down below
    df_core3 <- df %>%
      group_by(region) %>%
      summarise(confirmed = max(confirmed, na.rm = TRUE),
                date = Sys.Date()) %>%
    
    #Selectize Code for Bar Plot
    if (input$region_select_input != "All"){
      df_core3 <- df_core3 %>%
        filter(region == input$region_select_input)
    }
    df_cor3 <- df_core3 %>%
      filter(date >= input$date_range_input[1] &
               date <= input$date_range_input[2])
    
    barplot(df_core3$confirmed)

The problem when I run this code is the barplot just shows one bar, and I believe it's because of the max.

What I am trying to achieve is to output whatever region (by its most current confirmed number) is selected by the user in a barplot. But if the user enters 5 or more regions, the barplot will output the five regions with the most confirmed cases. How can I go about this? Thanks in advance!

1

lmnguyen
  • 15
  • 4
  • Welcome to SO! Maybe try `filter(region %in% input$region_select_input)`. Otherwise it's difficult to help you without a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (i.e. a minimal running shiny app and some example data) – starja Dec 01 '20 at 08:26
  • 1
    Sure, I just edited it with the code in its entirety with a sample data table. – lmnguyen Dec 01 '20 at 15:47

0 Answers0