Create a gist now

Instantly share code, notes, and snippets.

@wch /server.r
Last active Apr 3, 2018

What would you like to do?
Shiny example: dynamic input fields
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)
})
})
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
uiOutput("choose_dataset"),
uiOutput("choose_columns"),
br(),
a(href = "https://gist.github.com/4211337", "Source code")
),
mainPanel(
tableOutput("data_table")
)
))
@JRPTrad

This comment has been minimized.

Show comment Hide comment
@JRPTrad

JRPTrad Nov 29, 2014

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

JRPTrad commented Nov 29, 2014

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

@revuel

This comment has been minimized.

Show comment Hide comment
@revuel

revuel Mar 11, 2015

Very usefull example, lots of thanks.

revuel commented Mar 11, 2015

Very usefull example, lots of thanks.

@yizhexu

This comment has been minimized.

Show comment Hide comment
@yizhexu

yizhexu Jul 1, 2015

Thank you! This is a really useful example!

yizhexu commented Jul 1, 2015

Thank you! This is a really useful example!

@vluong

This comment has been minimized.

Show comment Hide comment
@vluong

vluong Nov 11, 2015

How can I see an example of this?

vluong commented Nov 11, 2015

How can I see an example of this?

@LilianaPacheco

This comment has been minimized.

Show comment Hide comment
@LilianaPacheco

LilianaPacheco Dec 22, 2015

Thank you!!!

Thank you!!!

@irenge

This comment has been minimized.

Show comment Hide comment
@irenge

irenge Apr 10, 2016

cool code

irenge commented Apr 10, 2016

cool code

@jzhang34

This comment has been minimized.

Show comment Hide comment
@jzhang34

jzhang34 Jul 21, 2016

Thank you!

Thank you!

@dihfahsih

This comment has been minimized.

Show comment Hide comment
@dihfahsih

dihfahsih Jul 26, 2016

i just needed this for my project

i just needed this for my project

@tigregrr

This comment has been minimized.

Show comment Hide comment
@tigregrr

tigregrr Jul 28, 2016

Thank you! Clean and useful code.

Thank you! Clean and useful code.

@Lorenagzp

This comment has been minimized.

Show comment Hide comment
@Lorenagzp

Lorenagzp Oct 24, 2017

Very useful example, thank you!!

Very useful example, thank you!!

@SmritiSatyan

This comment has been minimized.

Show comment Hide comment
@SmritiSatyan

SmritiSatyan Mar 7, 2018

could you show an example where plots are rendered, rather than tables? it will be great. i have done something similar to this, but when i unselect the first checkbox, i get errors. my code only works when the first checkbox is mandatorily selected and others are checked/unchecked

could you show an example where plots are rendered, rather than tables? it will be great. i have done something similar to this, but when i unselect the first checkbox, i get errors. my code only works when the first checkbox is mandatorily selected and others are checked/unchecked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment