Skip to content

Instantly share code, notes, and snippets.

@jcheng5
Last active December 10, 2015 13:18
Show Gist options
  • Save jcheng5/4439972 to your computer and use it in GitHub Desktop.
Save jcheng5/4439972 to your computer and use it in GitHub Desktop.
# Some pre-computed stuff in R. In this example, just assign values to a var
# data_sets <- c("mtcars", "morley", "rock")
data_sets <- c("mymtcars","mtcars", "morley", "rock")
mymtcars <- mtcars[,1:4]
shinyServer(function(input, output) {
mymtcars <- mymtcars
extra2 <- function() {
dat <- mymtcars
dat$test <- 1:32
mymtcars <<- dat
}
extra1 <- function() {
extra2()
}
# 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(is.null(input$dataset))
return()
# Get the data set with the appropriate name
dat <- get(input$dataset)
colnames <- names(dat)
print(colnames)
extra1()
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() {
if(is.null(input$dataset) || is.null(input$columns))
return()
# Get the data set
dat <- get(input$dataset)
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
# Return first 20 rows
head(dat, 20)
})
})
# Define UI for miles per gallon application
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