Skip to content

Instantly share code, notes, and snippets.

@mplourde
Created December 12, 2014 15:08
Show Gist options
  • Save mplourde/b507f9e2fef2f2c52b7e to your computer and use it in GitHub Desktop.
Save mplourde/b507f9e2fef2f2c52b7e to your computer and use it in GitHub Desktop.
R shiny dynamic outputs
ui <- fluidPage(
selectInput('plot_type', 'Plot type', choices=c('point', 'line')),
sliderInput('n', 'N plots', value=1, min=1, max=100),
uiOutput('plots')
)
server <- function(input, output, session) {
output$plots <- renderUI({
ptype <- input$plot_type
n <- input$n
output.list <- lapply(paste0(ptype, sequence(n)), function(id) plotOutput(id, height=280, width=250))
do.call(tagList, output.list)
})
observe({
ptype <- input$plot_type
n <- input$n
for (i in sequence(n)) {
local({
my_i <- i
id <- paste0(ptype, i)
output[[id]] <- renderPlot({
if (ptype == 'point') {
plot(1:10, type='p')
} else {
plot(1:10, type='l')
}
})
})
}
})
}
runApp(list(server=server, ui=ui))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment