
Hi.
here is a simple example that shows how to select a dataset and subsequently how to populate an input selector with the column names from the selected dataset. The key is to use
renderUI to construct the input selector, which is not in the ui.r script, but lives in the server.r script. To see more examples on how specific elements fit together please see the
Shiny gallery.
###
### ui.r
###
library(shiny)
shinyUI(fluidPage(
fluidRow(
br(),
h1("simple dynamic selector example")
),
fluidRow(
column(2,
selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars")),
uiOutput("ColumnSelector")
),
column(2,
verbatimTextOutput("summary")
)
)
)
)
###
### server.r
###
library(shiny)
library(datasets)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
output$ColumnSelector <- renderUI({
selectInput("SelectedColumn","select a column", choices = colnames(datasetInput()))
})
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset[input$SelectedColumn])
})
})