Trouble in interactive ggplot2 graphic in shiny

I get an error "'data' must be a [34m<data.frame>[39m, or an object coercible by 'fortify()", not a double vector.
I am trying to provide a slider input from 2 to 50 that is the number of values generated by a random number generator that will then be plotted as a histogram. I thought this would be a simple task.

Here is my attempt:

library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Multi-Selection Example"),
sidebarLayout(
sidebarPanel(
sliderInput("sampleN","Select a sample size between 2 and 50", min=2, max=50, value=10 )
),
mainPanel(
plotOutput("plot1"),
plotOutput("plot2")
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
data1 <- runif(input$sampleN, 0, 20)
ggplot(data1, aes(x=data1)) + geom_histogram()
})
output$plot2 <- renderPlot({
data1 <- runif(input$sampleN, 0, 20)
ggplot(data1, aes(x=data1)) + geom_histogram()
})
}
shinyApp(ui, server)

Hello, I believe the issue is data1 needs to be a data frame, to be used in ggplot. Also, instead of aes(x=data1), try setting x in aes to the column name containing the random values in the data frame.

library(shiny)
library(ggplot2)
ui <- fluidPage(
  titlePanel("Multi-Selection Example"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("sampleN","Select a sample size between 2 and 50", min=2, max=50, value=10 )
    ),
    mainPanel(
      plotOutput("plot1"),
      plotOutput("plot2")
    )
  )
)
server <- function(input, output) {
  output$plot1 <- renderPlot({
    data1 <- data.frame(value=runif(input$sampleN, 0, 20))
    ggplot(data1, aes(x=value)) + geom_histogram()
  })
  output$plot2 <- renderPlot({
    data1 <- data.frame(value= runif(input$sampleN, 0, 20))
    ggplot(data1, aes(x=value)) + geom_histogram()
  })
}
shinyApp(ui, server)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.