The app below contains a shinyTree
, a reset button (Reset nodes
) and a print output 'Selected nodes'. The print output prints the output of the get_selected
function, which returns a list of selected nodes.
Here is a screenshot of the tree on start-up:
No nodes are selected so get_selected
returns an empty list()
When I make a selection, e.g. node a
, the get_selected
correctly returns that selection:
When I click Reset nodes
, the node selections are cleared in the tree UI but get_selected
does not change from the previous selection:
When the reset button is clicked, an observer updates the tree via updateTree
as follows:
observeEvent(input$reset, {
updateTree(session,"tree", data = tree)
})
I would like get_selected
to return list()
when I hit reset. Am I using updateTree
incorrectly?
Here is the code to reproduce the above:
library(shiny)
library(shinyTree)
tree = structure(list(a=list(a1=1,a2=2) , b="b"), stopened = T)
tree = lapply(tree, function(x) structure(x, stopened = T))
ui <- fluidPage(
tags$head(tags$script('
$("#reset").onlick(function() {
$("#tree").jstree("deselect_all");
}
')),
fluidPage(
sidebarLayout(
sidebarPanel(
actionButton('reset', 'Reset nodes')
),
mainPanel(
shinyTree("tree", ),
hr(),
"Selected nodes:",
verbatimTextOutput("idSelected")#,
)
)
)
)
# server
server <- function(input, output, session) {
output$tree = renderTree({
tree
})
output$idSelected <- renderPrint({
str(get_selected(input$tree, format = "classid"))
})
# An observer is used to trigger a tree update when reset is clicked.
observeEvent(input$reset, {
updateTree(session,"tree", data = tree)
print(get_selected(input$tree, format = "classid"))
})
}
shinyApp(ui, server)
I've tried the following JQuery to no avail:
$("#reset").onlick(function() {
$("#tree").jstree("deselect_all");
}
You could update a reactiveVal
on hitting the reset button instead of directly referring to the data provided by get_selected
:
library(shiny)
library(shinyTree)
tree <- lapply(structure(list(a=list(a1=1,a2=2) , b="b"), stopened = TRUE) , function(x) structure(x, stopened = TRUE))
ui <- fluidPage(
fluidPage(
sidebarLayout(
sidebarPanel(
actionButton('reset', 'Reset nodes')
),
mainPanel(
shinyTree("tree", ),
hr(),
"Selected nodes:",
verbatimTextOutput("idSelected")#,
)
)
)
)
server <- function(input, output, session) {
treeSelection <- reactiveVal(list())
output$tree = renderTree({
tree
})
observeEvent(input$reset, {
updateTree(session, "tree", data = tree)
treeSelection(list())
})
observeEvent(input$tree, {
treeSelection(get_selected(input$tree, format = "classid"))
})
output$idSelected <- renderPrint({
treeSelection()
})
}
shinyApp(ui, server)