Search code examples
rshinyshinytree

shinyTree: set variable to value if checkbox is checked


Follow-up to shinyTree: view without selecting.

library(shiny)
library(shinyTree)
server <- shinyServer(function(input, output, session) {  
  output$tree <- renderTree({ 
    sss=list(  'I lorem impsum'= list( 
      'I.1 lorem impsum'   =  structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),  
      'I.2 lorem impsum'   =  structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
    attr(sss[[1]],"stopened")=TRUE 
    sss
  })
})
ui <- shinyUI(
  shiny::fluidPage(
    h4('Shiny hierarchical checkbox')
    ,shinyTree("tree", checkbox = TRUE)
  )
)
shinyApp(ui, server)

enter image description here

I would like to set a variable such that if I.1.2. lorem impsum is selected, it has a value of 4, for example.

All I know is that I have to use reactive(). As you can see here, I've learned how to do this with checkboxGroupInputs, but it is unclear to me if this can even be done within a shinyTree. There is no documentation on this that I can find online.

How can this be done?

I have also seen the functions here but am not sure how to use them.


Solution

  • As a side note, I'm really shocked how sparse the documentation is for this package.

    The function get_selected() returns a vector, as it can be seen in the GitHub code. I am going to use format = "slices".

    Consider the following code:

    library(shiny)
    library(shinyTree)
    ui <- shinyUI(
      shiny::fluidPage(
        h4('Shiny hierarchical checkbox'),
        shinyTree("tree", checkbox = TRUE),
        # table of weights
        fluidRow(column("",
                        tableOutput("Table"), width = 12,
                        align = "center"))
      )
    )
    server <- shinyServer(function(input, output, session) {  
      output$tree <- renderTree({ 
        sss=list(  'I lorem impsum'= list( 
          'I.1 lorem impsum'   =  structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),  
          'I.2 lorem impsum'   =  structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
        attr(sss[[1]],"stopened")=TRUE 
        sss
      })
      output$Table <- renderPrint({
    
        names(as.data.frame(get_selected(input$tree, format = "slices")))
    
      })
    })
    shinyApp(ui, server)
    

    Upon selecting I.1.2. lorem impsum, the following is returned:

    enter image description here

    This is a vector of length 1 with the column name. Notice that dots are being used instead of spaces.

    Thus, if we want to set a variable x equal to 4 when this is selected, we should see if I.1.2.lorem.impsum is in the names above, and then perform the assignment.

    library(shiny)
    library(shinyTree)
    ui <- shinyUI(
      shiny::fluidPage(
        h4('Shiny hierarchical checkbox'),
        shinyTree("tree", checkbox = TRUE),
        fluidRow(column("",
                        tableOutput("Table"), width = 12,
                        align = "center")),
        fluidRow(column("",
                        tableOutput("Table2"), width = 12,
                        align = "center"))
      )
    )
    server <- shinyServer(function(input, output, session) {  
      output$tree <- renderTree({ 
        sss=list(  'I lorem impsum'= list( 
          'I.1 lorem impsum'   =  structure(list('I.1.1 lorem impsum'='1', 'I.1.2 lorem impsum'='2'),stopened=TRUE),  
          'I.2 lorem impsum'   =  structure(list('I.2.1 lorem impsum'='3'), stopened=TRUE)))
        attr(sss[[1]],"stopened")=TRUE 
        sss
      })
    
      x <- reactive({
        if('I.1.2.lorem.impsum' %in% names(
          as.data.frame(
            get_selected(
              input$tree, format = "slices")))){
    
          x <- 4
    
        }
      })
    
    
      output$Table <- renderPrint({
    
        names(as.data.frame(get_selected(input$tree, format = "slices")))
    
      })
    
      output$Table2 <- renderTable({
    
        as.data.frame(x())
    
      })
    })
    shinyApp(ui, server)
    

    giving

    enter image description here

    as desired.