I tried creating a Shiny app where you can choose the x-Axis of a ggplot per 'selectizeInput'.
I know about the Gallery Example, where this was solved by pre-selecting the desired column. Because in my case the data structure is a bit complex I would prefer, when it is possible to change the x = attribute in aes() dynamically.
For better understanding I added a minimal working example. Unfortunately ggplot uses the input as value, instead to use the corresponding column.
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Select x Axis"),
sidebarLayout(
sidebarPanel(
selectizeInput("xaxis",
label = "x-Axis",
choices = c("carat", "depth", "table"))
),
mainPanel(
plotOutput("Plot")
)
)
))
server <- shinyServer(function(input, output) {
output$Plot <- renderPlot({
p <- ggplot(diamonds, aes(x = input$xaxis, y = price))
p <-p + geom_point()
print(p)
})
})
# Run the application
shinyApp(ui = ui, server = server)
aes_oraes_string, see?aes_. It would becomeggplot(diamonds, aes_string(x = input$xaxis, y = 'price')).aes_(). If you post it as an answere I will mark it as correct. Thank you.