R Shiny - Resetting shinyTree node selections

心不动则不痛 提交于 2019-12-02 02:13:57

问题


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");
                        }

回答1:


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)


来源:https://stackoverflow.com/questions/56555281/r-shiny-resetting-shinytree-node-selections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!