R Shiny how to refer to inputs that are part of dynamic number of inputs

Viewed 79

So, I need to make a shiny app that takes a dynamic number of inputs to eventually do dimension reduction, but I'm stuck trying to figure out how to refer to what's in my inputs when I have a dynamic number of them. I'm using the iris dataset and the inputs are the variables. Part of what I need to do is plot 2 of them with a k means, but I'm just trying to 1st make a basic plot. What I have so far is

library(shiny)
library(ggplot2)

ui <- shinyUI(fluidPage(
  
  titlePanel("Old Faithful Geyser Data"),
  
  fluidRow(
    column(2,
           textInput(inputId = "number", label = "number of selectInput",value = 2)
    ),
    column(8,
           plotOutput("distPlot")),
    column(2,
           uiOutput(outputId = "putselect"))
  )
))

server <- shinyServer(function(input, output) {
  
  
  
  output$putselect = renderUI(
    if(input$number != 0 ){
      lapply(1:(input$number), function(i){
        selectInput(inputId = paste0("var",i), label = paste0("input ",i), choices   = names(iris))
      })
    }
  )
  
  output$distPlot <- renderPlot({
    ggplot(iris, aes(x = input$var1, y = input$var2, color = Species)) +
      geom_point()
    
  })
})

shinyApp(ui = ui, server = server)

In my output$distplot what goes in the ggplot x and y? The way I have it now it shows up and the labels on the graph change, but there are no points on the graph. I'm new to using Shiny so any help would be appreciated.

1 Answers

instead of aes use aes_string like:

ggplot(iris, aes_string(x = input$var1,
                        y = input$var2,
                        color = "Species"
                        )
       )

note to quote the variables supplied as a string (Species in this case)

see: Shiny: passing input$var to aes() in ggplot2

Related