rcommand-line-interface

in package cli , run function tree return nothing


I want to show the data structure using cli::tree , but failed ( below code return nothing). Anyone can help ? Thanks!

library(cli)
mtest <- data.frame(
  stringsAsFactors = FALSE,
  category = c("A","B"),
  sub_category = I(list(c("A1","A2","A3"),c("B1","B2")))
)


tree(mtest,root = "rcmdcheck")

Solution

  • Two problems here:

    1. The purpose of tree is to print a dependency-tree of sorts based on the node defined in root. Unfortunately, "rcmdcheck" is not defined in mtest's column one. Since it is not defined, tree() prints nothing.

    2. The values in column 2 need to be present in column 1, even when they have no sub-categories of their own in column 2. So if we change to root="A" (or just nothing, it will default to "A" with this mtest), it will print just A, no dependencies.

    Let me walk through the sample data shown in ?tree:

    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)
      ))
    )
    
    cli::tree(data)
    # processx
    # ├─assertthat
    # ├─crayon
    # ├─debugme
    # │ └─crayon
    # └─R6
    

    It starts on "processx", because that is the first value of the first column in the data. I suspect your use of tree(mtest, root="rcmdcheck") is based on its second call. In your case, root= can take on values "A" or "B", since you define those in the first column mtest$category.

    From there, the above example shows "assertthat" and others. All of them happen to have rows where they are found in package:

    intersect(data$dependencies[[1]], data$package)
    # [1] "assertthat" "crayon"     "debugme"    "R6"        
    

    And for each of them, some have dependencies:

    for (dep in intersect(data$dependencies[[1]], data$package)) { print(dep); print(subset(data, package == dep)$dependencies); }
    # [1] "assertthat"
    # [[1]]
    # character(0)
    #
    # [1] "crayon"
    # [[1]]
    # character(0)
    #
    # [1] "debugme"
    # [[1]]
    # [1] "crayon"
    #
    # [1] "R6"
    # [[1]]
    # character(0)
    

    Three are included in package and have no dependencies; one is included in package and includes one dependency. You get where that's going.

    The difference between them and your mtest is that none of your sub_category levels are also defined in category. We can fix that by adding them with no dependencies.

    mtest2 <- data.frame(category = setdiff(unique(unlist(mtest$sub_category)), mtest$category)) |>
      transform(sub_category = I(lapply(category, function(ign) character(0))))
    mtest2 <- rbind(mtest, mtest2)
    mtest2
    #   category sub_category
    # 1        A   A1, A2, A3
    # 2        B       B1, B2
    # 3       A1             
    # 4       A2             
    # 5       A3             
    # 6       B1             
    # 7       B2             
    
    cli::tree(mtest2)
    # A
    # ├─A1
    # ├─A2
    # └─A3
    cli::tree(mtest2, root = "B")
    # B
    # ├─B1
    # └─B2