Learn R Programming

printtree (version 0.2.0)

print_rtree: Print an R Project or Directory Tree

Description

Prints a directory tree for a given path. Optionally detects an RStudio project (.Rproj) and can print from a project root.

Usage

print_rtree(
  path = NULL,
  ignore = c("renv", ".git", ".Rproj.user", "__pycache__", ".DS_Store", "node_modules",
    ".Rhistory"),
  max_depth = NULL,
  show_hidden = FALSE,
  project = c("auto", "root", "none"),
  search_paths = c(".", "..", "~/Documents", "~/Projects"),
  root_markers = c(".Rproj", "DESCRIPTION"),
  format = c("ascii", "unicode"),
  return_lines = FALSE,
  snapshot = FALSE,
  snapshot_file = "tree.png",
  snapshot_width = 800,
  snapshot_bg = c("white", "black"),
  snapshot_path = "."
)

Value

Invisible NULL, or a character vector of printed lines if return_lines = TRUE.

Arguments

path

Character. Directory path, project name, or .Rproj file. If NULL, uses ".".

ignore

Character vector. Basenames to exclude (e.g., ".git", "renv").

max_depth

Integer. Maximum depth to traverse. NULL for unlimited.

show_hidden

Logical (TRUE/FALSE). Whether to include hidden files/directories (starting with ".").

project

One of "auto", "root", "none".

  • "auto": use path as-is (only resolves project name / .Rproj file)

  • "root": walk upward from path to find a project root (via root_markers) and use it if found

  • "none": never attempt root detection; print the tree from path

search_paths

Character vector. Used only when path is not an existing directory (treated as a project name). Paths are searched in order.

root_markers

Character vector. Markers used when project = "root" to detect a root directory. Special value ".Rproj" means "any file ending in .Rproj". Common markers include "DESCRIPTION" (R package root) and "_quarto.yml" (Quarto project root).

format

One of "ascii" or "unicode". "ascii" is portable for all terminals.

return_lines

Logical. If TRUE, invisibly return the printed character vector of lines.

snapshot

Logical. If TRUE, saves a PNG snapshot of the tree.

snapshot_file

Character. Snapshot PNG filename (or full path).

snapshot_width

Integer. PNG width in pixels.

snapshot_bg

One of "white" or "black" for snapshot background.

snapshot_path

Character. Directory to save snapshot_file in when snapshot_file is not absolute.

Examples

Run this code
demo <- file.path(tempdir(), "printtree-demo")
if (dir.exists(demo)) unlink(demo, recursive = TRUE)
dir.create(demo, recursive = TRUE)
dir.create(file.path(demo, "R"))
file.create(file.path(demo, "R", "hello.R"))
file.create(file.path(demo, "README.md"))

print_rtree(demo)
print_rtree(demo, max_depth = 1)

png_file <- tempfile(fileext = ".png")
print_rtree(demo, snapshot = TRUE, snapshot_file = png_file)

Run the code above in your browser using DataLab