Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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.

like image 788
Arthur Vaz Avatar asked Jul 18 '18 17:07

Arthur Vaz


People also ask

What does Fortify () do in R?

The fortify function converts an S3 object generated by evalmod to a data frame for ggplot2.

How do you create a Dataframe in R?

We can create a dataframe in R by passing the variable a,b,c,d into the data. frame() function. We can R create dataframe and name the columns with name() and simply specify the name of the variables.

What does as data frame do in R?

as. data. frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors.


1 Answers

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)
like image 187
Artem Avatar answered Nov 01 '22 10:11

Artem