Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Shiny example: dynamic input fields

View server.r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
data_sets <- c("mtcars", "morley", "rock")
 
shinyServer(function(input, output) {
 
# Drop-down selection box for which data set
output$choose_dataset <- renderUI({
selectInput("dataset", "Data set", as.list(data_sets))
})
 
# Check boxes
output$choose_columns <- renderUI({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
 
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
 
# Create the checkboxes and select them all by default
checkboxGroupInput("columns", "Choose columns",
choices = colnames,
selected = colnames)
})
 
 
# Output the data
output$data_table <- renderTable({
# If missing input, return to avoid error later in function
if(is.null(input$dataset))
return()
 
# Get the data set
dat <- get(input$dataset)
 
# Make sure columns are correct for data set (when data set changes, the
# columns will initially be for the previous data set)
if (is.null(input$columns) || !(input$columns %in% names(dat)))
return()
 
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
 
# Return first 20 rows
head(dat, 20)
})
})
View server.r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
shinyUI(pageWithSidebar(
 
headerPanel(""),
 
sidebarPanel(
uiOutput("choose_dataset"),
 
uiOutput("choose_columns"),
br(),
a(href = "https://gist.github.com/4211337", "Source code")
),
 
 
mainPanel(
tableOutput("data_table")
)
))

Thanks, great code btw! Is this possible to extend so that it can be used with GoogleVis Charts by any chance?

Very usefull example, lots of thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.