【问题标题】:How can I make a line plot using r shiny?如何使用 r shiny 绘制线图?
【发布时间】:2020-12-07 18:07:55
【问题描述】:

这是我第一次使用 Shiny,每次运行代码时都会遇到错误消息。

    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)

我还想知道如何制作所有国家/地区的列表,而不仅仅是选定的国家/地区。

我收到的错误信息:

警告:错误:filter() 输入 ..1 有问题。 x 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能从反应式表达式或观察者内部完成的事情。) ℹ 输入..1location %in% input$sel_location。 83: 错误:filter() 输入 ..1 有问题。 x 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能从反应式表达式或观察者内部完成的事情。) ℹ 输入..1location %in% input$sel_location

我正在使用的数据:

  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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    所有反应函数都需要有reactive_fct({your_reactive_code})。你在renderPlot 中得到了正确的结果,但在reactive 调用中忘记了括号。你还有一个额外的逗号。试试这个:

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

    此外,您的代码中还有其他一些问题 (1) 将绘图分配给变量,但没有调用打印绘图 (2) 帖子标题显示“线图”,但您的代码正在制作条形图,( 3) 您正在将 Covid_data 重新分配给自己,因此您的数据将在不同国家的几次点击中消失。您可以尝试将过滤器放在带有绘图的反应函数中:

    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() 
        })
    }
    

    关于您问题的第二部分,请参阅Shiny - All sub-lists in "choices" must be named?

    【讨论】:

    • 谢谢!这解决了错误,但是一旦我运行它就没有显示图。有什么建议吗?
    • 我已经更新了答案 - 您还有其他问题。如果有效,请接受答案并投票!谢谢
    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 2012-10-30
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2015-03-17
    相关资源
    最近更新 更多