减少从列表创建的data.tree

时间:2018-06-21 16:57:34

标签: r shiny shinytree

我正在开发一个可绘制数据树的闪亮应用程序。我正在寻找合并ShinyTree应用程序以允许快速比较绘制的节点的方法。问题是ShinyTree应用程序返回子节点图列表的冗余列表。

enter image description here

列表的实际列表如下。我只想保留最长的分支。我也想删除id节点(整数节点),我为为什么它甚至基于列表而感到困惑。我尝试了许多不同的方法来处理此列表,但这确实是一个艰难的过程。列表概念很难理解。

我创建data.tree并通过以下方式进行绘制:

dataTree.a <- FromListSimple(checkList)
plot(dataTree.a)

> checkList
[[1]]
[[1]]$Asia
[[1]]$Asia$China
[[1]]$Asia$China$Beijing
[[1]]$Asia$China$Beijing$Round
[[1]]$Asia$China$Beijing$Round$`20383994`
[1] 0


[[2]]
[[2]]$Asia
[[2]]$Asia$China
[[2]]$Asia$China$Beijing
[[2]]$Asia$China$Beijing$Round
[1] 0


[[3]]
[[3]]$Asia
[[3]]$Asia$China
[[3]]$Asia$China$Beijing
[1] 0


[[4]]
[[4]]$Asia
[[4]]$Asia$China
[[4]]$Asia$China$Shanghai
[[4]]$Asia$China$Shanghai$Round
[[4]]$Asia$China$Shanghai$Round$`23740778`
[1] 0


[[5]]
[[5]]$Asia
[[5]]$Asia$China
[[5]]$Asia$China$Shanghai
[[5]]$Asia$China$Shanghai$Round
[1] 0


[[6]]
[[6]]$Asia
[[6]]$Asia$China
[[6]]$Asia$China$Shanghai
[1] 0


[[7]]
[[7]]$Asia
[[7]]$Asia$China
[1] 0


[[8]]
[[8]]$Asia
[[8]]$Asia$India
[[8]]$Asia$India$Delhi
[[8]]$Asia$India$Delhi$Round
[[8]]$Asia$India$Delhi$Round$`25703168`
[1] 0


[[9]]
[[9]]$Asia
[[9]]$Asia$India
[[9]]$Asia$India$Delhi
[[9]]$Asia$India$Delhi$Round
[1] 0


[[10]]
[[10]]$Asia
[[10]]$Asia$India
[[10]]$Asia$India$Delhi
[1] 0


[[11]]
[[11]]$Asia
[[11]]$Asia$India
[1] 0


[[12]]
[[12]]$Asia
[[12]]$Asia$Japan
[[12]]$Asia$Japan$Tokyo
[[12]]$Asia$Japan$Tokyo$Round
[[12]]$Asia$Japan$Tokyo$Round$`38001000`
[1] 0


[[13]]
[[13]]$Asia
[[13]]$Asia$Japan
[[13]]$Asia$Japan$Tokyo
[[13]]$Asia$Japan$Tokyo$Round
[1] 0


[[14]]
[[14]]$Asia
[[14]]$Asia$Japan
[[14]]$Asia$Japan$Tokyo
[1] 0


[[15]]
[[15]]$Asia
[[15]]$Asia$Japan
[1] 0


[[16]]
[[16]]$Asia
[1] 0

感谢进阶... 约翰

1 个答案:

答案 0 :(得分:0)

好吧,我确实凑齐了一个可怜的黑客程序,以使这项工作在这里完成,这是我对“ checkList”列表所做的工作

checkList <- get_selected(tree, format = "slices")

  # Convert and collapse shinyTree slices to data.tree
  #   This is a bit of a cluge to work the graphic with
  # shinyTree an alternate one liner is in works
  #   This transform works by finding the longest branches
  # and only plotting them since the other branches are
  # subsets due to the slices.

  # Extract the checkList name (as characters) from the checkList
  tmp <- names(unlist(checkList)) 

  # Determine the length of the individual checkList Names
  lens <- lapply(tmp, function(x) length(strsplit(x, ".", fixed=TRUE)[[1]]))

  # Find the elements with the highest length returns a list of high vals
  lens.max <- which(lens == max(sapply(lens, max)))

  # Replace all '.' with '\' prepping for DataFrameTable Converions
  tmp <- relist(str_replace_all(tmp, "\\.", "/"), skeleton=tmp)

  # Add a root node to work with multiple branches
  tmp <- unlist(lapply(tmp, function(x) paste0("Root/", x)))

  # Create a list of only the longest branches
  longBranches <- as.list(tmp[lens.max])

  # Convert the list into a data.frame for convert
  longBranches.df <- data.frame(pathString = do.call(rbind, longBranches))

  # Publish the data.frame for use 
  vals$selDF <- longBranches.df

  #save(checkList, file = "chkLists.RData") # Save for troubleshooting

  print(vals$selDF)ode here

新的清单如下:

[1] "Root/Europe/France/Paris/Round/10843285"          "Root/Europe/France/Paris/Round"                  
 [3] "Root/Europe/France/Paris"                         "Root/Europe/France"                              
 [5] "Root/Europe/Germany/Berlin/Diamond/3563194"       "Root/Europe/Germany/Berlin/Diamond"              
 [7] "Root/Europe/Germany/Berlin/Round/3563194"         "Root/Europe/Germany/Berlin/Round"                
 [9] "Root/Europe/Germany/Berlin"                       "Root/Europe/Germany"                             
[11] "Root/Europe/Italy/Rome/Round/3717956"             "Root/Europe/Italy/Rome/Round"                    
[13] "Root/Europe/Italy/Rome"                           "Root/Europe/Italy"                               
[15] "Root/Europe/United Kingdom/London/Round/10313307" "Root/Europe/United Kingdom/London/Round"         
[17] "Root/Europe/United Kingdom/London"                "Root/Europe/United Kingdom"                      
[19] "Root/Europe"                                     

它有效:)...但是我认为可以用两个衬垫来完成。...我将在一周左右的时间内再次进行处理。任何其他想法将不胜感激。