| Title: | A Mildly Opinionated Stan Bayesian Workflow |
|---|---|
| Description: | Streamlines access to the Stan ecosystem by attaching core workflow packages, surfacing conflicts, and providing helpers to set up Stan interfaces with sensible defaults. Facilitates the Bayesian Workflow as described in Gelman et al. (2020) <https://arxiv.org/abs/2011.01808>. |
| Authors: | Visruth Srimath Kandali [aut, cre, cph] (ORCID: <https://orcid.org/0009-0005-9097-0688>), Jonah Gabry [ctb], Posit Software, PBC [cph] (Copyright holder for code adapted from tidyverse package (MIT); see LICENSE.note) |
| Maintainer: | Visruth Srimath Kandali <[email protected]> |
| License: | BSD_3_clause + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-07-23 10:21:29 UTC |
| Source: | https://github.com/VisruthSK/stanflow |
Print a consolidated status report showing attached packages, available interfaces, and any conflicts.
flow_check(check_updates = FALSE, only = NULL)flow_check(check_updates = FALSE, only = NULL)
check_updates |
If |
only |
Set this to a character vector to restrict to conflicts only between the provided packages and loaded stanflow packages. |
Invisibly returns the character vector that was printed.
flow_check()flow_check()
This function is primarily exported for developers. The scanner itself is
generic and requires an explicit package universe; stan_cite() is the
Stan-configured entry point.
The scanning is wholly static (AST parsing), so there are
a number of restrictions on what calls are recognized:
calls to library(), require(), requireNamespace(),
or use() are all recognized as attaching a package.
scan_usage( path = ".", allowed_packages, export_index, origin_map, ignore_unqualified_functions = .stdlib_funs, strict = FALSE, skip_dirs = .scan_skip_dirs, metapackages = NULL, use_knitr = FALSE, quiet = getOption("stanflow.quiet", FALSE) )scan_usage( path = ".", allowed_packages, export_index, origin_map, ignore_unqualified_functions = .stdlib_funs, strict = FALSE, skip_dirs = .scan_skip_dirs, metapackages = NULL, use_knitr = FALSE, quiet = getOption("stanflow.quiet", FALSE) )
path |
A single project directory (searched recursively) or a vector of files (.R/.Rmd/.qmd). |
allowed_packages |
Character vector of package namespaces to attribute calls to. |
export_index |
Named list mapping function names to packages. |
origin_map |
Named character vector mapping |
ignore_unqualified_functions |
Defaults to exports from base R packages
listed in |
strict |
If |
skip_dirs |
Character vector of directory names to skip when scanning a
directory. Defaults to |
metapackages |
Named list mapping attached package names to additional
packages that should be treated as co-attached for unqualified resolution.
Defaults to |
use_knitr |
Logical. If |
quiet |
Logical. If |
Explicit package references from library(), require(),
requireNamespace(), use(), and pkg::fun are only recorded when their
package is included in allowed_packages. Unqualified function calls are
only attributed when a package is attached via library() or require() in
the same file and allowed_packages, export_index, and origin_map
describe how to resolve them. Attaching a metapackage can also be treated as
attaching additional packages when metapackages is supplied. When multiple
attached packages export the same unqualified function, attachment order is
respected: the most recently attached matching package whose attachment
appears before the call is treated as the winner. Known reexports are
remapped to their origin packages; missing mappings fall back to the
resolved package.
A list of packages, resolved functions, and ambiguous function calls.
path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(stats)", "requireNamespace(\"utils\")", "filter(1:10, rep(1, 3))", "utils::head(letters)" ), path ) scan_usage( path, allowed_packages = c("stats", "utils"), export_index = list(filter = "stats"), origin_map = c("stats::filter" = "stats"), ignore_unqualified_functions = character(), quiet = TRUE ) unlink(path)path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(stats)", "requireNamespace(\"utils\")", "filter(1:10, rep(1, 3))", "utils::head(letters)" ), path ) scan_usage( path, allowed_packages = c("stats", "utils"), export_index = list(filter = "stats"), origin_map = c("stats::filter" = "stats"), ignore_unqualified_functions = character(), quiet = TRUE ) unlink(path)
Configures brms to use available cores and sets the backend.
Prefer setup_interface() for user-facing setup since it performs argument
validation and defaults; setup_brms() assumes inputs are already checked.
setup_brms(quiet, brms_backend, cores, dry_run = FALSE)setup_brms(quiet, brms_backend, cores, dry_run = FALSE)
quiet |
Logical. If |
brms_backend |
Character. The |
cores |
Integer. Number of cores to use. Defaults to
|
dry_run |
Logical. If |
Returns NULL invisibly.
## Not run: setup_brms(quiet = TRUE, brms_backend = "cmdstanr", cores = 2) ## End(Not run)## Not run: setup_brms(quiet = TRUE, brms_backend = "cmdstanr", cores = 2) ## End(Not run)
Checks the C++ toolchain, locates CmdStan, and installs or upgrades
CmdStan if needed. Prefer setup_interface() for user-facing setup since
it performs argument validation and defaults; setup_cmdstanr() assumes
inputs are already checked.
setup_cmdstanr( quiet, force, reinstall = FALSE, check_updates = FALSE, cores, dry_run = FALSE )setup_cmdstanr( quiet, force, reinstall = FALSE, check_updates = FALSE, cores, dry_run = FALSE )
quiet |
Logical. If |
force |
Logical. If |
reinstall |
Logical. If |
check_updates |
Logical. If |
cores |
Integer. Number of cores to use. Defaults to
|
dry_run |
Logical. If |
Returns TRUE invisibly when no install/upgrade is needed.
Otherwise, returns NULL invisibly after installation.
## Not run: setup_cmdstanr( quiet = TRUE, force = TRUE, reinstall = FALSE, check_updates = FALSE, cores = 2 ) ## End(Not run)## Not run: setup_cmdstanr( quiet = TRUE, force = TRUE, reinstall = FALSE, check_updates = FALSE, cores = 2 ) ## End(Not run)
This function ensures specific Stan interfaces are installed, configured, and loaded. It handles package installation (from R-multiverse/CRAN (stable) or Stan universe (dev)) and performs necessary one-time setup (like installing CmdStan).
setup_interface( interface = c("brms", "cmdstanr", "rstan", "rstanarm"), cores = getOption("mc.cores"), quiet = getOption("stanflow.quiet", FALSE), force = FALSE, reinstall = FALSE, check_updates = FALSE, dev = FALSE, brms_backend = c("cmdstanr", "rstan"), rstan_auto_write = TRUE, dry_run = FALSE )setup_interface( interface = c("brms", "cmdstanr", "rstan", "rstanarm"), cores = getOption("mc.cores"), quiet = getOption("stanflow.quiet", FALSE), force = FALSE, reinstall = FALSE, check_updates = FALSE, dev = FALSE, brms_backend = c("cmdstanr", "rstan"), rstan_auto_write = TRUE, dry_run = FALSE )
interface |
A character vector. Select at least one of: "brms", "cmdstanr", "rstan", "rstanarm". |
cores |
Integer. Number of cores to use. Defaults to
|
quiet |
Logical. If |
force |
Logical. If |
reinstall |
Logical. If |
check_updates |
Logical. If |
dev |
Logical. If |
brms_backend |
Character. The |
rstan_auto_write |
Logical. If |
dry_run |
Logical. If |
The setup functions are exported (e.g., setup_brms()) for transparency.
Each function has some side effects, mainly setting mc.cores, see the function
for specifics.
Returns attached package names invisibly. With dry_run = TRUE, returns the package names that would be attached.
## Not run: options(mc.cores = 2) setup_interface("cmdstanr", quiet = TRUE) setup_interface( c("brms", "cmdstanr"), brms_backend = "cmdstanr", quiet = TRUE ) ## End(Not run)## Not run: options(mc.cores = 2) setup_interface("cmdstanr", quiet = TRUE) setup_interface( c("brms", "cmdstanr"), brms_backend = "cmdstanr", quiet = TRUE ) ## End(Not run)
Configures rstan to use available cores and write compiled models to disk.
Prefer setup_interface() for user-facing setup since it performs argument
validation and defaults; setup_rstan() assumes inputs are already checked.
setup_rstan(quiet, cores, rstan_auto_write, dry_run = FALSE)setup_rstan(quiet, cores, rstan_auto_write, dry_run = FALSE)
quiet |
Logical. If |
cores |
Integer. Number of cores to use. Defaults to
|
rstan_auto_write |
Logical. If |
dry_run |
Logical. If |
Returns NULL invisibly.
## Not run: setup_rstan(quiet = TRUE, cores = 2, rstan_auto_write = TRUE) ## End(Not run)## Not run: setup_rstan(quiet = TRUE, cores = 2, rstan_auto_write = TRUE) ## End(Not run)
Configures rstanarm to use available cores.
Prefer setup_interface() for user-facing setup since it performs argument
validation and defaults; setup_rstanarm() assumes inputs are already checked.
setup_rstanarm(quiet, cores, dry_run = FALSE)setup_rstanarm(quiet, cores, dry_run = FALSE)
quiet |
Logical. If |
cores |
Integer. Number of cores to use. Defaults to
|
dry_run |
Logical. If |
Returns NULL invisibly.
## Not run: setup_rstanarm(quiet = TRUE, cores = 2) ## End(Not run)## Not run: setup_rstanarm(quiet = TRUE, cores = 2) ## End(Not run)
stan_cite() generates the correct citations for Stan packages
in a directory or set of files. Quarto (.qmd) and R Markdown (.Rmd)
documents are scanned by extracting R code chunks directly from the
source text by default. Setting use_knitr = TRUE switches to
knitr::purl(), which is more accurate for knitr/quarto chunk handling
but much slower. stan_cite() uses some simple heuristics to guess which
packages export functions, and also attempts to map re-exports to their
origin package. Calls to library(), require(), requireNamespace(),
or use() are all recognized as attaching a package.
stan_cite( path = ".", strict = TRUE, format = c("bibtex", "bibentry"), skip_dirs = .scan_skip_dirs, ignore_unqualified_functions = .stdlib_funs, use_knitr = FALSE, quiet = getOption("stanflow.quiet", FALSE) )stan_cite( path = ".", strict = TRUE, format = c("bibtex", "bibentry"), skip_dirs = .scan_skip_dirs, ignore_unqualified_functions = .stdlib_funs, use_knitr = FALSE, quiet = getOption("stanflow.quiet", FALSE) )
path |
A single project directory (searched recursively) or a vector of files (.R/.Rmd/.qmd). |
strict |
If |
format |
One of "bibtex" or "bibentry", specifying the return format. |
skip_dirs |
Defaults to directories listed in |
ignore_unqualified_functions |
Defaults to exports from base R packages
listed in |
use_knitr |
Logical. If |
quiet |
Logical. If |
The parsing is handled by scan_usage(); stan_cite() owns
the citation lookups.
A BibTeX character vector or a bibentry object.
path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(posterior)", "requireNamespace(\"loo\")", "draws <- as_draws(list(mu = rnorm(10)))", "posterior::rhat(draws)", "loo::loo(matrix(1))" ), path ) stan_cite(path, quiet = TRUE) stan_cite(path, format = "bibentry", quiet = TRUE) unlink(path)path <- tempfile(fileext = ".R") writeLines( c( "# one messy analysis file", "library(posterior)", "requireNamespace(\"loo\")", "draws <- as_draws(list(mu = rnorm(10)))", "posterior::rhat(draws)", "loo::loo(matrix(1))" ), path ) stan_cite(path, quiet = TRUE) stan_cite(path, format = "bibentry", quiet = TRUE) unlink(path)
Print the Stan ASCII art logo
stan_logo()stan_logo()
Invisibly returns the character vector that was printed.
stan_logo()stan_logo()
Stan package repositories
stan_repos(dev = FALSE)stan_repos(dev = FALSE)
dev |
Include the development r-universe repo–don't use this unless you need the latest commits. |
Character vector of repository URLs.
stan_repos() stan_repos(dev = TRUE)stan_repos() stan_repos(dev = TRUE)
List conflicts between stanflow packages and other attached packages.
stanflow_conflicts(only = NULL) ## S3 method for class 'stanflow_conflicts' print(x, ...)stanflow_conflicts(only = NULL) ## S3 method for class 'stanflow_conflicts' print(x, ...)
only |
Defaults to |
x |
A |
... |
Unused. Included for consistency with |
There are several conflicts that are deliberately ignored: diag,
drop, match, \%in\%, mad, sd, and var from posterior.
Adapted from tidyverse::tidyverse_conflicts() for stanflow's package set.
Invisibly returns x.
stanflow_conflicts() stanflow_conflicts(c("base")) conflicts <- stanflow_conflicts() print(conflicts)stanflow_conflicts() stanflow_conflicts(c("base")) conflicts <- stanflow_conflicts() print(conflicts)
Returns a data frame of Stan workflow packages and their local/remote versions.
When check_updates = FALSE, remote versions are not queried and the remote
and behind columns are NA and FALSE, respectively.
Adapted from tidyverse::tidyverse_deps().
stanflow_deps(recursive = FALSE, dev = FALSE, check_updates = TRUE)stanflow_deps(recursive = FALSE, dev = FALSE, check_updates = TRUE)
recursive |
If |
dev |
If |
check_updates |
Logical. If |
A data frame with columns:
Package name.
Repository version (character, NA when not queried).
Installed version (character, "0" if not installed).
Logical; TRUE when remote is newer than local.
## Not run: # Full dependency check with remote versions stanflow_deps(recursive = TRUE) # Local-only inventory (fast, no network) stanflow_deps(check_updates = FALSE) ## End(Not run)## Not run: # Full dependency check with remote versions stanflow_deps(recursive = TRUE) # Local-only inventory (fast, no network) stanflow_deps(check_updates = FALSE) ## End(Not run)
Adapted from tidyverse::tidyverse_logo().
stanflow_logo()stanflow_logo()
Invisibly returns the character vector that was printed.
stanflow_logo()stanflow_logo()
Checks for outdated Stan workflow packages and installs updates. This function
requires an interactive R session for installation unless dry_run = TRUE.
Dry runs check repositories and preview installs without prompting or
installing packages.
Adapted from tidyverse::tidyverse_update().
stanflow_update(recursive = FALSE, dev = FALSE, dry_run = FALSE)stanflow_update(recursive = FALSE, dev = FALSE, dry_run = FALSE)
recursive |
If |
dev |
If |
dry_run |
Logical. If |
Invisibly returns a data frame of outdated packages (same columns as
stanflow_deps). Returns NULL invisibly when no updates are
needed.
## Not run: # Update direct dependencies only stanflow_update() # Update full dependency tree (including suggests) stanflow_update(recursive = TRUE) ## End(Not run)## Not run: # Update direct dependencies only stanflow_update() # Update full dependency tree (including suggests) stanflow_update(recursive = TRUE) ## End(Not run)