反应函数在 Shiny 框架 R 中给出错误
Reactive function giving error in Shiny framework R
我正在尝试制作一个带有闪亮单选按钮的反应图。但是,在使用反应式表达式后,我看不到任何情节。
这是完整应用程序的可重现代码
# 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)
您的示例不可重现,因为没有数据也没有 library
调用。请参阅 here and here 了解如何制作最小的、可重现的示例。
根据 OP 的编辑进行编辑:
问题是您尝试 return 来自 reactive_df
的不同数据帧,这是不可能的。您应该将 covid_map
和 state_names
合并在一起,这样您需要的所有信息都在 map.df
中,return 由 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)
我正在尝试制作一个带有闪亮单选按钮的反应图。但是,在使用反应式表达式后,我看不到任何情节。
这是完整应用程序的可重现代码
# 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)
您的示例不可重现,因为没有数据也没有 library
调用。请参阅 here and here 了解如何制作最小的、可重现的示例。
根据 OP 的编辑进行编辑:
问题是您尝试 return 来自 reactive_df
的不同数据帧,这是不可能的。您应该将 covid_map
和 state_names
合并在一起,这样您需要的所有信息都在 map.df
中,return 由 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)