Home > database >  How to use dplyr select on an input selection
How to use dplyr select on an input selection

Time:10-06

I'm having trouble figuring out how to select variables then use them in dplyr functions (although filtering is no problem). I've tried some variations using all_of(), or creating a reactive variable, but this rookie can't seem to figure it out.

library(shiny)

ui <- fluidPage(

    titlePanel("Help me figure this out...."),

    sidebarLayout(
        sidebarPanel(
            selectInput("response_var",
                        "Response Variable:",
                        choices = colnames(mtcars)[c(1,4,7)]),
            selectInput("explanatory_var",
                        "Explanatory Variable:",
                        choices = colnames(mtcars)[-c(1,4,7)])
        ),

        mainPanel(
           plotOutput("distPlot")
        )
    )
)

server <- function(input, output) {

    output$distPlot <- renderPlot({
      
      mtcars %>% dplyr::select(input$response_var, input$explanatory_var) %>% 
        ggplot(aes(x = input$explanatory_var, y = input$response_var))
    })
}

shinyApp(ui = ui, server = server)

## I'd like to use the input variables both in select() and ggplot(aes())

CodePudding user response:

One way is to use !! sym(...) with which we evaluate strings as names. Btw. we don't actually need the call to dplyr::select() but I left it there to show how sym() also works in dplyr and ggplot.

library(shiny)
library(dplyr)

shinyApp(ui = fluidPage(
  
  titlePanel("Help me figure this out...."),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("response_var",
                  "Response Variable:",
                  choices = colnames(mtcars)[c(1,4,7)]),
      selectInput("explanatory_var",
                  "Explanatory Variable:",
                  choices = colnames(mtcars)[-c(1,4,7)])
    ),
    
    mainPanel(
      plotOutput("distPlot")
    )
  )
),

  server = function(input, output) {
  
    output$distPlot <- renderPlot({
      print(input$response_var)
      print(input$explanatory_var)
      
      mtcars %>% 
        dplyr::select(!! sym(input$response_var), !! sym(input$explanatory_var)) %>% 
        ggplot(aes(x = !! sym(input$response_var), y = !! sym(input$explanatory_var)))  
        geom_point()
    })
  })

Another way is to use varSelectInput here we only need !! without sym(), because it already returns an object name, unlike selectInput which returns a string.

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


shinyApp(ui = fluidPage(
  
  titlePanel("Help me figure this out...."),
  
  sidebarLayout(
    sidebarPanel(
      varSelectInput(
        "response_var",
        "Response Variable:",
        data = mtcars[c(1,4,7)],
      ),
      varSelectInput(
        "explanatory_var",
        "Explanatory Variable:",
        data = mtcars[-c(1,4,7)],
      )
    ),
    
    mainPanel(
      plotOutput("distPlot")
    )
  )
),

  server = function(input, output) {
  
    output$distPlot <- renderPlot({
      
       
      mtcars %>% 
        ggplot(aes(x = !! input$response_var,
                   y = !! input$explanatory_var))  
        geom_point()
    })
  })
  • Related