Arthur Vaz
Arthur Vaz

Reputation: 409

Error in : `data` must be a data frame, or other object coercible by `fortify()`

I am working on Shiny vizualization with 2 inputs.

Dataset:

est_popai <- data.frame(concat = c("A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2"),
                  variables  = c("quantity","quantity","quantity","quantity","quantity","quantity","price","price","price","price","price","price","quality","quality","quality","quality","quality","quality","size","size","size","size","size","size"),
                   values = round(runif(24, 5.0, 7.5),2)
                 )  

UI:

ui <- fluidPage(
  headerPanel(
    h1("Combinacao de atributos")
  ),
  sidebarPanel(
    selectInput("xcol"," Variavel X", unique(est_popai$variable),
                selected = 'price'),
    selectInput("ycol"," Variavel y", unique(est_popai$variable),
                selected = 'size')
  ),
  mainPanel(
    plotOutput("plot1") 
  )
)

Server:

server <- function(input, output) {
  
  selectData <- reactive  ({
    
    est_popai[est_popai$variable == input$xcol | est_popai$variable == input$ycol,] %>%
      unique() %>%
      spread(variable,value)  
  })
  
  
  output$plot1 <- renderPlot({
    ggplot(data = selectData, aes(x = input$xcol, y = input$ycol)) +
      geom_point()
  })
  
  
}

Run:

shinyApp(ui = ui, server = server)

When I run the whole code i got this error message:

Warning: Error in : data must be a data frame, or other object coercible by fortify(), not an S3 object with class reactiveExpr/reactive [No stack trace available]

I've tried to add as.data.frame() function with no success. Someone could help me to solve this erro, I've been searching a while.

Upvotes: 19

Views: 26641

Answers (1)

Artem
Artem

Reputation: 3414

Copy-pasted from OP.

Please see below the corrected script. The errors were in 3 parts:

1- I forgot to add () in data ggplot function data = selectData()

2- The objects had different names, I forgot the letter s in variable and value objects

3- aes() is supposed to be aes_string() in ggplot function

ui <- fluidPage(
  headerPanel(
    h1("Combinacao de atributos")
  ),
  sidebarPanel(
    selectInput("xcol"," Variavel X", unique(est_popai$variables),
                selected = 'price'),
    selectInput("ycol"," Variavel y", unique(est_popai$variables),
                selected = 'size')
  ),
  mainPanel(
    plotOutput("plot1") 
  )
)

server <- function(input, output) {
  
  selectData <- reactive  ({
    
    est_popai[est_popai$variables == input$xcol | est_popai$variables == input$ycol,] %>%
      unique() %>%
      spread(variables,values)  
  })
  
  
  output$plot1 <- renderPlot({
    ggplot(data = selectData(), aes_string(x = input$xcol, y = input$ycol)) +
      geom_point()
  })
  
  
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions