问题
I am trying to build a Corona Dashboard. Where If someone selects State from the dropdown, District wise cases to be displayed in the graph. E.g. If someone selects Gujarat, it shows district wise cases in Bar chart. Someone change it to Maharashtra, It should update with the district of Maharashtra.
But I am getting "ERROR:data
must be a data frame, or other object coercible by fortify()
, not an S3 object with class reactiveExpr/reactive" error.
library(shiny)
library(readxl)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
# Application title
titlePanel("Corona Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput(inputId = "state",
label = "Select the State",
choices = unique(data$`Detected State`))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("stateplot")
)
)
)
data <- read_excel("Live Data Worldometer.xlsx", sheet = "IndiaData")
data <- data[,c(-2,-1)]
server <- function(session, input, output) {
d1 <- reactive({data %>% group_by(`Detected State`) %>% count(`Detected District`) %>% filter(`Detected State` == input$state)
})
output$stateplot <- renderPlot({
ggplot(d1, aes(d1$`Detected District`, d1$n))+geom_bar(stat = "identity")
})
}
# Run the application
shinyApp(ui = ui, server = server)
This is the output I am getting
回答1:
d1
is not a dataframe but an expression that needs to be evaluated (reactive
does not return a dataframe). You need to active the reactive element before using it in ggplot2
server <- function(session, input, output) {
d1 <- reactive({
data %>% group_by(`Detected State`) %>%
count(`Detected District`) %>%
filter(`Detected State` == input$state)
})
output$stateplot <- renderPlot({
ggplot(d1(), aes(x = `Detected District`, y = n)) +
geom_bar(stat = "identity")
})
}
来源:https://stackoverflow.com/questions/61156354/errordata-must-be-a-data-frame-or-other-object-coercible-by-fortify-not