没有绘制来自' ggplot2'在闪亮的应用程序

时间:2016-01-06 17:15:33

标签: r shiny

首先,免责声明:我参加了Coursera"开发数据产品"课程。我上个月在课程中,但工作义务占用了我很多时间,我无法在截止日期前完成课程。我继续从事该课程项目,并重新注册了刚刚开始的1月份课程。

我通过RStudio.com观看了关于Shiny特定网站上的课程和教程系列的视频。我有一切工作,除了ggplot的绘图输出。问题是没有输出。也没有错误消息。我无法弄明白,所以我希望有人能够看到我的代码,并指出我正确的方向。

我的R代码如下。

谢谢! (提前。)

ui.R:

## Coursera Data Science Specialization
#  Class: Delivering Data Products
#
#  Author: Richard Ian Carpenter
#  Date Created: 14 Dec 2015
#  Data Updated: 06 Jan 2016
#
## NOTES: 
#
## I am repeating this course, unrolled from Dec 2015 course due to work obligations.
#
## Beyond the notes and videos for the class and the Shiny website, I used the 
#  following:
#
## This Stack Overflow question from 2013 helped with correcting my R code:
#  http://stackoverflow.com/questions/18762962/passing-variable-names-to-model-in-shiny-app
#  
## This shiny example to get the regression output to be in the same format
#  that R uses, instead of a table: https://rich.shinyapps.io/regression/
#

library(shiny)
library(ggplot2)
data("mtcars")

mtcars <- mtcars

ui <- fluidPage(
      sidebarPanel(
            selectInput("dependent", "Dependent Variable:", c(names(mtcars))),
            uiOutput("independent"),
            checkboxInput("factor", "Create factor variables (am, cyl, etc.)?")
      ),

      mainPanel(
            tabsetPanel(
                  tabPanel("Regression Plot",
                           plotOutput("regPlot")),
                  tabPanel("Regression Summary", 
                           verbatimTextOutput("regSummary")))
            )
)

server.R:

## Coursera Data Science Specialization
#  Class: Delivering Data Products
#
#  Author: Richard Ian Carpenter
#  Date Created: 14 Dec 2015
#  Data Updated: 06 Jan 2016
#
## NOTES: 
#
## I am repeating this course, unrolled from Dec 2015 course due to work obligations.
#
## Beyond the notes and videos for the class and the Shiny website, I used the 
#  following:
#
## This Stack Overflow question from 2013 helped with correcting my R code:
#  http://stackoverflow.com/questions/18762962/passing-variable-names-to-model-in-shiny-app
#  
## This shiny example to get the regression output to be in the same format
#  that R uses, instead of a table: https://rich.shinyapps.io/regression/
#

library(shiny)
library(ggplot2)
data("mtcars")

mtcars <- mtcars    

server <- (function(input, output) {

      output$independent <- renderUI({
                            checkboxGroupInput(
                               "independent", "Independent Variables:",
                               names(mtcars)[!names(mtcars) %in% input$dependent],

                               if(input$factor) {
                                   mtcars$cyl <- as.factor(mtcars$cyl)
                                   mtcars$vs <- as.factor(mtcars$vs)
                                   mtcars$am <- as.factor(mtcars$am)
                                   mtcars$gear <- as.factor(mtcars$gear)
                                   mtcars$carb <- as.factor(mtcars$carb)
                               }

                               )
                            })

      regression <- reactive({
                       lm(paste(input$dependent," ~ ",
                          paste(input$independent,
                                collapse = " + ")),
                          data = mtcars)
                    })

      output$regPlot <- reactive({

            renderPlot({p <- ggplot(regression(), 
                             aes(x = paste(input$independent, collapse = " + "), 
                                 y = input$dependent,
                                 environment = environment())
                             )

                        p <- p + geom_point() #+ geom_smooth()

                        print(p)

            }, height = 700)
        })

      output$regSummary <- renderPrint({
                         if(!is.null(input$independent)){
                                    summary(regression())
                         } else {
                              print("Please select the model's dependent and independent variables.")
                         }
                        })
})

0 个答案:

没有答案