R闪亮的应用程序与ggplot2错误

时间:2017-06-08 19:48:33

标签: r ggplot2 shiny

我正在尝试构建我的第一个闪亮的应用程序并遇到了我无法解决的错误。我搜索过并搜索过,但似乎无法找到解决这个问题的答案。

我的代码在下面使用mtcars数据集。目标是为调查数据集开发视觉和文本分析工具。我目前有两个带调查问题的下拉菜单,一个带有分组变量菜单的下拉菜单,以及一些用于绘图类型的单选按钮。我正在尝试创建第一个图 - 直方图。运行代码后,我得到了正确渲染的数据表,但没有直方图 - 错误:

对象' s1'找不到

我试过这个,有没有用print()包装我的ggplot代码。

非常感谢任何帮助!

谢谢!

library(shiny)
install.packages("shinythemes")
library(shinythemes)
library(ggplot2)
library(dplyr)

mt <- mtcars
ui <- fluidPage(theme=shinytheme("readable"),

                titlePanel("Test"),

                sidebarLayout(

                  sidebarPanel(
                    # use mpg and cyl as survey option groups
                    selectizeInput(inputId = 's1',
                                   label = 'Survey Question 1 (X-Axis)',
                                   choices = c("mpg", "cyl")),

                    selectizeInput(inputId = 's2', 
                                   label ='Survey Question 2 (Y-Axis)', 
                                   choices = c("mpg", "cyl")),

                    # use gear and vs as grouping variables
                    selectizeInput(inputId = 'g', 
                                   label = 'Group By', 
                                   choices = c("Gear"="gear", "VS" = "vs")),

                    # use radio buttons for pot type options
                    radioButtons(inputId = 'plottype', 
                                label = 'Plot Type', 
                                choices = c("Histogram" = "geom_histogram", "Box Plot" = "geom_boxplot", "Scatterplot" = "geom_point")
                    )
                  ), 
                      mainPanel(
                        plotOutput("plot1"), # ui for plot
                        verbatimTextOutput("table1") # ui for table
                    )
                  )
)

server <- function(input, output, session) {

## subset dataset to include only two options for now - include grouping after
  plotData <- reactive({
    ((mt[,c(input$s1,input$s2)])) ## data for plot
  }) 

## render hist plot in ggplot
output$plot1 <- renderPlot({
  d1<-(plotData())
 print(ggplot(d1, aes(x=s1)) + geom_histogram(fill = "dark green", alpha = 0.6, binwidth = 1))
  print(str(d1))
  })

## render summary table for survey questions
output$table1 <- renderPrint({
  summary(plotData())
  })
}
shinyApp(ui = ui, server = server)

## but this works
ggplot(mt, aes(x=mpg)) + geom_histogram(fill = "dark green", alpha = 0.6, binwidth = 1)

1 个答案:

答案 0 :(得分:1)

数据集s1中没有列d1。使用ggplot(d1, aes_string(x=input$s1))