I'm trying to build a datatable which would react when clicking on a node of a tree. Especially, I would like the corresponding row of the table to be selected (given id
stored in both datatable column and tree). How can I achieve that? Here is a sample code:
library(shiny)
library(DT)
library(jsTreeR)
nodes <- list(
list(
text = "Root",
state = list(opened = TRUE),
children = list(
list(
text = "Alice",
state = list(opened = TRUE),
li_attr = list(id = "1")),
list(
text = "Bob",
state = list(opened = TRUE),
li_attr = list(id = "2"))
)
)
)
dat <- data.frame(
ID = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
Name = c("Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Hank", "Ivy", "Jack"),
Age = c(25, 32, 45, 28, 40, 52, 34, 29, 37, 48)
)
ui <- fluidPage(
br(),
fluidRow(
column(
3,
jstreeOutput("tree")
),
column(
9,
DTOutput("dtable")
)
)
)
server <- function(input, output, session) {
output[["tree"]] <- renderJstree(
jstree(nodes)
)
output[["dtable"]] <- renderDT({
datatable(
dat, selection = "single", rownames = FALSE
)
})
}
shinyApp(ui, server)
You can use
selectRows
to select rows andinput$tree_selected
to get the selected nodes from the tree. With these 2 ingredients you can implement this feature as follows:This approach uses the text which may not be necessarily unique, to circumvent this you could add the
id
asdata
to the tree, astree_selected
also returns thedata
attribute (no need to store theid
on theHTML
though (viali_attr
) since we ae using ashiny
only solution and do not need to fallback toJavaScript
: