SHARE
TWEET

Untitled

a guest Jun 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # UTILS -------------------------------------------------------------------
  2.  
  3. #' Generate input IDs for Shiny for dynamically generated elements
  4. #'
  5. #' @param prefix a meaningful text for the element e.g. button, table, slider etc.
  6. #' @param id increase iteration from e.g. a loop.
  7. #' @noRd
  8. shinyInputId <- function(prefix, id) {
  9.   paste(prefix, id, sep = "_")
  10. }
  11.  
  12. # DATA --------------------------------------------------------------------
  13.  
  14. mtcars <- datasets::mtcars
  15.  
  16. # APP ---------------------------------------------------------------------
  17.  
  18. ui <- fluidPage(
  19.   h1("Cars"),
  20.   br(),
  21.   lapply(seq(nrow(mtcars)), function(i) {
  22.     uiOutput(shinyInputId("button", i))
  23.   })
  24. )
  25.  
  26. server <- function(input, output, session) {
  27.  
  28.   lapply(seq(nrow(mtcars)), function(i) {
  29.  
  30.     Name <- rownames(mtcars)[i]
  31.     gen.input <- shinyInputId("button", i)
  32.  
  33.     output[[gen.input]] <- renderUI({
  34.       actionButton(
  35.         inputId = gen.input,
  36.         label = Name
  37.       )
  38.     })
  39.  
  40.     observeEvent(input[[gen.input]], {
  41.       print(paste("You clicked", Name))
  42.     })
  43.  
  44.   })
  45.  
  46. }
  47.  
  48. shinyApp(ui, server)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top