Given a shiny application with a ggplot2 plot, how would you update which x & y variable are used to construct the plot based on user input?
Code:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("xcol",
"X:",
choices = c("Sepal.Length", "Sepal.Width")
),
selectInput("ycol",
"Y:",
choices = c("Sepal.Length", "Sepal.Width")
)
),
mainPanel(plotOutput("plot"))
)
)
server <- function(input,output) {
output$plot <- renderPlot({
iris %>%
ggplot(aes(input$xcol, input$ycol)) +
geom_point()
})
}
shinyApp(ui, server)
Desired output:
You are trying to map aesthetics with character vectors in the aes
function. You need aes_string
instead:
###<Omitted Library Calls and UI>
server <- function(input,output) {
output$plot <- renderPlot({
iris %>%
ggplot(aes_string(x= input$xcol, y = input$ycol)) +
geom_point()
})
}
###<Omitted shinyApp call>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.