Home > Net >  Programmatically use column names in ggplot in Shiny
Programmatically use column names in ggplot in Shiny

Time:10-18

I have a Shiny app part of which makes histograms via ggplot. The user will choose a data column via the UI from which I can extract a name. The data could have many columns.

It works fine with aes_ but that is soft deprecated.

I saw programming with ggplot using aes_ or aes_string with special characters in column names and attempted to implement the tidy evaluation solution, but I am getting an error in that ggplot wants x to be continuous.

Here is an MRE. The original code that works but is deprecated is commented out. My non-working code is below it. Can you tell me what I am doing wrong?


library(shiny)
library(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Columns"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$distPlot <- renderPlot({
      data<-data.frame(rnorm(100))
      names(data)<-"Some random thing"
      
      #old code##################
      # p<-ggplot(data=data,aes_(x=as.name(names(data)[1])))  #simulates user choosing column 1 via the UI
      #             geom_histogram(bins=input$bins)
      ###########################
      
      #my attempt###############
      temp<-names(data)[1] #simulates the user choosing column 1 via the UI
      x<-enquo(temp)
      p<-ggplot(data=data,aes(x=!!x)) 
        geom_histogram(bins=input$bins)
      #########################
      
      p
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

Using the .data pronoun you could pass column names as strings via aes like so:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  titlePanel("Columns"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      )
    ),
    mainPanel(
      plotOutput("distPlot")
    )
  )
)
server <- function(input, output) {
  output$distPlot <- renderPlot({
    data <- data.frame(rnorm(100))
    names(data) <- "Some random thing"

    x <- names(data)[1]
    ggplot(data = data, aes(x = .data[[x]]))  
      geom_histogram(bins = input$bins)
  })
}

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:3887

CodePudding user response:

The shiny parts are irrelevant, here's a much more minimal working example using .data, which is appopriate for column names stored as stings:

data <- data.frame(rnorm(100))
names(data) <- "Some random thing"
input = list(bins = 30)

temp <- names(data)[1]
ggplot(data = data, aes(x = .data[[temp]]))  
  geom_histogram(bins = input$bins)
  • Related