0

As I have complained various times on this site (shinyTree: set variable to value if checkbox is checked, shinyTree: view without selecting), there is a serious lack of documentation on shinyTree.

Consider the following code in R:

library(shiny)
library(shinyTree)
ui <- shinyUI(
  shiny::fluidPage(
    h4('Shiny hierarchical checkbox'),
    shinyTree("tree", checkbox = TRUE),
    # slider
    sliderInput("slider_input",
                                          "Slider",
                                          min = 0,
                                          max = 100,
                                          value = 0,
                                          step = 0.1,
                                          width = '100%'),
                              width = 8)
)
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
  })

})
shinyApp(ui, server)

enter image description here

I am interested in having the sliderInput appear if and only if I.1.1 lorem impsum in the tree is selected.

I know that when using checkboxGroupInputs, I can use what is found at conditionalPanel javascript condtions in shiny: is there R %in% operator in javascript?. This is all fine and dandy, but due to the lack of documentation (and the lack of questions asked on this site) about shinyTree, I am uncertain how to pursue this problem. I understand I need to use a conditionalPanel, but I have no idea how to refer to the tree and its nodes in the condition argument of the function. What complicates matters more is that the tree itself is not in the input (since it doesn't have the input$ when declaring it), and I can't reference output$ variables, to my knowledge.

I do know that the condition has to be written in JavaScript, but this is useless if I have no idea how to refer to the tree and its nodes.

Community
  • 1
  • 1
Clarinetist
  • 1,097
  • 18
  • 46
  • You can use shinyjs and show\hide or render your slider on server side with if condition. – Batanichek Aug 30 '16 at 12:17
  • @Batanichek If I wanted to render my slider with the `if` condition, what would I use? I imagine there's not a `renderSlider()` function. – Clarinetist Aug 30 '16 at 12:22
  • @Batanichek I imagine I would have to use something like `output$slider <- reactive({ if(--some tree node with condition--){renderSlider({sliderInput("slider_input", "Slider", min = 0, max = 100, value = 0, step = 0.1, width = '100%'), width = 8)})}})` but, again, it's the problem of referring to the tree node. – Clarinetist Aug 30 '16 at 12:23

1 Answers1

1

Render slider on server side

library(shiny)
library(shinyTree)
ui <- shinyUI(
  shiny::fluidPage(
    h4('Shiny hierarchical checkbox'),
    shinyTree("tree", checkbox = TRUE),
    # slider
    uiOutput("slider_ui"),

    width = 8)
)
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$slider_ui=renderUI({
if('I.1.1 lorem impsum' %in% get_selected(input$tree)){
  sliderInput("slider_input",
              "Slider",
              min = 0,
              max = 100,
              value = 0,
              step = 0.1,
              width = '100%')
}
  })


})
shinyApp(ui, server)

shinyjs variant

library(shiny)
library(shinyTree)
library(shinyjs)
ui <- shinyUI(
  shiny::fluidPage(
    useShinyjs(),
    h4('Shiny hierarchical checkbox'),
    shinyTree("tree", checkbox = TRUE),
    # slider
    sliderInput("slider_input",
                "Slider",
                min = 0,
                max = 100,
                value = 0,
                step = 0.1,
                width = '100%'),

    width = 8)
)
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
  })
observe({
  if('I.1.1 lorem impsum' %in% get_selected(input$tree)){
    show("slider_input")
  }else{
    hide("slider_input")
  }
})

  })



shinyApp(ui, server)
Batanichek
  • 7,761
  • 31
  • 49