简体   繁体   中英

update x / y plot based on user input (Shiny)

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:

在此处输入图像描述

Current 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM