I don't understand why my R code give me this error:
data
must be a data frame, or other object coercible byfortify()
, not a numeric vector.
library(shiny)
library(ggplot2)
ui <- fluidPage( sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100), plotOutput("ggplot") )
server <- function(input, output) { output$ggplot <- renderPlot({
ggplot(data=rnorm(input$num), aes(input$num)) geom_histogram() }) }
shinyApp(ui = ui, server = server)
CodePudding user response:
The first argument to ggplot
should be a data frame. You have supplied the output of rnorm()
, which is a vector. That's the first problem.
The second is that aes()
should refer to a column name in the supplied data frame.
I would create a data frame first using input$num
. Something like this:
server <- function(input, output) {
data_df <- data.frame(x = rnorm(as.numeric(input$num)))
output$ggplot <- renderPlot({
ggplot(data = data_df, aes(x = x)) geom_histogram()
})
}