在闪亮中使用无功输入时遇到问题

时间:2017-08-09 18:32:05

标签: r shiny

我不熟悉使用Shiny和R.我必须开发一个Web应用程序来显示基于年份的图表。闪亮的服务器必须从ui(开始年份和结束年份)获取输入并进行计算并绘制输出。我编写了一个R脚本来创建一个名为years的向量,其中包含所选范围内的不同年份,并将其绑定到数据框。然后,它会向表中添加新的计算列并绘制不同的列。 这是代码

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)})
  sample <- data.frame(years)
  sample$timestepdays <- (sample$years-min(sample$years))*365
  sample$timestep <- (sample$years-min(sample$years))
  sample$pop <- sample$timestepdays *500 +839000
  sample$no_of_trips <- sample$pop*4.4045
  sample$NE <- sample$no_of_trips*0.00000003+0.3738
  sample$Delta_VE_logit <- sample$timestep*(-0.149) +5 
  sample$Delta_VE <- (exp(sample$Delta_VE_logit)/(exp(sample$Delta_VE_logit)+1))*(1-0.5)+0.5
  sample$VMT <- 7.3 * sample$no_of_trips
  sample$ER_per_mile <- sample$Delta_VE * sample$NE
  sample$CO2 <- (sample$ER_per_mile * sample$VMT ) /10000

 output$plot1 <- renderPlot({lines.default(sample&years,sample$population)})
   output$plot2 <- renderPlot({lines.default(sample&years,sample$no_of_trips)})
     output$plot3 <- renderPlot({lines.default(sample&no_of_trips,sample$NE)})

}

shinyApp(ui = ui, server = server)

我收到以下错误

Warning: Error in as.data.frame.default: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame
Stack trace (innermost first):
    40: as.data.frame.default
    39: as.data.frame
    38: data.frame
    37: server [C:/Users/R-Jaikumar/Downloads/shiny-quickstart-1/downloads/code/01-template.R#17]
     1: runApp
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame

1 个答案:

答案 0 :(得分:0)

我认为make sample作为被动对象应该有效,并且函数名称不是数据框名称的好选择。不太确定你在绘制什么,但是你可以稍后改变它。请尝试以下代码:

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)