user8229029
user8229029

Reputation: 1162

How to pass varSelectInput to ggplot2 R Shiny

I'm making an app where users select the x and y axes, and the facet variable they want from a list of data frame names using varSelectInput. I've tried many different ways I've seen suggested on stackOverflow, and can't get any of them to work in my case. Here's the renderUI code with the inputId's for each variable.

    output$descrxaxis_Variable <- renderUI({
      varSelectInput(inputId = "descrxaxis", label = "Choose x axis variable", data = descrDataMelted_selVars$df)})
    
    output$descryaxis_Variable <- renderUI({
      varSelectInput(inputId = "descryaxis", label = "Choose y axis variable(s)", data = descrDataMelted_selVars$df,
                     multiple = TRUE, selectize = TRUE)})
    
    output$descrFacet_Variable <- renderUI({
      varSelectInput(inputId = "descrfacet", label = "Choose facet variable(s)", data = descrDataMelted_selVars$df,
                     multiple = TRUE, selectize = TRUE)})

My code currently looks like this, which follows the example here: https://shiny.rstudio.com/reference/shiny/latest/varSelectInput.html. I'm getting an error that tells me the input cannot be a list with this code. The input$descrxaxis contains column names like "day", "hour", "month", "TA", "PP", etc.

    observeEvent(input$descrBtnPlot,{
      p <- ggplot(descrDataMelted_selVars$df, aes(!!input$descrxaxis, !!input$descryaxis))
      
      output$descrSummaryStatsPlot <- renderPlot(p)
    })

Otherwise, I've tried using the .data[[column]] notation to do this, and the !!!rlang::syms(column) notation. Any help is greatly appreciated.

Upvotes: 0

Views: 497

Answers (1)

TimTeaFan
TimTeaFan

Reputation: 18551

The problem is that ggplots aes() takes first x and y arguments and then .... The first problem is that when varSelectInput allows for multiple selections it will return a list of names which needs to be spliced into aes(). Splicing !!! however only works when using the dots (see here). You could use one of your inputs to splice arguments after x and y into ..., but in this case the arguments must be named to have an effect, just splicing in variable names after x and y won't change your plot. Please try out the example below with the workaround (aes2()) offered in the answer linked above. As you can see, only the first two variables (x and y) will have an effect on the plot.

library(shiny)
library(dplyr)
library(ggplot2)

aes2 <- function(...) {
  eval(expr(aes(!!!enquos(...))))
}

shinyApp(ui = fluidPage(
  
  sidebarLayout(
    sidebarPanel(
      
      varSelectInput('variables',
                     'Choose variables',
                     mtcars,
                     selected = c("mpg","cyl"),
                     multiple = TRUE)
      
    ),
    mainPanel(
      plotOutput("graph")
    )
  )
),

server = function(input, output) {
  
  output$graph <- renderPlot({
  
    mtcars %>% 
      ggplot(aes2(!!! input$variables)) +
      geom_point()
    
  })

})

Upvotes: 2

Related Questions