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)
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 checkboxGroupInput
s, 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.
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)