Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Forked from wch/server.r
Last active September 10, 2017 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramnathv/4728438 to your computer and use it in GitHub Desktop.
Save ramnathv/4728438 to your computer and use it in GitHub Desktop.
data_sets <- c("mtcars", "morley", "rock")
shinyServer(function(input, output) {
# Drop-down selection box for which data set
output$choose_dataset <- reactiveUI(function() {
selectInput("dataset", "Data set", as.list(data_sets))
})
# Check boxes
output$choose_columns <- reactiveUI(function() {
# 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 <- reactiveTable(function() {
options(shiny.table.class = "table table-striped");
# 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)
})
})
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
uiOutput("choose_dataset"),
uiOutput("choose_columns")
),
mainPanel(
tableOutput("data_table")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment