简体   繁体   中英

How can I make a line plot using r shiny?

It is my first time using shiny and I am facing an error message every time that I run the code.

    server <- function(input, output, 
    session){
    #plot data
    data <- reactive(
    req(input$sel_location),
    Covid_data <- Covid_data %>%
    filter(location %in% input$sel_location)
    )
    #plot 
    output$plot <- renderPlot({
    Covid_plot <- ggplot(data(), aes(
    x= date, y = new_cases
    )) + geom_bar() 
    })


    }

    ui <- basicPage(
    h1("Covid data across the world"),
    selectInput(inputId = "sel_location",
          label = "Choose country",
          list("India", "Pakistan")),
    plotOutput("plot")
    )

    shinyApp(ui = ui, server = server)

I would also like to know how I can make a list of all countries, instead of just the selected ones.

The error message I receive:

Warning: Error in: Problem with filter() input ..1 . x Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) ℹ Input ..1 is location %in% input$sel_location . 83: Error: Problem with filter() input ..1 . x Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) ℹ Input ..1 is location %in% input$sel_location .

The data I am using:

  iso_code continent    location       date total_cases new_cases new_cases_smoothed total_deaths
1      AFG      Asia Afghanistan 2020-01-23          NA         0                 NA           NA
2      AFG      Asia Afghanistan 2020-01-24          NA         0                 NA           NA
3      AFG      Asia Afghanistan 2020-01-25          NA         0                 NA           NA
4      AFG      Asia Afghanistan 2020-01-26          NA         0                 NA           NA
5      AFG      Asia Afghanistan 2020-01-27          NA         0                 NA           NA
6      AFG      Asia Afghanistan 2020-01-28          NA         0                  0           NA
  new_deaths new_deaths_smoothed total_cases_per_million new_cases_per_million
1          0                  NA                      NA                     0
2          0                  NA                      NA                     0
3          0                  NA                      NA                     0
4          0                  NA                      NA                     0
5          0                  NA                      NA                     0
6          0                   0                      NA                     0
  new_cases_smoothed_per_million total_deaths_per_million new_deaths_per_million
1                             NA                       NA                      0
2                             NA                       NA                      0
3                             NA                       NA                      0
4                             NA                       NA                      0
5                             NA                       NA                      0
6                              0                       NA                      0
  new_deaths_smoothed_per_million reproduction_rate icu_patients icu_patients_per_million hosp_patients
1                              NA                NA           NA                       NA            NA
2                              NA                NA           NA                       NA            NA
3                              NA                NA           NA                       NA            NA
4                              NA                NA           NA                       NA            NA
5                              NA                NA           NA                       NA            NA
6                               0                NA           NA                       NA            NA
  hosp_patients_per_million weekly_icu_admissions weekly_icu_admissions_per_million weekly_hosp_admissions
1                        NA                    NA                                NA                     NA
2                        NA                    NA                                NA                     NA
3                        NA                    NA                                NA                     NA
4                        NA                    NA                                NA                     NA
5                        NA                    NA                                NA                     NA
6                        NA                    NA                                NA                     NA
  weekly_hosp_admissions_per_million total_tests new_tests total_tests_per_thousand new_tests_per_thousand
1                                 NA          NA        NA                       NA                     NA
2                                 NA          NA        NA                       NA                     NA
3                                 NA          NA        NA                       NA                     NA
4                                 NA          NA        NA                       NA                     NA
5                                 NA          NA        NA                       NA                     NA
6                                 NA          NA        NA                       NA                     NA
  new_tests_smoothed new_tests_smoothed_per_thousand positive_rate tests_per_case tests_units
1                 NA                              NA            NA             NA            
2                 NA                              NA            NA             NA            
3                 NA                              NA            NA             NA            
4                 NA                              NA            NA             NA            
5                 NA                              NA            NA             NA            
6                 NA                              NA            NA             NA            
  stringency_index population population_density median_age aged_65_older aged_70_older gdp_per_capita
1                0   38928341             54.422       18.6         2.581         1.337       1803.987
2                0   38928341             54.422       18.6         2.581         1.337       1803.987
3                0   38928341             54.422       18.6         2.581         1.337       1803.987
4                0   38928341             54.422       18.6         2.581         1.337       1803.987
5                0   38928341             54.422       18.6         2.581         1.337       1803.987
6                0   38928341             54.422       18.6         2.581         1.337       1803.987
  extreme_poverty cardiovasc_death_rate diabetes_prevalence female_smokers male_smokers
1              NA               597.029                9.59             NA           NA
2              NA               597.029                9.59             NA           NA
3              NA               597.029                9.59             NA           NA
4              NA               597.029                9.59             NA           NA
5              NA               597.029                9.59             NA           NA
6              NA               597.029                9.59             NA           NA
  handwashing_facilities hospital_beds_per_thousand life_expectancy human_development_index
1                 37.746                        0.5           64.83                   0.498
2                 37.746                        0.5           64.83                   0.498
3                 37.746                        0.5           64.83                   0.498
4                 37.746                        0.5           64.83                   0.498
5                 37.746                        0.5           64.83                   0.498
6                 37.746                        0.5           64.83                   0.498

All reactive functions need to have reactive_fct({your_reactive_code}) . You got that right in your renderPlot but forgot the brackets in your reactive call. You also have an extra comma. Try this:

#plot data
data <- reactive({
    req(input$sel_location)
    Covid_data <- Covid_data %>%
        filter(location %in% input$sel_location)
})

Also, you have several other troubles in your code (1) Assigning a plot to a variable, but no call to print the plot (2) post title says "line plot" but your code is making a bar plot, (3) you are re-assigning Covid_data to itself so your data will be gone in a couple clicks of different countries. You might try putting the filter inside the reactive function with the plot:

server <- function(input, output, session){
    output$plot <- renderPlot({
        my_data <- Covid_data %>%
            filter(location %in% input$sel_location)
        ggplot(my_data, aes(x= date, y = new_cases)) + 
            geom_line() 
    })
}

For the second part of your question, see Shiny - All sub-lists in "choices" must be named?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM