首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在shinyTree中保存和恢复选择

在shinyTree中保存和恢复选择
EN

Stack Overflow用户
提问于 2019-09-16 16:13:59
回答 1查看 364关注 0票数 1

是否可以保存和恢复shinyTree的选择?

我找到了一个删除selections R Shiny - Updating shinyTree node selections的解决方案

但我需要保存选择并在以后恢复它们,例如,通过actionButton

EN

回答 1

Stack Overflow用户

发布于 2019-09-16 22:41:31

仅使用shinyTree不可能做到这一点。必须直接调用底层jsTree库的一些函数,并将值从JavaScript传递到R,反之亦然。

我举了一个小例子,作为一个起点应该会对你有所帮助。

如果您通过单击按钮保存选择,R将向JavaScript发送一条自定义消息,后者将获得所选的it并通过Shiny.setInputValue将其返回给R。

然后,选定的ID将保存在reactiveValues selectionRV中,但如果需要,您可以将其保存在文件或数据库中。

代码语言:javascript
复制
library(shiny)
library(shinyTree)
library(shinyjs)

js <- HTML("
$(document).on('shiny:connected', function(event) {
  Shiny.addCustomMessageHandler('saveselection', function(e) {
    var selection = $('#tree').jstree().get_selected();
    Shiny.setInputValue('treeselection', selection, {priority: 'event'});
  });
})
")

## ui ####################
ui <- fluidPage(
  useShinyjs(),
  tags$head(tags$script(js)),
  actionButton("deselect", "Deselect all"),
  actionButton("savesele", "Save Selection"),
  actionButton("restoresele", "Restore Selection"),
  shinyTree("tree", dragAndDrop = TRUE,types= #Types is in the same format that jstree expects
              "{
          '#': { 'max_children' : 2, 'max_depth' : 4, 'valid_children' : ['root'] },
          'root' : { 'valid_children' : ['file'] },
          'default' : { 'valid_children' : ['default','file'] },
          'file' : { 'icon' : 'fa fa-file', 'valid_children' : [] }
        }"
  )  
)

## server ####################
server <- function(input, output, session) {
  treeData <- reactive({
    rootstrc <- structure(list(
      SubListA = structure(list(
        leaf1 = structure("",sttype="file",sticon="fa fa-signal"), 
        leaf2 = structure("",sttype="file",sticon="fa fa-signal"),
        leaf3 = structure("",sttype="file",sticon="fa fa-signal")),
        sttype="root",stopened=F,sticon="fa fa-signal"
      ),
      SubListB = structure(list(
        leafA = structure("",sttype="default",sticon="glyphicon glyphicon-leaf"),
        leafB = structure("",sttype="default",sticon="shinyTree/icon.png"),
        leafC = structure("",sttype="default",sticon="fa fa-signal")
      ),stopened=F,sttype="root",sticon="fa fa-signal")
    ),
    sttype="root",stopened=F,sticon="fa fa-signal"
    )

    list(
      root1 = rootstrc,
      root2 = rootstrc,
      root3 = rootstrc,
      root4 = rootstrc
    )
  })
  output$tree <- renderTree({
    treeData()
  })

  selectionRV <- reactiveValues(list = NULL)
  observeEvent(input$deselect, {
    runjs("$('#tree').jstree().deselect_all()")
  })
  observeEvent(input$savesele, {
    session$sendCustomMessage("saveselection", message)
  })
  observeEvent(input$restoresele, {
    req(input$treeselection)
    tmp <- paste0("[", paste(input$treeselection, collapse = ","), "]")
    js <- sprintf("$('#tree').jstree().select_node(%s)", tmp)
    runjs(js)
  })
}

shinyApp(ui, server)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57952813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档