Something went wrong. Try again.
[mirror] R Package for a Mildly Opinionated Stan Bayesian Workflow stanflow.visruth.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163#' Cite Stan packages in a project/files#'#' `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.#'#' The parsing is handled by `scan_usage()`; `stan_cite()` owns#' the citation lookups.#'#' @param path A single project directory (searched recursively) or a vector of#' files (.R/.Rmd/.qmd).#' @param ignore_unqualified_functions Defaults to exports from base R packages#' listed in `stdlib_funs()`. Character vector of function names to ignore when#' attributing (unqualified) calls to Stan packages. Calls like `rstan::plot()`#' will NOT be ignored even if `plot` is in `ignore_unqualified_functions`, since#' they are namespaced.#' @param strict If `TRUE` (default), only count unqualified function calls#' whose origin can be determined exactly from the static scan, including#' attachment-order tie-breaks when the winner is unambiguous from the file.#' Unresolved calls are warned about and omitted.#' @param skip_dirs Defaults to directories listed in `scan_skip_dirs`. Character#' vector of directory names to skip when scanning a directory.#' @param use_knitr Logical. If `TRUE`, parse `.Rmd` and `.qmd` files with#' `knitr::purl()`. This is more accurate for knitr/quarto chunk extraction#' but much slower than the default in-house parser. Defaults to `FALSE`.#' @param format One of "bibtex" or "bibentry", specifying the return format.#' @param quiet Logical. If `TRUE`, suppresses status messages.#' @return A BibTeX character vector or a bibentry object.#' @export#' @examples#' 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)stan_cite <- function( 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)) { local_cli_quiet(quiet)
scan_usage( path = path, allowed_packages = .stan_pkgs, export_index = .stan_export_index, origin_map = .stan_origin_map, ignore_unqualified_functions = ignore_unqualified_functions, strict = strict, skip_dirs = skip_dirs, metapackages = list(stanflow = core), use_knitr = use_knitr, quiet = quiet ) |> (\(x) { list( pkgs = unique(c(x$packages, "stanflow")), funs = unique(x$functions) ) })() |> (\(k) { c( # Build package citations by first looking them up in the `.stan_citation_pkgs` # environment generated in `data-raw/sysdata.R`, then appealing to `.pkg_cite()`. packages = mget( k$pkgs, envir = .stan_citation_pkgs, inherits = TRUE, ifnotfound = list(NULL) ) |> Map( \(pkg, entry) { if (is.null(entry)) .pkg_cite(pkg) else entry }, pkg = k$pkgs, entry = _ ), # Build function citations by pure lookup against `.stan_citation_funs` # environment generated in `data-raw/sysdata.R` functions = mget( k$funs, envir = .stan_citation_funs, inherits = TRUE, ifnotfound = list(NULL) ) ) |> Filter(Negate(is.null), x = _) # ignore functions without citations })() |> (\(entries) { if (!length(entries)) { cli::cli_alert_info("No citations found.") character() } else { # Add base R citation, and format as requested entries <- do.call(c, entries) |> c(utils::citation("base")) if (match.arg(format, c("bibtex", "bibentry")) == "bibentry") { entries } else { toBibtex(entries) } } })()}
#' Build Stan package bibentry citations#'#' Helper function to build standardized package citations.#' This mostly matches how each Stan R package wants to be cited.#' Some Stan packages have additional paper citations generated in#' `data-raw/sysdata.R` and stored in `.stan_citation_pkg_extras`.#'#' @param pkg Stan package name as a character scalar.#' @return Vector of bibentries for citing that package.#' @keywords internal.pkg_cite <- function(pkg) { pkg |> packageDescription() |> (\(meta) { c( utils::bibentry( bibtype = "Manual", key = pkg, title = meta[["Title"]], author = citation(meta[["Package"]])[[1]]$author, year = sub("-.*", "", meta[["Date"]]), note = sprintf( "R package version %s, https://discourse.mc-stan.org", meta$Version ), # rstan url will point to rstan package site instead of main Stan site here. url = sprintf("https://mc-stan.org/%s/", pkg) ), mget( pkg, envir = .stan_citation_pkg_extras, inherits = TRUE, ifnotfound = list(NULL) )[[1L]] ) })()}