【问题标题】:Reactive function giving error in Shiny framework RShiny框架R中的反应函数给出错误
【发布时间】:2020-09-17 06:01:50
【问题描述】:

我正在尝试用闪亮的单选按钮制作一个反应图。但是,在使用反应式表达式后,我看不到任何情节。

这是完整应用程序的可重现代码

# Loading libs
library(shiny)
library(shinythemes)
library(ggplot2)
library(mapdata)
library(mapproj)

ui <- fluidPage(theme = shinytheme("cosmo"),
    navbarPage("PageTitle",
        tabPanel("US-Data-Visualizer",
                sidebarPanel(tags$h2("Map Visualizer"), tags$h4("Visualize the counts on US map, state wise"),
                      radioButtons("radio_option", label = "Select from the following options:",
                          choices = list("Total Cases" = "state$TOTAL.CASES", "New Cases" = "state$NEW.CASES",
                              "Active Cases" = "state$ACTIVE.CASES", "Total Deaths" = "state$TOTAL.DEATHS", 
                              "New Deaths" = "state$NEW.DEATHS", "Total Tests" = "state$TOTAL.TESTS"), 
                              selected = "state$TOTAL.CASES")),
                mainPanel(plotOutput("us_cases"))))
) #close fluid page

server <- function(input, output){
  state <- read.csv("https://raw.githubusercontent.com/spriyansh/ShinyApps/master/datasets/USA_State_Wise_Data.csv", fileEncoding="UTF-8")
  us_geo_data <- map_data("county")
  reactive_df <- reactive({
    us_geo_data <- data.table(us_geo_data)
    covid_map <- data.frame(state_names=unique(us_geo_data$region),
    values = input$radio_option)
    setkey(us_geo_data,region)
    covid_map <- data.table(covid_map)
    setkey(covid_map,state_names)
    map.df <- us_geo_data[covid_map]})
  output$us_cases <- renderPlot(
    ggplot(reactive_df()$map.df,
           aes(x = reactive_df()$map.df$long, y = reactive_df()$map.df$lat,
               group = reactive_df()$map.df$group,fill = reactive_df()$covid_map$values)) + 
      geom_polygon(alpha = 0.8) + coord_map() 
  )}

shinyApp(ui, server)

【问题讨论】:

  • 嗨,不需要reactive_option()$optreactive_option()就够了。 reactive_df()$manipulated_df 也一样
  • 代码运行正常,但是没有显示plot
  • 我认为你不需要isolate。删除它并尝试output$plot &lt;- renderPlot({ggplot(reactive_df())}),但不要忘记在ggplot 中使用aes 并添加geom_line,或geom_point 或其他内容
  • 是的,我试过了。并在问题中附加了实际代码(驱动链接)。如果您可以阅读它会非常有帮助。
  • 我已经编辑了我的答案

标签: r shiny shiny-server shinyapps shiny-reactivity


【解决方案1】:

您的示例不可重现,因为没有数据,也没有 library 调用。请参阅 herehere 了解如何制作一个最小的、可重现的示例。

按照 OP 的编辑进行编辑:

问题是您尝试从reactive_df 返回不同的数据帧,这是不可能的。您应该将covid_mapstate_names 合并在一起,以便您需要的所有信息都在map.df 中,reactive 返回,因为它是最后一个操作。然后,您可以删除ggplot 中的长表达式。

radioButtons 中的选择也有问题。最好只将列名作为选项,然后使用state[[input$radio_option]] 调用过滤这些列名的数据。

这是你的代码:

# Loading libs
library(shiny)
library(shinythemes)
library(ggplot2)
library(mapdata)
library(mapproj)
library(data.table)
library(dplyr)

state <- read.csv("https://raw.githubusercontent.com/spriyansh/ShinyApps/master/datasets/USA_State_Wise_Data.csv", fileEncoding = "UTF-8")
us_geo_data <- map_data("county")

ui <- fluidPage(
  theme = shinytheme("cosmo"),
  navbarPage(
    "PageTitle",
    tabPanel(
      "US-Data-Visualizer",
      sidebarPanel(
        tags$h2("Map Visualizer"), tags$h4("Visualize the counts on US map, state wise"),
        radioButtons("radio_option",
          label = "Select from the following options:",
          choices = list(
            "Total Cases" = "TOTAL.CASES", "New Cases" = "NEW.CASES",
            "Active Cases" = "ACTIVE.CASES", "Total Deaths" = "TOTAL.DEATHS",
            "New Deaths" = "NEW.DEATHS", "Total Tests" = "TOTAL.TESTS"
          ),
          selected = "TOTAL.CASES"
        )
      ),
      mainPanel(plotOutput("us_cases"))
    )
  )
) # close fluid page

server <- function(input, output) {

  reactive_df <- reactive({
    us_geo_data <- data.table(us_geo_data)
    covid_map <- data.frame(
      state_names = unique(us_geo_data$region),
      values = state[[input$radio_option]]
    )
    setkey(us_geo_data, region)
    covid_map <- data.table(covid_map)
    setkey(covid_map, state_names)
    map.df <- dplyr::left_join(us_geo_data, covid_map, by = c("region" = "state_names"))
  })
  output$us_cases <- renderPlot(
    ggplot(
      reactive_df(),
      aes(
        x = long, y = lat,
        group = group, fill = values
      )
    ) +
      geom_polygon(alpha = 0.8) +
      coord_map()
  )
}

shinyApp(ui, server)

【讨论】:

  • 嗨!我已经编辑了问题并添加了可重现的代码。
猜你喜欢
  • 2014-12-14
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 2020-12-20
  • 2021-11-29
  • 1970-01-01
  • 2020-02-12
相关资源
最近更新 更多