# Printing directory trees with printtree

    library(printtree)

## 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()
    #> printtree/
    #> |-- doc/
    #> |   |-- printtree.html
    #> |   |-- printtree.R
    #> |   `-- printtree.Rmd
    #> |-- docs/
    #> |   |-- articles/
    #> |   |   |-- index.html
    #> |   |   |-- printtree.html
    #> |   |   |-- tree-dark.png
    #> |   |   `-- tree-white.png
    #> |   |-- deps/
    #> |   |   |-- bootstrap-5.3.1/
    #> |   |   |   |-- bootstrap.bundle.min.js
    #> |   |   |   |-- bootstrap.bundle.min.js.map
    #> |   |   |   `-- bootstrap.min.css
    #> |   |   |-- bootstrap-toc-1.0.1/
    #> |   |   |   `-- bootstrap-toc.min.js
    #> |   |   |-- clipboard.js-2.0.11/
    #> |   |   |   `-- clipboard.min.js
    #> |   |   |-- font-awesome-6.5.2/
    #> |   |   |   |-- css/
    #> |   |   |   |   |-- all.css
    #> |   |   |   |   |-- all.min.css
    #> |   |   |   |   |-- v4-shims.css
    #> |   |   |   |   `-- v4-shims.min.css
    #> |   |   |   `-- webfonts/
    #> |   |   |       |-- fa-brands-400.ttf
    #> |   |   |       |-- fa-brands-400.woff2
    #> |   |   |       |-- fa-regular-400.ttf
    #> |   |   |       |-- fa-regular-400.woff2
    #> |   |   |       |-- fa-solid-900.ttf
    #> |   |   |       |-- fa-solid-900.woff2
    #> |   |   |       |-- fa-v4compatibility.ttf
    #> |   |   |       `-- fa-v4compatibility.woff2
    #> |   |   |-- headroom-0.11.0/
    #> |   |   |   |-- headroom.min.js
    #> |   |   |   `-- jQuery.headroom.min.js
    #> |   |   |-- jquery-3.6.0/
    #> |   |   |   |-- jquery-3.6.0.js
    #> |   |   |   |-- jquery-3.6.0.min.js
    #> |   |   |   `-- jquery-3.6.0.min.map
    #> |   |   |-- search-1.0.0/
    #> |   |   |   |-- autocomplete.jquery.min.js
    #> |   |   |   |-- fuse.min.js
    #> |   |   |   `-- mark.min.js
    #> |   |   `-- data-deps.txt
    #> |   |-- news/
    #> |   |   `-- index.html
    #> |   |-- reference/
    #> |   |   |-- figures/
    #> |   |   |   |-- favicon.ico
    #> |   |   |   |-- favicon.png
    #> |   |   |   `-- new_tree.png
    #> |   |   |-- index.html
    #> |   |   `-- print_rtree.html
    #> |   |-- tutorials/
    #> |   |-- 404.html
    #> |   |-- authors.html
    #> |   |-- index.html
    #> |   |-- katex-auto.js
    #> |   |-- LICENSE-text.html
    #> |   |-- LICENSE.html
    #> |   |-- lightswitch.js
    #> |   |-- link.svg
    #> |   |-- pkgdown.js
    #> |   |-- pkgdown.yml
    #> |   |-- search.json
    #> |   `-- sitemap.xml
    #> |-- inst/
    #> |   `-- WORDLIST
    #> |-- man/
    #> |   |-- figures/
    #> |   |   |-- favicon.ico
    #> |   |   |-- favicon.png
    #> |   |   `-- new_tree.png
    #> |   `-- print_rtree.Rd
    #> |-- Meta/
    #> |   `-- vignette.rds
    #> |-- R/
    #> |   |-- print_rtree.R
    #> |   |-- utils-path.R
    #> |   `-- utils-snapshot.R
    #> |-- tests/
    #> |   |-- testthat/
    #> |   |   `-- test-print_rtree.R
    #> |   |-- spelling.R
    #> |   `-- testthat.R
    #> |-- vignettes/
    #> |   |-- printtree.R
    #> |   |-- printtree.Rmd
    #> |   |-- tree-dark.png
    #> |   |-- tree-white.png
    #> |   `-- tree.png
    #> |-- _pkgdown.yml
    #> |-- cran-comments.md
    #> |-- CRAN-SUBMISSION
    #> |-- DESCRIPTION
    #> |-- LICENSE
    #> |-- LICENSE.md
    #> |-- NAMESPACE
    #> |-- NEWS.md
    #> |-- printtree.Rproj
    #> `-- README.md

## 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)
    #> printtree/
    #> |-- doc/
    #> |   |-- printtree.html
    #> |   |-- printtree.R
    #> |   `-- printtree.Rmd
    #> |-- docs/
    #> |   |-- articles/
    #> |   |-- deps/
    #> |   |-- news/
    #> |   |-- reference/
    #> |   |-- tutorials/
    #> |   |-- 404.html
    #> |   |-- authors.html
    #> |   |-- index.html
    #> |   |-- katex-auto.js
    #> |   |-- LICENSE-text.html
    #> |   |-- LICENSE.html
    #> |   |-- lightswitch.js
    #> |   |-- link.svg
    #> |   |-- pkgdown.js
    #> |   |-- pkgdown.yml
    #> |   |-- search.json
    #> |   `-- sitemap.xml
    #> |-- inst/
    #> |   `-- WORDLIST
    #> |-- man/
    #> |   |-- figures/
    #> |   `-- print_rtree.Rd
    #> |-- Meta/
    #> |   `-- vignette.rds
    #> |-- R/
    #> |   |-- print_rtree.R
    #> |   |-- utils-path.R
    #> |   `-- utils-snapshot.R
    #> |-- tests/
    #> |   |-- testthat/
    #> |   |-- spelling.R
    #> |   `-- testthat.R
    #> |-- vignettes/
    #> |   |-- printtree.R
    #> |   |-- printtree.Rmd
    #> |   |-- tree-dark.png
    #> |   |-- tree-white.png
    #> |   `-- tree.png
    #> |-- _pkgdown.yml
    #> |-- cran-comments.md
    #> |-- CRAN-SUBMISSION
    #> |-- DESCRIPTION
    #> |-- LICENSE
    #> |-- LICENSE.md
    #> |-- NAMESPACE
    #> |-- NEWS.md
    #> |-- printtree.Rproj
    #> `-- README.md

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