繁体   English   中英

绘图不使用Shiny渲染

[英]Plot not rendering with Shiny

我正在尝试创建一个闪亮的应用程序,该应用程序使用macheish库中的内置数据集显示macheish 但是,当我运行代码时,没有出现任何错误,但是我的ggplot不显示。 这是我看到的:

没有显示ggplot

理想情况下,我希望看到这样的内容

随着ggplot的显示

这是我的代码:

ui.r

library(shiny)
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(input = "datasets", label = "Choose a Dataset:",
                   choices = c("whately_2015", "orchard_2015"))),
  mainPanel(
    plotOutput("ggPlot"))
    )
  )
)

server.r

library(shiny)
library(ggplot2)
library(macleish)

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$datasets,
           "whately_2015" = whately_2015,
           "orchard_2015" = orchard_2015)

    output$ggPlot <- renderPlot({
      p <- ggplot(data = input$datasets, aes(y = temperature, x = when)) + 
        geom_line(color = "darkgray") +
        geom_smooth() +
        xlab("Time") +
        ylab("Temperature (in F)") +
        ggtitle("Weather Data in 2015") +
        theme(plot.title = element_text(hjust = 0.5))
      print(p)

    })
  })
})

有人可以指出我的代码有什么问题来帮助我吗?

这是您需要的更正-

  datasetInput <- reactive({
    switch(input$datasets,
           "whatley_2015" = whately_2015,
           "orchard_2015" = orchard_2015)
  })

    output$ggPlot <- renderPlot({
      p <- ggplot(data = datasetInput(), aes(y = temperature, x = when)) + 
        geom_line(color = "darkgray") +
        geom_smooth() +
        xlab("Time") +
        ylab("Temperature (in F)") +
        ggtitle("Weather Data in 2015") +
        theme(plot.title = element_text(hjust = 0.5))
      print(p)    
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM