Subset dataset basen on dynamic column name in a shiny app

Im trying to subset my dataset based on column names by using a shiny widget but it does not seem to work.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
diamonds<-structure(list(`Approved Amount_UA Equivalent` = c(690221.630165446, 
                                                             75290000, 27500000, 2131605.31196044, 7580000, 5920000, 5947178.82036962, 
                                                             191844.478076439, 8e+07, 11842251.7331135, 11842251.7331135, 
                                                             11842251.7331135, 22250000, 27990000, 2273712.3327578, 89721563.7663194, 
                                                             14933054.9695902, 1200000, 38520632.9893852, 72490558.1048068
), `Approved Amount_USDEquivalent` = c(920065.43301054, 100361570, 
                                       36657500, 2841429.88084326, 10104140, 7891360, 7927589.3675527, 
                                       255728.689275894, 106640000, 15785721.5602403, 15785721.5602403, 
                                       15785721.5602403, 29659250, 37310670, 3030858.53956615, 119598844.500504, 
                                       19905762.2744638, 1599600, 51348003.7748504, 96629913.9537075
), `High Five Prority 1: Feed Africa` = c(NA, NA, NA, NA, NA, 
                                          NA, 178415.364611089, NA, NA, NA, NA, NA, 22250000, NA, NA, 89721563.7663194, 
                                          447991.649087706, 36000, NA, NA), `High Five Prority 2: Light Up And Power Africa` = c(NA, 
                                                                                                                                 75290000, 27500000, NA, NA, NA, 59471.7882036962, NA, NA, NA, 
                                                                                                                                 NA, NA, NA, NA, NA, NA, 149330.549695902, 12000, 38520632.9893852, 
                                                                                                                                 NA), `High Five Prority 3: Industrialize Africa` = c(NA, NA, 
                                                                                                                                                                                      NA, 2131605.31196044, NA, NA, NA, NA, NA, 11842251.7331135, 11842251.7331135, 
                                                                                                                                                                                      11842251.7331135, NA, NA, 2273712.3327578, NA, NA, NA, NA, 72490558.1048068
                                                                                                                                 ), `High Five Prority 4: Integrate Africa` = c(NA, NA, NA, NA, 
                                                                                                                                                                                NA, NA, NA, NA, 8e+07, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                                                                NA), `High Five Prority 5: Improve Quality Of Life` = c(690221.630165446, 
                                                                                                                                                                                                                                        NA, NA, NA, 7580000, 5920000, 5709291.66755484, 191844.478076439, 
                                                                                                                                                                                                                                        NA, NA, NA, NA, NA, 27990000, NA, NA, 14335732.7708066, 1152000, 
                                                                                                                                                                                                                                        NA, NA), `NUMBER OF PROJECT` = c(1, 1, 1, 1, 0.5, 0.5, 1, 1, 
                                                                                                                                                                                                                                                                         1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1), `Amount in UA Million` = c(0.690221630165446, 
                                                                                                                                                                                                                                                                                                                                             75.29, 27.5, 2.13160531196044, 7.58, 5.92, 5.94717882036962, 
                                                                                                                                                                                                                                                                                                                                             0.191844478076439, 80, 11.8422517331135, 11.8422517331135, 11.8422517331135, 
                                                                                                                                                                                                                                                                                                                                             22.25, 27.99, 2.2737123327578, 89.7215637663194, 14.9330549695902, 
                                                                                                                                                                                                                                                                                                                                             1.2, 38.5206329893852, 72.4905581048068)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                      -20L), class = c("tbl_df", "tbl", "data.frame"))

ui <- dashboardPage(
  dashboardHeader(
    title="Task Managers' Workload Analysis",
    titleWidth = 400
    
  ),
  dashboardSidebar(
    selectInput("var","Select variable",choices = colnames(diamonds),selected = colnames(diamonds)[1])
  ),
  dashboardBody(
    plotOutput("hist")
  )
)

server <- function(input, output,session) {
  output$hist<-renderPlot({
    ggplot(data=diamonds, aes_string(x=input$var)) +
      geom_histogram(fill="steelblue", color="black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

>Solution :

2. Edit: Here it is recommended to replace aes_string with the .data pronoun:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
library(ggplot2)

# `stat_bin()` requires a continuous x aesthetic
choices <- setdiff(colnames(diamonds), c("cut", "color", "clarity"))

ui <- dashboardPage(
  dashboardHeader(title = "Task Managers' Workload Analysis",
                  titleWidth = 400),
  dashboardSidebar(
    selectInput(
      "var",
      "Select variable",
      choices = choices,
      selected = choices[1]
    )
  ),
  dashboardBody(plotOutput("hist"))
)

server <- function(input, output, session) {
  output$hist <- renderPlot({
    ggplot(data = diamonds, aes(x = .data[[input$var]])) +
      geom_histogram(fill = "steelblue", color = "black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

Edit: aes_string() was deprecated in ggplot2 3.0.0. – now using ?sym instead:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyjs)
library(htmlwidgets)
library(shinyWidgets)
library(ggplot2)

# `stat_bin()` requires a continuous x aesthetic
choices <- setdiff(colnames(diamonds), c("cut", "color", "clarity"))

ui <- dashboardPage(
  dashboardHeader(title = "Task Managers' Workload Analysis",
                  titleWidth = 400),
  dashboardSidebar(
    selectInput(
      "var",
      "Select variable",
      choices = choices,
      selected = choices[1]
    )
  ),
  dashboardBody(plotOutput("hist"))
)

server <- function(input, output, session) {
  output$hist <- renderPlot({
    ggplot(data = diamonds, aes(x = !!sym(input$var))) +
      geom_histogram(fill = "steelblue", color = "black") +
      ggtitle("Histogram of Price Values")
  })
}

shinyApp(ui, server)

Leave a Reply