| Title: | Print Directory Trees for R Projects and Folders |
|---|---|
| Description: | Quickly visualize 'R' project directory structures with automatic project detection and clean tree output. |
| Authors: | George Arthur [aut, cre, cph] |
| Maintainer: | George Arthur <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-10-12 23:23:10 UTC |
| Source: | https://github.com/prigasg/printtree |
Prints a directory tree for a given path. Optionally, detects an RStudio project
(.Rproj) and can print from a project root.
print_rtree( path = NULL, ignore = c("renv", ".git", ".Rproj.user", "__pycache__", ".DS_Store", "node_modules", ".Rhistory"), ignore_type = c("auto", "fixed", "glob", "regex"), 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, quiet = FALSE, count_footer = TRUE, git = FALSE, git_legend = TRUE, prune = FALSE, snapshot = FALSE, snapshot_file = "tree.png", snapshot_width = 800, snapshot_bg = c("white", "black"), snapshot_path = "." )print_rtree( path = NULL, ignore = c("renv", ".git", ".Rproj.user", "__pycache__", ".DS_Store", "node_modules", ".Rhistory"), ignore_type = c("auto", "fixed", "glob", "regex"), 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, quiet = FALSE, count_footer = TRUE, git = FALSE, git_legend = TRUE, prune = FALSE, snapshot = FALSE, snapshot_file = "tree.png", snapshot_width = 800, snapshot_bg = c("white", "black"), snapshot_path = "." )
path |
Character. Directory path, project name, or |
ignore |
Character vector. Basenames to exclude (e.g., ".git", "renv").
With |
ignore_type |
One of "auto", "fixed", "glob", or "regex". Controls how
|
max_depth |
Integer. Maximum depth to traverse. NULL for unlimited. |
|
Logical (TRUE/FALSE). Whether to include hidden files/directories (starting with "."). |
|
project |
One of "auto", "root", "none".
|
search_paths |
Character vector. Used only when |
root_markers |
Character vector. Markers used when |
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. |
quiet |
Logical. If TRUE, suppress console output. Useful with |
count_footer |
Logical. If TRUE, append a summary like "3 directories, 12 files". |
git |
Logical. If TRUE, annotate files and directories with porcelain
|
git_legend |
Logical. If TRUE and |
prune |
Logical. If TRUE, omit directories with no displayable children. |
snapshot |
Logical. If TRUE, gives a visual snapshot of tree. |
snapshot_file |
Text. Snapshot PNG name if snapshot is set as TRUE. |
snapshot_width |
Integer between 1 and 15000. Default 800. |
snapshot_bg |
Either white or black for snapshot background. If white, tree text appears black and vice. |
snapshot_path |
Character. If snapshot_path is provided, the file is saved there. |
Invisible NULL, or a character vector of printed lines if return_lines = TRUE.
# Create a small example directory tree 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 the tree print_rtree(demo) # Limit depth print_rtree(demo, max_depth = 1) # Save a PNG snapshot to a temporary file png_file <- tempfile(fileext = ".png") print_rtree(demo, snapshot = TRUE, snapshot_file = png_file)# Create a small example directory tree 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 the tree print_rtree(demo) # Limit depth print_rtree(demo, max_depth = 1) # Save a PNG snapshot to a temporary file png_file <- tempfile(fileext = ".png") print_rtree(demo, snapshot = TRUE, snapshot_file = png_file)
Builds both trees with the same options and renders the difference as a
Graphviz DOT graph: nodes only in after are filled green, nodes only in
before are filled red and dashed, and unchanged nodes are neutral. Nodes
are matched by their path relative to each tree root.
tree_diff_dot( before, after, rankdir = c("TB", "LR", "RL", "BT"), file = NULL, ... )tree_diff_dot( before, after, rankdir = c("TB", "LR", "RL", "BT"), file = NULL, ... )
before |
Character. Directory path for the "before" tree. |
after |
Character. Directory path for the "after" tree. |
rankdir |
Character. Graph direction: |
file |
Character or NULL. If given, the diagram text is written to this
file with |
... |
Additional arguments passed to |
Invisibly, the diagram text as a single string (or file when
file is given).
old <- file.path(tempdir(), "printtree-diff-old") new <- file.path(tempdir(), "printtree-diff-new") for (d in c(old, new)) { if (dir.exists(d)) unlink(d, recursive = TRUE) dir.create(d, recursive = TRUE) } file.create(file.path(old, "gone.txt")) file.create(file.path(new, "added.txt")) cat(tree_diff_dot(old, new)) # Left-to-right layout cat(tree_diff_dot(old, new, rankdir = "LR"))old <- file.path(tempdir(), "printtree-diff-old") new <- file.path(tempdir(), "printtree-diff-new") for (d in c(old, new)) { if (dir.exists(d)) unlink(d, recursive = TRUE) dir.create(d, recursive = TRUE) } file.create(file.path(old, "gone.txt")) file.create(file.path(new, "added.txt")) cat(tree_diff_dot(old, new)) # Left-to-right layout cat(tree_diff_dot(old, new, rankdir = "LR"))
Builds both trees with the same options and renders the difference as a
Mermaid flowchart: nodes only in after are green, nodes only in before
are red and dashed, and unchanged nodes are neutral. Nodes are matched by
their path relative to each tree root.
tree_diff_mermaid( before, after, direction = c("TD", "LR", "RL", "BT"), file = NULL, ... )tree_diff_mermaid( before, after, direction = c("TD", "LR", "RL", "BT"), file = NULL, ... )
before |
Character. Directory path for the "before" tree. |
after |
Character. Directory path for the "after" tree. |
direction |
Character. Flowchart direction: |
file |
Character or NULL. If given, the diagram text is written to this
file with |
... |
Additional arguments passed to |
Invisibly, the diagram text as a single string (or file when
file is given).
old <- file.path(tempdir(), "printtree-diff-old") new <- file.path(tempdir(), "printtree-diff-new") for (d in c(old, new)) { if (dir.exists(d)) unlink(d, recursive = TRUE) dir.create(d, recursive = TRUE) } file.create(file.path(old, "gone.txt")) file.create(file.path(new, "added.txt")) file.create(file.path(old, "kept.txt")) file.create(file.path(new, "kept.txt")) cat(tree_diff_mermaid(old, new)) # Left-to-right layout cat(tree_diff_mermaid(old, new, direction = "LR"))old <- file.path(tempdir(), "printtree-diff-old") new <- file.path(tempdir(), "printtree-diff-new") for (d in c(old, new)) { if (dir.exists(d)) unlink(d, recursive = TRUE) dir.create(d, recursive = TRUE) } file.create(file.path(old, "gone.txt")) file.create(file.path(new, "added.txt")) file.create(file.path(old, "kept.txt")) file.create(file.path(new, "kept.txt")) cat(tree_diff_mermaid(old, new)) # Left-to-right layout cat(tree_diff_mermaid(old, new, direction = "LR"))
Builds the directory tree with the same options as build_tree() and
returns it as Graphviz DOT text. The output renders
in Quarto documents (in a {dot} chunk when Graphviz is installed) and
with any DOT-compatible tool. Directories use the folder shape and files
use the note shape.
tree_to_dot( path = NULL, rankdir = c("TB", "LR", "RL", "BT"), file = NULL, git_colors = FALSE, repo_url = NULL, repo_branch = "main", ... )tree_to_dot( path = NULL, rankdir = c("TB", "LR", "RL", "BT"), file = NULL, git_colors = FALSE, repo_url = NULL, repo_branch = "main", ... )
path |
Character. Directory path, project name, or |
rankdir |
Character. Graph direction: |
file |
Character or NULL. If given, the diagram text is written to this
file with |
git_colors |
Logical. If TRUE, nodes are filled by their Git status
(amber for modified, green for untracked, blue for staged). Implies
|
repo_url |
Character or NULL. Base URL of the Git repository, e.g.
|
repo_branch |
Character. Branch used to build the |
... |
Additional arguments passed to |
Invisibly, the diagram text as a single string (or file when
file is given). Use cat() to print it.
demo <- file.path(tempdir(), "printtree-dot-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) cat(tree_to_dot(demo)) # Left-to-right layout with clickable nodes cat(tree_to_dot(demo, rankdir = "LR", repo_url = "https://github.com/PrigasG/printtree"))demo <- file.path(tempdir(), "printtree-dot-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) cat(tree_to_dot(demo)) # Left-to-right layout with clickable nodes cat(tree_to_dot(demo, rankdir = "LR", repo_url = "https://github.com/PrigasG/printtree"))
Builds the directory tree with the same options as build_tree() and
returns it as a self-contained HTML document: directories expand and
collapse on click, with no external dependencies. When the tree is built
with git = TRUE, Git status badges are shown next to changed entries.
tree_to_html(path = NULL, title = NULL, file = NULL, git_colors = FALSE, ...)tree_to_html(path = NULL, title = NULL, file = NULL, git_colors = FALSE, ...)
path |
Character. Directory path, project name, or |
title |
Character or NULL. Page title and heading. Defaults to
|
file |
Character or NULL. If given, the HTML is written to this file
with |
git_colors |
Logical. If TRUE, nodes are colored by their Git status
(badges for modified, untracked, and staged files). Implies |
... |
Additional arguments passed to |
Invisibly, the HTML document as a single string (or file when
file is given).
demo <- file.path(tempdir(), "printtree-html-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) html <- tree_to_html(demo, title = "Demo project") out <- tempfile(fileext = ".html") tree_to_html(demo, file = out) # Inside a Git checkout, git_colors = TRUE adds status badges # next to modified, untracked, and staged filesdemo <- file.path(tempdir(), "printtree-html-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) html <- tree_to_html(demo, title = "Demo project") out <- tempfile(fileext = ".html") tree_to_html(demo, file = out) # Inside a Git checkout, git_colors = TRUE adds status badges # next to modified, untracked, and staged files
Builds the directory tree with the same options as build_tree() and
returns it as Mermaid flowchart text. The output
renders natively in Quarto documents (in a {mermaid} chunk), GitHub
Markdown, and anywhere else Mermaid is supported. Directories use the
stadium shape and files use rectangles.
tree_to_mermaid( path = NULL, direction = c("TD", "LR", "RL", "BT"), file = NULL, git_colors = FALSE, subgraph = FALSE, repo_url = NULL, repo_branch = "main", ... )tree_to_mermaid( path = NULL, direction = c("TD", "LR", "RL", "BT"), file = NULL, git_colors = FALSE, subgraph = FALSE, repo_url = NULL, repo_branch = "main", ... )
path |
Character. Directory path, project name, or |
direction |
Character. Flowchart direction: |
file |
Character or NULL. If given, the diagram text is written to this
file with |
git_colors |
Logical. If TRUE, nodes are colored by their Git status
(amber for modified, green for untracked, blue for staged). Implies
|
subgraph |
Logical. If TRUE, each directory is wrapped in a Mermaid
|
repo_url |
Character or NULL. Base URL of the Git repository, e.g.
|
repo_branch |
Character. Branch used to build the |
... |
Additional arguments passed to |
Invisibly, the diagram text as a single string (or file when
file is given). Use cat() to print it, e.g. into a Quarto
{mermaid} chunk.
demo <- file.path(tempdir(), "printtree-mermaid-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) cat(tree_to_mermaid(demo)) # Directories as visual containers, laid out left to right cat(tree_to_mermaid(demo, subgraph = TRUE, direction = "LR")) # Nodes that link into the repository on GitHub cat(tree_to_mermaid(demo, repo_url = "https://github.com/PrigasG/printtree"))demo <- file.path(tempdir(), "printtree-mermaid-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) cat(tree_to_mermaid(demo)) # Directories as visual containers, laid out left to right cat(tree_to_mermaid(demo, subgraph = TRUE, direction = "LR")) # Nodes that link into the repository on GitHub cat(tree_to_mermaid(demo, repo_url = "https://github.com/PrigasG/printtree"))
Builds the directory tree with the same options as build_tree() and
returns it as Mermaid mindmap text: a radial
alternative to the flowchart produced by tree_to_mermaid(). The root uses
the cloud shape and every level is indented beneath its parent.
tree_to_mindmap(path = NULL, file = NULL, ...)tree_to_mindmap(path = NULL, file = NULL, ...)
path |
Character. Directory path, project name, or |
file |
Character or NULL. If given, the diagram text is written to this
file with |
... |
Additional arguments passed to |
Invisibly, the diagram text as a single string (or file when
file is given). Use cat() to print it, e.g. into a Quarto
{mermaid} chunk.
demo <- file.path(tempdir(), "printtree-mindmap-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) cat(tree_to_mindmap(demo)) # Save a mindmap file, ready for a Quarto {mermaid} chunk tree_to_mindmap(demo, file = tempfile(fileext = ".mmd"))demo <- file.path(tempdir(), "printtree-mindmap-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) cat(tree_to_mindmap(demo)) # Save a mindmap file, ready for a Quarto {mermaid} chunk tree_to_mindmap(demo, file = tempfile(fileext = ".mmd"))
Writes an HTML page that renders tree_to_mermaid() output as an SVG diagram
in a web browser. The page loads Mermaid from its public CDN, so rendering
requires an internet connection. Browser opening is always suppressed in
non-interactive sessions, including package checks and automated jobs.
view_mermaid( path = NULL, file = tempfile("printtree-mermaid-", fileext = ".html"), title = "Directory tree", theme = c("default", "neutral", "dark", "forest", "base"), open = interactive(), pan_zoom = TRUE, save = NULL, ... )view_mermaid( path = NULL, file = tempfile("printtree-mermaid-", fileext = ".html"), title = "Directory tree", theme = c("default", "neutral", "dark", "forest", "base"), open = interactive(), pan_zoom = TRUE, save = NULL, ... )
path |
Character. Directory path, project name, or |
file |
Character. HTML output path. By default, a temporary file is used. |
title |
Character. Page title and heading. |
theme |
Mermaid theme: |
open |
Logical. Open the generated page when running interactively.
Browser opening is skipped when |
pan_zoom |
Logical. Enable pan/zoom controls for navigating large diagrams (via the svg-pan-zoom library). |
save |
Character or NULL. If given, render the diagram to an image
file instead of the HTML page: one of |
... |
Additional arguments passed to |
Invisibly, the generated HTML file path (or the image path when
save is given).
demo <- file.path(tempdir(), "printtree-view-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) preview <- view_mermaid(demo, open = FALSE) file.exists(preview)demo <- file.path(tempdir(), "printtree-view-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(file.path(demo, "R"), recursive = TRUE) file.create(file.path(demo, "R", "hello.R")) file.create(file.path(demo, "README.md")) preview <- view_mermaid(demo, open = FALSE) file.exists(preview)
Builds a directory tree with the same options as print_rtree() and writes it
to a plain text, Markdown, diagram, or Quarto file.
write_tree( path = NULL, file, format = c("txt", "md", "mermaid", "dot", "qmd", "mindmap", "html"), title = NULL, create_dirs = TRUE, ... )write_tree( path = NULL, file, format = c("txt", "md", "mermaid", "dot", "qmd", "mindmap", "html"), title = NULL, create_dirs = TRUE, ... )
path |
Character. Directory path, project name, or |
file |
Character. Output file path. |
format |
One of |
title |
Optional heading: used as the Markdown heading when
|
create_dirs |
Logical. If TRUE, create the output file's parent directory when it does not exist. |
... |
Additional arguments passed to the underlying tree builder
(the same tree options as |
Invisibly returns the output file path.
demo <- file.path(tempdir(), "printtree-write-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(demo, recursive = TRUE) file.create(file.path(demo, "README.md")) out <- tempfile(fileext = ".md") write_tree(demo, out, format = "md") # Mermaid flowchart and a Quarto document embedding it write_tree(demo, tempfile(fileext = ".mmd"), format = "mermaid") write_tree(demo, tempfile(fileext = ".qmd"), format = "qmd", title = "Demo project tree") # Mermaid mindmap and a collapsible HTML tree write_tree(demo, tempfile(fileext = ".mmd"), format = "mindmap") write_tree(demo, tempfile(fileext = ".html"), format = "html", title = "Demo project tree")demo <- file.path(tempdir(), "printtree-write-demo") if (dir.exists(demo)) unlink(demo, recursive = TRUE) dir.create(demo, recursive = TRUE) file.create(file.path(demo, "README.md")) out <- tempfile(fileext = ".md") write_tree(demo, out, format = "md") # Mermaid flowchart and a Quarto document embedding it write_tree(demo, tempfile(fileext = ".mmd"), format = "mermaid") write_tree(demo, tempfile(fileext = ".qmd"), format = "qmd", title = "Demo project tree") # Mermaid mindmap and a collapsible HTML tree write_tree(demo, tempfile(fileext = ".mmd"), format = "mindmap") write_tree(demo, tempfile(fileext = ".html"), format = "html", title = "Demo project tree")