How to observe dynamically rendered ui

浪尽此生 提交于 2019-12-04 16:58:09

Ok, so after long puzzling I have managed to find an answer that works:

server.R

shinyServer = function(input, output, session) {

  ################# start functionality HOME TAB #############################  

  values <- reactiveValues()

  dynamicvalues <- reactiveValues()

  observeEvent (input$myNr, {
    values$nrofelements <- paste0(input$myNr) })



#### RENDER DYNAMIC UI 
  observeEvent(values$nrofelements, {
    if (isolate(values$nrofelements>0)) { 
      output$actionbuttons <- renderUI({
        lapply(1:values$nrofelements, function(ab) {
          if (!is.null(dynamicvalues[[paste0("button", ab)]])) { if(dynamicvalues[[paste0("button", ab)]] == 0 ) {
            div(br(), actionButton(inputId = paste0("ind", ab), label = paste("highlight", ab)), br(), br()) } 
            else { div(br(), actionButton(inputId = paste0("ind", ab), label = paste("unhighlight", ab)), br(), br())} }
          else {
            div(br(), actionButton(inputId = paste0("ind", ab), label = paste("highlight", ab)), br(), br()) } 
            })
      })
      output$allbutton <- renderUI({ div( br(), br(), actionButton(inputId = "All", label = "All"), br(), br())
        })
      output$buttons <- renderUI({
        lapply(1:values$nrofelements, function(bsb) {
          div(br(),bsButton(inputId = paste0("indr", bsb), label = paste("Yes", bsb), 
                            block = FALSE, style = "succes", onclick = paste0("Shiny.onInputChange('rnd',", bsb,")")), br(), br() )
          })
      })

      output$multicheckbox <- renderUI ({ 
            div( br(), checkboxInput(inputId = "multiselect", label ="allow multiselect", value = FALSE, width = NULL), br())   })

    }  
  })


#### OBSERVE DYNAMIC UI

observeEvent( values$nrofelements, {
  if (values$nrofelements> 0) {

    isolate(lapply(1:values$nrofelements,  function(su) {
      dynamicvalues[[paste0("button", su)]] <- 0 }))


    dynamiclistB <- reactiveValuesToList(dynamicvalues)

    values$dynamiclistB2 <- as.character(unlist(dynamiclistB, use.names = FALSE))


  isolate(lapply(1:values$nrofelements,  function(ob) {
     observeEvent(input[[paste0("ind", ob)]], {

       if (input$multiselect == FALSE) { 
            for (clicked in 1:values$nrofelements) { if ( ob != clicked) { dynamicvalues[[paste0("button", clicked)]] <- 0} }}

        if    (is.null(dynamicvalues[[paste0("button", ob)]]))  {dynamicvalues[[paste0("button", ob)]] <- 1}  
        else    {  if (dynamicvalues[[paste0("button", ob)]] == 1) {dynamicvalues[[paste0("button", ob)]] <- 0} 
                  else { dynamicvalues[[paste0("button", ob)]] <- 1} 
                }

      dynamiclist <- reactiveValuesToList(dynamicvalues)
      values$dynamiclist2 <- as.character(unlist(dynamiclist, use.names = FALSE))
      print(paste0("dl = ", toString(values$dynamiclist2)))

      print(paste("ob =", ob ))
      values$button_nr_clicked <- ob

      myVector <- vector(mode="character", length=values$nrofelements)
      myVector <- replace(myVector, myVector == "", "GREY")
      myVector[values$button_nr_clicked]="RED"
      print(paste("pallete = ", toString(myVector)))
      print( "-----------next click event prints the below this line--------------------------------------------------------------")
      }) }) ) }})



observeEvent(input$All,{
  myVector <- NULL
  myVector <- vector(mode="character", length=values$nrofelements)
  myVector <- replace(myVector, myVector == "", "RED")

  print(paste("pallete = ", toString(myVector)))
  print( "-----------next click event prints the below this line--------------------------------------------------------------")

})

}

and the UI.R

library(shiny)
library(shinydashboard)
library(shinyBS)


ui <- dashboardPage(
  dashboardHeader(title = "FLOW C.A.R.S."),
  dashboardSidebar(
    sidebarMenu(id = "tabs", menuItem("Home", tabName = "Home", icon = icon("book"))
    )
  ),

  dashboardBody(



    tabItems(




      ### HOME ###_________
      tabItem(tabName = "Home",  

              h5("Enter desired nr of elements here"),
              textInput(inputId ="myNr", label = NULL , placeholder = "NULL"),

              fluidRow(
                column(3,
                       uiOutput("actionbuttons"),
                uiOutput("allbutton")),

                column(1,
                       uiOutput("buttons")
                )
              ),
              uiOutput("multicheckbox")
              )
    )
  )
)

As you can see I build in some extra features like the "All" button, which I will use in reality to run a pallete with a rainbow of colors (each group in the plotly 3D scatter it's own color, and the "multi select check box so the user can hightlight either 1 group, or multiple.

what we have now is:

var 'ob' that reports the last clicked button var 'dl' that gives a list of 0's and 1's to see who's highlighted / clicked var 'pallete that converts the clicks into "RED" or "GREY" status

Next I will work on building text input fields to add a name to the groups, possibly a color picker so the user can custom compose his/her own pallete and a modification of the bsButtons into YES/NO buttons by conditionaly rendering the Label as YES or NO as i did with the highlight / unhighlight buttons to allow the user to select which groups to "keep" and which to "remove" for the next clustering phase.

Still wondering whether it's possible to achieve the same with the observer structure BigDataScientist started with.............

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!