JC31
JC31

Reputation: 23

R Shiny app with ggplot2 error

I am trying to build my first shiny app and have come across an error I can't resolve. I have searched and searched but can't seem to find an answer that resolves this question.

My code is below using the mtcars dataset. The goal is to develop visual and text analysis tools for a survey dataset. I currently have two drop down menus with survey questions, a drop down menu with a grouping variable menu, and some radio buttons for plot types. I am trying to create the first plot - a histogram. After I run the code I get the correctly rendered data tables, but no histogram plot - and the error:

object 's1' not found

I have tried this with and without wrapping my ggplot code with print().

Any help would be very appreciated !

Thanks!


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)

Upvotes: 2

Views: 225

Answers (1)

lukeA
lukeA

Reputation: 54237

There's no column s1 in the data set d1. Use ggplot(d1, aes_string(x=input$s1)).

Upvotes: 1

Related Questions