Error in As.Data.Frame.Default: Cannot Coerce Class "C("Reactiveexpr", "Reactive")" to a Data.Frame in Shiny

Error in as.data.frame.default: cannot coerce class c(reactiveExpr, reactive) to a data.frame in Shiny

As Chi Pak has mentioned, reactive expressions must contain () at the end. Changing d.f to d.f() fixes the issue.

Error in as.data.frame.default: cannot coerce class c(reactiveExpr, reactive) to a data.frame

I think that the problem is that your reactive does not know what pos variable is in case that if statement is not executed, that is in case input$line == 'All'. Then you are asking it to return variable unknown in the context.

Having problems with using reactive inputs in shiny

I think make sample as a reactive object shall work, and a function name is not a good choice as a name of a data frame. Not quite sure what you are plotting, but you may change it later. Try code below:

library(shiny)
ui <- fluidPage(
sliderInput(inputId = "startyear",
label = "Choose starting year",
value = 25, min = 2016, max = 2050),
sliderInput(inputId = "endyear",
label = "Choose ending year",
value = 25, min = 2016, max = 2050),
plotOutput("plot1"),
plotOutput("plot2"),
plotOutput("plot3")
)

server <- function(input, output) {
years <- reactive({(input$startyear:input$endyear)})
sam <- reactive({
years = years()
timestepdays <- (years-min(years))*365
timestep <- (years-min(years))
pop <- timestepdays *500 +839000
no_of_trips <- pop*4.4045
NE <- no_of_trips*0.00000003+0.3738
Delta_VE_logit <- timestep*(-0.149) +5
Delta_VE <- (exp(Delta_VE_logit)/(exp(Delta_VE_logit)+1))*(1-0.5)+0.5
VMT <- 7.3 * no_of_trips
ER_per_mile <- Delta_VE * NE
CO2 <- (ER_per_mile * VMT ) /10000
sam = data.frame(years, timestepdays, timestep, pop, no_of_trips, NE, Delta_VE_logit,
Delta_VE, VMT, ER_per_mile, CO2)
})

output$plot1 <- renderPlot({
line(sam()$years,sam()$pop)
})
output$plot2 <- renderPlot({
line(sam()$years,sam()$no_of_trips)
})
output$plot3 <- renderPlot({
line(sam()$no_of_trips,sam()$NE)
})
}

shinyApp(ui = ui, server = server)


Related Topics



Leave a reply



Submit