Skip to contents

Basic Usage

Print the tree for current working directory

tmp <- tempdir()
demo <- file.path(tmp, "printtree-demo")

# Start fresh

if (dir.exists(demo)) unlink(demo, recursive = TRUE, force = TRUE)

dir.create(demo, recursive = TRUE)
dir.create(file.path(demo, "R"))
dir.create(file.path(demo, "data", "raw"), recursive = TRUE)

file.create(file.path(demo, "R", "hello.R"))
#> [1] TRUE
file.create(file.path(demo, "README.md"))
#> [1] TRUE
file.create(file.path(demo, ".Rhistory"))
#> [1] TRUE

Print the tree

print_rtree()
#> vignettes/
#> |-- printtree.Rmd
#> |-- tree-dark.png
#> |-- tree-white.png
#> `-- tree.png

Project root detection

When project = “root”, printtree can walk upward from the provided path to detect a project root using simple markers. By default this includes:

  • .Rproj files (RStudio / Posit projects)

  • DESCRIPTION files (R package roots)

In this example, we mark demo as a package-like root by creating a DESCRIPTION file, then print from a subdirectory:

file.create(file.path(demo, "DESCRIPTION"))
#> [1] TRUE
subdir <- file.path(demo, "data", "raw")
print_rtree(subdir, project = "root")
#> printtree-demo/
#> |-- data/
#> |   `-- raw/
#> |-- R/
#> |   `-- hello.R
#> |-- DESCRIPTION
#> `-- README.md

You can customize detection using rootmarkers:

print_rtree(subdir,
project = "root",
root_markers = c(".Rproj", "DESCRIPTION", "_quarto.yml"))
#> printtree-demo/
#> |-- data/
#> |   `-- raw/
#> |-- R/
#> |   `-- hello.R
#> |-- DESCRIPTION
#> `-- README.md

Common Options

Limit depth:

print_rtree(max_depth = 2)
#> vignettes/
#> |-- printtree.Rmd
#> |-- tree-dark.png
#> |-- tree-white.png
#> `-- tree.png

Show hidden files:

print_rtree(demo, show_hidden = TRUE, max_depth = 2)
#> printtree-demo/
#> |-- data/
#> |   `-- raw/
#> |-- R/
#> |   `-- hello.R
#> |-- DESCRIPTION
#> `-- README.md

Use unicode tree glyphs (if your terminal supports them):

print_rtree(demo, format = "unicode", max_depth = 2)
#> printtree-demo/
#> ├── data/
#> │   └── raw/
#> ├── R/
#> │   └── hello.R
#> ├── DESCRIPTION
#> └── README.md

Generate a PNG snapshot:

# Save PNG snapshots 
png_light <- tempfile("tree-light-", fileext = ".png")
png_dark  <- tempfile("tree-dark-",  fileext = ".png")

# Light (white bg, black text)
print_rtree(demo, snapshot = TRUE, snapshot_bg = "white", snapshot_file = png_light)
#> printtree-demo/
#> |-- data/
#> |   `-- raw/
#> |-- R/
#> |   `-- hello.R
#> |-- DESCRIPTION
#> `-- README.md

# Dark (black bg, white text)
print_rtree(demo, snapshot = TRUE, snapshot_bg = "black", snapshot_file = png_dark)
#> printtree-demo/
#> |-- data/
#> |   `-- raw/
#> |-- R/
#> |   `-- hello.R
#> |-- DESCRIPTION
#> `-- README.md