Let's assume I have a minimum working example like
library(shiny)
library(shinyTree)
ui <- fluidPage(
shinyTree("tree", contextmenu = TRUE, search = TRUE, unique = TRUE, sort = TRUE)
)
server <- function(input, output, session) {
output$tree <- renderTree({
list(
root1 = "",
root2 = list(
SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
SubListB = list(leafA = "", leafB = "")
),
root3 = list(
SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
SubListB = list(leafA = "", leafB = "")
)
)
})
}
shinyApp(ui, server)
which generates a shinyTree (jstree) like
Is it possible to run a function if I click on one of the small triangles on the left when I expand the tree (not necessarily selecting anything). I was thinking about using shinyjs
package with onclick event, but did not really manage all that much
You can use the
jstree
events to trigger a shiny observer to execute a function. In the example below, the JS code will update the value ofinput$expanded_node
with the name of the expanded node, then the associated observer will be triggered.