ERROR:`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class reactiveExpr/reactive

大城市里の小女人 提交于 2021-02-17 02:01:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!