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 byfortify()
, 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.
The fortify function converts an S3 object generated by evalmod to a data frame for ggplot2.
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.
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.
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
invariable
andvalue
objects3-
aes()
is supposed to beaes_string()
inggplot
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With