Home > Mobile >  user chooses theme R shiny ggplot
user chooses theme R shiny ggplot

Time:05-06

I want the user to be able to choose the theme for a ggplot. I am a little shaky on non-standard evaluation, but I am guessing that that is what I am missing. Any advice appreciated.

library(shiny)
library(ggplot2)
themes <- ls("package:ggplot2", pattern = "^theme_")
themes <- themes[themes != "theme_get" &
  themes != "theme_set" &
  themes != "theme_replace" &
  themes != "theme_test" &
  themes != "theme_update"]
themes <- paste0(themes, "()")
# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
        "Choose bin width:",
        min = 5,
        max = 15,
        value = 10
      ),
      selectInput(
        "theme",
        "choose a theme",
        themes,
        selected = "theme_minimal()",
        multiple = FALSE
      )
    ),

    # 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({
    ggplot(faithful, aes(waiting))  
      geom_histogram(binwidth = input$bins)  
      labs(title = input$theme)  
      input$theme
  })
}

# Run the application
shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:6760
#> Warning: Error in ggplot_add: Can't add `input$theme` to a ggplot object.

Created on 2022-05-05 by the enter image description here

  • Related