How to display the directory structure by `cli::tree`

Here is the example in the help document of function cli::tree. I works well. And I want to produce the structure in this way.

library(cli)
data <- data.frame(
  stringsAsFactors = FALSE,
  package = c("processx", "backports", "assertthat", "Matrix",
    "magrittr", "rprojroot", "clisymbols", "prettyunits", "withr",
    "desc", "igraph", "R6", "crayon", "debugme", "digest", "irlba",
    "rcmdcheck", "callr", "pkgconfig", "lattice"),
  dependencies = I(list(
    c("assertthat", "crayon", "debugme", "R6"), character(0),
    character(0), "lattice", character(0), "backports", character(0),
    c("magrittr", "assertthat"), character(0),
    c("assertthat", "R6", "crayon", "rprojroot"),
    c("irlba", "magrittr", "Matrix", "pkgconfig"), character(0),
    character(0), "crayon", character(0), "Matrix",
    c("callr", "clisymbols", "crayon", "desc", "digest", "prettyunits",
      "R6", "rprojroot", "withr"),
    c("processx", "R6"), character(0), character(0)
  ))
)
tree(data)
#> processx
#> ├─assertthat
#> ├─crayon
#> ├─debugme
#> │ └─crayon
#> └─R6
tree(data, root = "rcmdcheck")
#> rcmdcheck
#> ├─callr
#> │ ├─processx
#> │ │ ├─assertthat
#> │ │ ├─crayon
#> │ │ ├─debugme
#> │ │ │ └─crayon
#> │ │ └─R6
#> │ └─R6
#> ├─clisymbols
#> ├─crayon
#> ├─desc
#> │ ├─assertthat
#> │ ├─R6
#> │ ├─crayon
#> │ └─rprojroot
#> │   └─backports
#> ├─digest
#> ├─prettyunits
#> │ ├─magrittr
#> │ └─assertthat
#> ├─R6
#> ├─rprojroot
#> │ └─backports
#> └─withr

Created on 2018-11-18 by the reprex package (v0.2.1)

But when I edit it into a simpler example, I always get such an error.

library(cli)
library(tidyverse)
data.frame(
    package = 'processx'
    ,dependencies = c("assertthat", "crayon", "debugme", "R6") %>% list %>% I
) %>% 
    tree(root = "rcmdcheck")
#> Error in `[[.default`(labels, num_root): 下标出界

Created on 2018-11-18 by the reprex package (v0.2.1)

I don't know why, and I find there is less examples of cli::tree online.

cli::tree works with a data.frame that describe a full tree. In you example, your missing some lines for each children.

Also, in your tree call tree(root = "rcmdcheck"), you are calling a root that does not exist in your data.frame. So it won't work.

You must add the minimal for the processx tree.

library(magrittr)
data.frame(
  stringsAsFactors = FALSE,
  package = c('processx','assertthat', 'crayon', 'debugme', 'R6'),
  dependencies = I(list(
    c("assertthat", "crayon", "debugme", "R6"),
    character(0),
    character(0),
    "crayon",
    character(0)
  ))
) %>% 
  cli::tree()
#> processx
#> +-assertthat
#> +-crayon
#> +-debugme
#> | \-crayon
#> \-R6

Created on 2018-11-18 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.