Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # UTILS -------------------------------------------------------------------
- #' Generate input IDs for Shiny for dynamically generated elements
- #'
- #' @param prefix a meaningful text for the element e.g. button, table, slider etc.
- #' @param id increase iteration from e.g. a loop.
- #' @noRd
- shinyInputId <- function(prefix, id) {
- paste(prefix, id, sep = "_")
- }
- # DATA --------------------------------------------------------------------
- mtcars <- datasets::mtcars
- # APP ---------------------------------------------------------------------
- ui <- fluidPage(
- h1("Cars"),
- br(),
- lapply(seq(nrow(mtcars)), function(i) {
- uiOutput(shinyInputId("button", i))
- })
- )
- server <- function(input, output, session) {
- lapply(seq(nrow(mtcars)), function(i) {
- Name <- rownames(mtcars)[i]
- gen.input <- shinyInputId("button", i)
- output[[gen.input]] <- renderUI({
- actionButton(
- inputId = gen.input,
- label = Name
- )
- })
- observeEvent(input[[gen.input]], {
- print(paste("You clicked", Name))
- })
- })
- }
- shinyApp(ui, server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement