In running the below reproducible code, I'm trying to extract specific node elements from a jsTree
(using the jsTreeR
package) into a data frame. Similar to what was done in related post that used sortable
DnD instead of jstree
at How to pull list elements from HTML/CSS and into an R data frame?
Any ideas for extracting specific node elements from a jsTree
for use in a dataframe?
This is so further R operations can be performed on those dragged-in (or better said, copied over) elements.
The image at the bottom better explains.
Reproducible code (I commented out my attempts to resolve this in the below):
library(jsTreeR)
library(shiny)
nodes <- list(
list(
text = "Menu",
state = list(opened = TRUE),
children = list(
list(text = "A", type = "moveable", state = list(disabled = TRUE)),
list(text = "B", type = "moveable", state = list(disabled = TRUE))
)
),
list(text = "Drag here:",
type = "target",
state = list(opened = TRUE)
)
)
checkCallback <- JS(
"function(operation, node, parent, position, more) { console.log(node);",
" if(operation === 'copy_node') {",
" if(parent.id === '#' || node.parent !== 'j1_1' || parent.type !== 'target') {",
" return false;", # prevent moving an item above or below the root
" }", # and moving inside an item except a 'target' item
" }",
" return true;", # allow everything else
"}"
)
dnd <- list(
always_copy = TRUE,
is_draggable = JS(
"function(node) {",
" return node[0].type === 'moveable';",
"}"
)
)
ui <- fluidPage(
tags$head(
tags$script(
HTML(
script <-
'
$(document).ready(function(){
$("#mytree").on("copy_node.jstree", function(e, data){
var instance = data.new_instance;
var node = data.node;
var id = node.id;
var text = node.text;
var index = $("#"+id).index() + 1;
instance.rename_node(node, index + ". " + text);
})
});
'
)
)
),
jstreeOutput("mytree"),
# tableOutput("table1")
)
server <- function(input, output){
output[["mytree"]] <- renderJstree({
jstree(
nodes,
dragAndDrop = TRUE,
dnd = dnd,
checkCallback = checkCallback,
types = list(moveable = list(),
target = list()),
)
})
# draggedElements <- reactive({
# data.frame(data = paste0(seq_along(jstreeOutput("mytree")), ". ", jstreeOutput("mytree")))
# })
# output$table1 <- renderTable({draggedElements()})
}
shinyApp(ui, server)
First, unrelated to this question, I added the option
inside_pos="last"
in the drag-and-drop handler:With this option, you can drop a node on the node "Drag here" and it automatically goes to the last position (see the GIF). Very convenient.
Now, for your question. This is a job for
Shiny.setInputValue
. Modify the script:And here is the Shiny app:
EDIT: deletion
Full app, with icons and the proton theme: