【问题标题】:How to change node shapes and labels of a data.tree in a shiny app如何在闪亮的应用程序中更改 data.tree 的节点形状和标签
【发布时间】:2021-01-31 03:51:17
【问题描述】:

第一次发帖,希望我记得包含所有内容并使用正确的术语!

不久前,我使用 data.tree 创建了一个图表,显示了一群动物之间的关系。除了动物名称之外,该图表还包括信息,例如它在牛群中的排名。这是一个输出示例。

我现在正试图将它变成一个闪亮的应用程序,这样你就可以从下拉列表中选择一种动物来显示它的家庭。我已经成功地完成了这项工作,但是,这棵树缺少有关动物的样式和其他信息。这是一个与前一个进行比较的示例,没有任何样式。

这是原始 R 脚本中产生所需格式的代码。

library(data.tree)
library(dplyr)

#data to generate the tree
TreeInfo <- tribble(~Animal, ~pathString, ~BW, ~Current, ~Sex,
"CLQG-04-7","CLQG-04-7",                                               148,"No", "F",
"JTGD-08-106","CLQG-04-7/JTGD-08-106",                                 166,"Yes","F",
"JTGD-10-73", "CLQG-04-7/JTGD-10-73",                                  147,"No", "F",
"DLCQ-13-150","CLQG-04-7/JTGD-10-73/DLCQ-13-150",                      211,"Yes","F",
"DLCQ-13-150","CLQG-04-7/JTGD-10-73/DLCQ-13-150",                      211,"Yes","F",
"DLCQ-14-48", "CLQG-04-7/JTGD-10-73/DLCQ-14-48",                       167,"No", "F",
"DLCQ-14-48", "CLQG-04-7/JTGD-10-73/DLCQ-14-48",                       167,"No", "F",
"DLCQ-15-168","CLQG-04-7/JTGD-08-106/DLCQ-15-168",                     134,"Yes","F",
"DLCQ-15-168","CLQG-04-7/JTGD-08-106/DLCQ-15-168",                     134,"Yes","F",
"DLCQ-15-153","CLQG-04-7/JTGD-10-73/DLCQ-15-153",                      148,"Yes","F",
"DLCQ-15-153","CLQG-04-7/JTGD-10-73/DLCQ-15-153",                      148,"Yes","F",
"DLCQ-17-117","CLQG-04-7/JTGD-10-73/DLCQ-14-48/DLCQ-17-117",           216,"No", "F",
"DLCQ-17-94", "CLQG-04-7/JTGD-10-73/DLCQ-13-150/DLCQ-17-94",           215,"No", "F",
"DLCQ-18-126","CLQG-04-7/JTGD-10-73/DLCQ-15-153/DLCQ-18-126",          194,"Yes","F",
"DLCQ-18-126","CLQG-04-7/JTGD-10-73/DLCQ-15-153/DLCQ-18-126",          194,"Yes","F",
"DLCQ-19-170","CLQG-04-7/JTGD-08-106/DLCQ-19-170",                     213,"Yes","F",
"DLCQ-19-170","CLQG-04-7/JTGD-08-106/DLCQ-19-170",                     213,"Yes","F",
"DLCQ-19-62", "CLQG-04-7/JTGD-10-73/DLCQ-13-150/DLCQ-17-94/DLCQ-19-62",246,"Yes","F")

TreeInfo2 <- as.Node(TreeInfo)

#Formatting the tree
GetNodeShape <- function(TreeInfo2) {switch(TreeInfo2$Sex, `F` = "box", `M` = "oval")}
GetNodeLabel <- function(TreeInfo2) {switch(TreeInfo2$Current, No = paste0("*",TreeInfo2$Animal,"\n BW " ,TreeInfo2$BW), Yes = paste0(TreeInfo2$Animal,"\n BW ",TreeInfo2$BW) )}
TreeInfo2$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "brown4",  inherit = FALSE), 
             filterFun = function(TreeInfo2) is.null(TreeInfo2$BW) == FALSE && TreeInfo2$BW >= 200)
TreeInfo2$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
             filterFun = function(TreeInfo2)  is.null(TreeInfo2$BW) == FALSE &&  TreeInfo2$BW < 200)
TreeInfo2$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = TreeInfo2$Animal, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
             filterFun = function(TreeInfo2)  is.null(TreeInfo2$BW) == TRUE)
SetGraphStyle(TreeInfo2, rankdir = "LR")

plot(TreeInfo2)

在 Shiny 应用程序中,TreeInfo 和 TreeInfo2 是反应性的(由从下拉列表中选择的动物确定,大约 3000 只动物)所以我知道我需要将它们更改为 TreeInfo2()。但是,它不会让我在代码的函数部分输入这个,即 function(TreeInfo2()) 给出一个错误,说它期望 RPAREN。除此之外,我已经尝试了下面代码的许多不同组合,但我不确定在哪里,但 reactive({}) 位 - 我只知道它需要它们,否则它不会运行。

  TreeInfo2 <- reactive({as.Node(TreeInfo())})
  
  reactive({GetNodeShape <- function(TreeInfo2) {switch(TreeInfo2()$Sex, `F` = "box", `M` = "oval")}})
  reactive({GetNodeLabel <- function(TreeInfo2) {switch(TreeInfo2()$Current, No = paste0("*",TreeInfo2()$Animal,"\n BW " ,TreeInfo2()$BW), Yes = paste0(TreeInfo2()$Animal,"\n BW ",TreeInfo2()$BW) )}})
  reactive({TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2(), fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "brown4",  inherit = FALSE), 
               filterFun = function(TreeInfo2) is.null(TreeInfo2()$BW) == FALSE && TreeInfo2()$BW >= 200)})
  reactive({TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2(), fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
               filterFun = function(TreeInfo2)  is.null(TreeInfo2()$BW) == FALSE &&  TreeInfo2()$BW < 200)})
  reactive({TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2(), fontname = 'helvetica', label = TreeInfo2()$Animal, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
               filterFun = function(TreeInfo2)  is.null(TreeInfo2()$BW) == TRUE)})
  reactive({SetGraphStyle(TreeInfo2(), rankdir = "LR")})

即使将其简化为从垂直变为水平的最后一个 SetGraphStyle 步骤似乎也没有效果,这让我想知道我是否在正确的位置找到了这个块,也许是“样式” ' 代码应该放在其他地方,例如 Shiny 服务器的输出部分,目前是这样的:

output$Tree <- renderGrViz({grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(TreeInfo2())))})}
  

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: r shiny diagrammer data.tree


    【解决方案1】:

    如果以后对任何人有帮助,这里有一个可行的解决方案。将代码的格式化部分移动到 output$Tree 步骤。这修复了格式,但随后破坏了每个框中出现的名称。通过一步封装反应部分来解决此问题。

     TreeInfo2 <- reactive({
    
    ###code here to select the animal to display
    
        TreeInfo2 <- as.Node(TreeInfo)    
        return(TreeInfo2)})
    
    
     output$Tree <- renderGrViz({
                
        GetNodeShape <- function(TreeInfo2) {switch(TreeInfo2$Sex, `F` = "box", `M` = "oval")}
        GetNodeLabel <- function(TreeInfo2) {switch(TreeInfo2$Current, No = paste0("*",TreeInfo2$Animal,"\n BW " ,TreeInfo2$BW), Yes = paste0(TreeInfo2$Animal,"\n BW ",TreeInfo2$BW) )}
        TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "brown4",  inherit = FALSE), 
                       filterFun = function(TreeInfo2) is.null(TreeInfo2$BW) == FALSE && TreeInfo2$BW >= 200)
        TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = GetNodeLabel, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
                       filterFun = function(TreeInfo2)  is.null(TreeInfo2$BW) == FALSE &&  TreeInfo2$BW < 200)
        TreeInfo2()$Do(function(TreeInfo2) SetNodeStyle(TreeInfo2, fontname = 'helvetica', label = TreeInfo2$Animal, shape = GetNodeShape, fontcolor = "blue",  inherit = FALSE), 
                       filterFun = function(TreeInfo2)  is.null(TreeInfo2$BW) == TRUE)
        SetGraphStyle(TreeInfo2(), rankdir = "HR")
        
        
        grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(TreeInfo2())))})}
    

    【讨论】:

    • 你能公开一个 gist 或 github repo 来演示整个代码和功能吗?另外,将此解决方案标记为后代的答案。
    猜你喜欢
    • 2020-02-14
    • 2020-01-08
    • 2021-10-30
    • 2017-06-04
    • 2016-07-27
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多