Something went wrong. Try again.
[mirror] R Package for a Mildly Opinionated Stan Bayesian Workflow stanflow.visruth.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624test_that("setup_interface adds cmdstanr when brms_backend = cmdstanr", { libraries <- character() same_library <- function(pkg) { libraries <<- c(libraries, pkg) invisible(NULL) } local_mocked_bindings( is_installed = function(pkg) TRUE, install_backend_package = function(...) { stop("install_backend_package should not run") }, .same_library = same_library, setup_cmdstanr = function(...) invisible(NULL), setup_brms = function(...) invisible(NULL), .package = "stanflow" )
setup_interface( interface = "brms", brms_backend = "cmdstanr", cores = 2, quiet = TRUE )
expect_equal(libraries, c("brms", "cmdstanr"))})
test_that("setup_interface aborts when cores are missing", { withr::local_options(list(mc.cores = NULL)) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE), "cores" )})
test_that("setup_interface aborts when interface is missing", { expect_error( setup_interface(), "interface" )})
test_that("setup_interface installs backends when reinstall = TRUE", { installed <- character()
local_mocked_bindings( is_installed = function(pkg) TRUE, install_backend_package = function(pkg, ...) { installed <<- c(installed, pkg) }, .same_library = function(pkg) NULL, setup_cmdstanr = function(...) invisible(NULL), setup_rstan = function(...) invisible(NULL), .package = "stanflow" )
invisible( setup_interface( interface = c("cmdstanr", "rstan"), force = TRUE, reinstall = TRUE, cores = 2, quiet = TRUE ) )
expect_equal(installed, c("cmdstanr", "rstan"))})
test_that("setup_interface installs missing packages when not installed", { installed <- character()
local_mocked_bindings( is_installed = function(pkg) FALSE, install_backend_package = function(pkg, ...) { installed <<- c(installed, pkg) }, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" )
invisible( setup_interface( interface = c("brms"), brms_backend = "rstan", force = FALSE, cores = 2, quiet = TRUE ) )
expect_equal(installed, "brms")})
test_that("setup_interface runs backend setup helpers", { calls <- character()
local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) { calls <<- c(calls, paste0("library:", pkg)) invisible(NULL) }, setup_cmdstanr = function(...) { calls <<- c(calls, "setup_cmdstanr") invisible(NULL) }, setup_rstan = function(...) { calls <<- c(calls, "setup_rstan") invisible(NULL) }, setup_brms = function(...) { calls <<- c(calls, "setup_brms") invisible(NULL) }, setup_rstanarm = function(...) { calls <<- c(calls, "setup_rstanarm") invisible(NULL) }, .package = "stanflow" )
invisible( setup_interface( interface = c("cmdstanr", "rstan", "brms", "rstanarm"), brms_backend = "cmdstanr", cores = 2, quiet = TRUE, force = FALSE ) )
expect_equal( calls, c( "setup_cmdstanr", "library:cmdstanr", "setup_rstan", "library:rstan", "setup_brms", "library:brms", "setup_rstanarm", "library:rstanarm" ) )})
test_that("setup_interface is silent when quiet = TRUE", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" )
output <- capture_messages( setup_interface( interface = "brms", brms_backend = "rstan", cores = 2, quiet = TRUE ) )
expect_equal(output, character())})
test_that("setup_interface defaults to stanflow.quiet option", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, setup_brms = function(...) invisible(NULL), .package = "stanflow" )
withr::local_options(list(stanflow.quiet = TRUE)) silent <- capture_messages( setup_interface(interface = "brms", brms_backend = "rstan", cores = 2) ) expect_equal(silent, character())
withr::local_options(list(stanflow.quiet = FALSE)) noisy <- capture_messages( setup_interface(interface = "brms", brms_backend = "rstan", cores = 2) ) expect_true(length(noisy) > 0)})
test_that("install_backend_package installs from Stan universe when dev = TRUE", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") )) called <- list()
local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" )
install_backend_package( "cmdstanr", dev = TRUE, quiet = TRUE, force = TRUE, reinstall = FALSE )
expect_equal(called$pkg, "cmdstanr") expect_equal(unname(called$repos[1]), "https://stan-dev.r-universe.dev")})
test_that("install_backend_package respects quiet = FALSE", { called <- list()
local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" )
install_backend_package( "cmdstanr", dev = FALSE, quiet = FALSE, force = TRUE, reinstall = FALSE )
expect_false(called$quiet)})
test_that("install_backend_package installs from multiverse when dev = FALSE", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") )) called <- list()
local_mocked_bindings( install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" )
install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = TRUE, reinstall = FALSE )
expect_equal(called$pkg, "cmdstanr") expect_equal(unname(called$repos[1]), "https://community.r-multiverse.org") expect_equal(unname(called$repos[2]), unname(getOption("repos")))})
test_that("install_backend_package aborts when user declines interactive install", { withr::local_options(list(stanflow.force_interactive = TRUE)) local_mocked_bindings( menu = function(...) 2, install.packages = function(...) stop("should not reach install"), .package = "utils" ) expect_error( install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = FALSE, reinstall = FALSE ), "Installation of" )})
test_that("install_backend_package installs when user accepts interactive prompt", { called <- list() withr::local_options(list(stanflow.force_interactive = TRUE)) local_mocked_bindings( menu = function(...) 1, install.packages = function(pkg, repos, quiet) { called <<- list(pkg = pkg, repos = repos, quiet = quiet) }, .package = "utils" ) invisible( install_backend_package( "cmdstanr", dev = FALSE, quiet = TRUE, force = FALSE, reinstall = FALSE ) ) expect_equal(called$pkg, "cmdstanr")})
test_that("setup_brms configures brms backend", { withr::local_options(list(mc.cores = NULL, brms.backend = NULL))
setup_brms(quiet = TRUE, brms_backend = "cmdstanr", cores = 8)
expect_equal(getOption("mc.cores"), 8) expect_equal(getOption("brms.backend"), "cmdstanr")})
test_that("setup_brms emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL, brms.backend = NULL)) msg <- NULL cli_alert_info <- function(...) { msg <<- paste0(...) invisible(NULL) }
local_mocked_bindings( cli_alert_info = cli_alert_info, .package = "cli" )
setup_brms(quiet = FALSE, brms_backend = "rstan", cores = 4)
expect_match(msg, "Configured") expect_match(msg, "brms")})
test_that("setup_rstan configures parallel cores and rstan options", { withr::local_options(list(mc.cores = NULL)) rstan_args <- NULL skip_if_not_installed("rstan") rstan_options <- function(...) { rstan_args <<- list(...) invisible(NULL) }
local_mocked_bindings( rstan_options = rstan_options, .package = "rstan" )
setup_rstan(quiet = TRUE, cores = 6, rstan_auto_write = FALSE)
expect_equal(getOption("mc.cores"), 6) expect_false(rstan_args$auto_write)})
test_that("setup_rstan emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL)) skip_if_not_installed("rstan") msg <- NULL cli_alert_info <- function(...) { msg <<- paste0(...) invisible(NULL) }
local_mocked_bindings( rstan_options = function(...) NULL, .package = "rstan" ) local_mocked_bindings( cli_alert_info = cli_alert_info, .package = "cli" )
setup_rstan(quiet = FALSE, cores = 3, rstan_auto_write = FALSE)
expect_match(msg, "Configured") expect_match(msg, "rstan") expect_match(msg, "auto_write = FALSE")})
test_that("setup_rstanarm emits configuration message when quiet = FALSE", { withr::local_options(list(mc.cores = NULL)) msg <- NULL cli_alert_info <- function(...) { msg <<- paste0(...) invisible(NULL) }
local_mocked_bindings( cli_alert_info = cli_alert_info, .package = "cli" )
setup_rstanarm(quiet = FALSE, cores = 5)
expect_match(msg, "Configured") expect_match(msg, "rstanarm")})
test_that("setup_cmdstanr aborts when toolchain check fails", { skip_on_cran() skip_if_not_installed("cmdstanr") expect_error( { local_mocked_bindings( check_cmdstan_toolchain = function(...) stop("broken"), .package = "cmdstanr" ) setup_cmdstanr(quiet = TRUE, force = FALSE, cores = 2) }, "CmdStan toolchain check failed" )})
test_that("setup_cmdstanr installs CmdStan when not ready and force = TRUE", { skip_on_cran() skip_if_not_installed("cmdstanr") installed <- FALSE install_cmdstan <- function(...) { installed <<- TRUE invisible(NULL) }
local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() stop("missing"), cmdstan_version = function() stop("missing"), install_cmdstan = install_cmdstan, .package = "cmdstanr" )
invisible(setup_cmdstanr(quiet = TRUE, force = TRUE, cores = 2))
expect_true(installed)})
test_that("setup_cmdstanr returns invisibly when up to date", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" ) result <- setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = FALSE, cores = 2 )
expect_identical(result, TRUE)})
test_that("setup_cmdstanr does not check updates by default", { skip_on_cran() skip_if_not_installed("cmdstanr")
local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" )
local_mocked_bindings( readLines = function(...) stop("should not check updates"), .package = "base" )
expect_identical( setup_cmdstanr(quiet = TRUE, force = FALSE, cores = 2), TRUE )})
test_that("setup_cmdstanr ignores failed update checks when CmdStan is ready", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", install_cmdstan = function(...) stop("should not install"), .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" )
result <- setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = TRUE, cores = 2 )
expect_identical(result, TRUE)})
test_that("try_fetch_latest_cmdstan_version parses latest release", { local_mocked_bindings( readLines = function(...) { c( "{", ' "tag_name": "v2.37.0"', "}" ) }, .package = "base" )
expect_equal(try_fetch_latest_cmdstan_version(), numeric_version("2.37.0"))})
test_that("try_fetch_latest_cmdstan_version returns NULL on bad responses", { local_mocked_bindings( readLines = function(...) "{}", .package = "base" ) expect_null(try_fetch_latest_cmdstan_version())
local_mocked_bindings( readLines = function(...) stop("offline"), .package = "base" ) expect_null(try_fetch_latest_cmdstan_version())})
test_that("setup_cmdstanr is silent when quiet = TRUE", { skip_on_cran() skip_if_not_installed("cmdstanr") local_mocked_bindings( check_cmdstan_toolchain = function(...) TRUE, cmdstan_path = function() "/tmp", cmdstan_version = function() "2.31.0", .package = "cmdstanr" ) local_mocked_bindings( readLines = function(...) stop("no network"), .package = "base" )
output <- capture_messages( setup_cmdstanr( quiet = TRUE, force = FALSE, check_updates = FALSE, cores = 2 ) )
expect_equal(output, character())})
test_that("setup_interface aborts when install_backend_package fails", { local_mocked_bindings( is_installed = function(pkg) FALSE, install_backend_package = function(pkg, ...) { cli::cli_abort("Boom") }, setup_cmdstanr = function(...) invisible(NULL), .package = "stanflow" ) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "Boom" )})
test_that("setup_interface handles same_library errors gracefully", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) stop("library error"), setup_cmdstanr = function(...) invisible(NULL), .package = "stanflow" ) expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "library error" )})
test_that("setup_interface does not attach packages before setup succeeds", { attached <- character() local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) { attached <<- c(attached, pkg) }, setup_cmdstanr = function(...) cli::cli_abort("Boom"), .package = "stanflow" )
expect_error( setup_interface(interface = "cmdstanr", quiet = TRUE, cores = 2), "Boom" ) expect_equal(attached, character())})
test_that("setup_interface warns when brms_backend adds cmdstanr", { local_mocked_bindings( is_installed = function(pkg) TRUE, .same_library = function(pkg) NULL, install_backend_package = function(...) stop("install should not run"), setup_cmdstanr = function(...) invisible(NULL), setup_brms = function(...) invisible(NULL), .package = "stanflow" ) expect_snapshot( setup_interface( interface = c("brms"), brms_backend = "cmdstanr", cores = 2, quiet = FALSE ) )})