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
Extract English Words from a Text in R
Changing the Order of Dodged Bars in Ggplot2 Barplot
Include a Comma Separator for Data Labels
How to Display Line Numbers for Code Chunks in Rmarkdown HTML and PDF
How to Prevent Blogdown from Rerendering All Posts
How to Pass R Variable into SQLdf
Character String Is Not in a Standard Unambiguous Format
Npc Coordinates of Geom_Point in Ggplot2
How to Highlight Area Between Two Lines? Ggplot
Does Calculating Correlation Between Two Dataframes Require a Loop
Standard Eval with Ggplot2 Without 'Aes_String()'
Puzzled by Xlim/Ylim Behavior in R
Use 'J' to Select the Join Column of 'X' and All Its Non-Join Columns
Do I Need to Reshape This Wide Data to Effectively Use Ggplot2