Something went wrong. Try again.
[mirror] R Package for a Mildly Opinionated Stan Bayesian Workflow stanflow.visruth.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504run_update_with <- function(deps_df, update_fun, check = NULL) { stopifnot(is.function(update_fun)) withr::local_options(list( stanflow.testing = TRUE, stanflow.force_interactive = TRUE )) with_mocked_bindings( stanflow_deps = function(recursive, dev, check_updates = TRUE) { if (!is.null(check)) { check(recursive, dev) } deps_df }, with_mocked_bindings( menu = function(...) 1, install.packages = function(...) invisible(NULL), update_fun(), .package = "utils" ), .package = "stanflow" )}
test_that("stanflow_update reports when nothing is behind", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") ))
empty <- data.frame( package = character(), remote = character(), local = character(), behind = logical() )
expect_snapshot_output( run_update_with(empty, function() stanflow_update()) )})
test_that("stanflow_update lists behind packages", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") ))
behind <- data.frame( package = c("cmdstanr", "posterior"), remote = c("1.2.0", "1.6.0"), local = c("1.1.0", "1.5.0"), behind = c(TRUE, TRUE), stringsAsFactors = FALSE )
expect_snapshot_output( run_update_with(behind, function() stanflow_update()) )
withr::local_output_sink(withr::local_tempfile()) result <- run_update_with( behind, function() stanflow_update() )
expect_identical(result, behind)})
test_that("stanflow_update aborts when user declines interactive update", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org"), stanflow.force_interactive = TRUE ))
behind <- data.frame( package = c("cmdstanr", "posterior"), remote = c("1.2.0", "1.6.0"), local = c("1.1.0", "1.5.0"), behind = c(TRUE, TRUE), stringsAsFactors = FALSE )
withr::local_output_sink(withr::local_tempfile()) with_mocked_bindings( stanflow_deps = function(...) behind, with_mocked_bindings( menu = function(...) 2, install.packages = function(...) stop("should not reach install"), expect_error( stanflow_update(), "Update aborted by user" ), .package = "utils" ), .package = "stanflow" )})
test_that("stanflow_update installs when user accepts interactive prompt", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org"), stanflow.force_interactive = TRUE ))
behind <- data.frame( package = c("cmdstanr", "posterior"), remote = c("1.2.0", "1.6.0"), local = c("1.1.0", "1.5.0"), behind = c(TRUE, TRUE), stringsAsFactors = FALSE ) called <- list()
withr::local_output_sink(withr::local_tempfile()) result <- with_mocked_bindings( stanflow_deps = function(...) behind, with_mocked_bindings( menu = function(choices, title) { called$choices <<- choices called$title <<- title 1 }, install.packages = function(pkg, repos, quiet) { called$pkg <<- pkg called$repos <<- repos called$quiet <<- quiet invisible(NULL) }, stanflow_update(), .package = "utils" ), .package = "stanflow" )
expect_identical(called$choices, c("Yes", "No")) expect_identical(called$title, "Update packages from R-multiverse (Stable)?") expect_identical(called$pkg, behind$package) expect_true(isTRUE(called$quiet)) expect_identical(result, behind)})
test_that("stanflow_deps computes remote/local state", { fake_available <- matrix( c("1.2.0", "1.6.0", "2.8.0"), nrow = 3, dimnames = list(c("cmdstanr", "posterior", "base"), "Version") )
local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", is_installed = function(pkg) pkg == "cmdstanr", .package = "stanflow" ) local_mocked_bindings( available.packages = function(repos) fake_available, packageVersion = function(pkg) package_version("1.1.0"), .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { list(stanflow = c("cmdstanr", "posterior", "base")) }, .package = "tools" )
deps <- stanflow_deps(recursive = TRUE, dev = FALSE)
expect_equal(deps$package, c("cmdstanr", "posterior")) expect_equal(deps$local, c("1.1.0", "0")) expect_true(all(deps$behind))})
test_that("stanflow_deps aborts when package metadata cannot be downloaded", { local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", .package = "stanflow" ) local_mocked_bindings( available.packages = function(...) stop("no network"), .package = "utils" )
expect_error( stanflow_deps(check_updates = TRUE), "Unable to reach repositories" )})
test_that("stanflow_deps warns but proceeds on repository access warnings", { local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", .package = "stanflow" ) local_mocked_bindings( available.packages = function(...) { warning("unable to access index for repository") matrix( character(), nrow = 0, ncol = 3, dimnames = list(NULL, c("Package", "Version", "Repository")) ) }, .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { list(stanflow = NA) }, .package = "tools" ) local_mocked_bindings( packageDescription = function(pkg) { list(Depends = "R", Imports = "foo", Suggests = "") }, .package = "utils" ) local_mocked_bindings( is_installed = function(pkg) FALSE, .package = "stanflow" )
expect_warning( deps <- stanflow_deps(check_updates = TRUE), "unable to access index" )
expect_s3_class(deps, "data.frame") expect_equal(deps$package, "foo")})
test_that("stanflow_deps handles missing repo versions", { fake_available <- matrix( c(NA, "1.6.0"), nrow = 2, dimnames = list(c("cmdstanr", "posterior"), "Version") )
local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", is_installed = function(pkg) FALSE, .package = "stanflow" ) local_mocked_bindings( available.packages = function(repos) fake_available, packageVersion = function(pkg) package_version("0"), .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { list(stanflow = c("cmdstanr", "posterior")) }, .package = "tools" )
deps <- stanflow_deps(recursive = FALSE, dev = TRUE)
expect_false(deps$behind[1]) expect_true(deps$behind[2]) expect_true(is.na(deps$remote[1]))})
test_that("stanflow_deps builds from description when check_updates = FALSE", { dep_calls <- list(recursive = NULL)
local_mocked_bindings( packageDescription = function(pkg) { list(Depends = "R", Imports = "foo, bar (>= 1.0)", Suggests = "baz") }, installed.packages = function() matrix(character(), nrow = 0), packageVersion = function(pkg) package_version("1.0.0"), .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { dep_calls$recursive <<- recursive list(foo = "qux") }, .package = "tools" ) local_mocked_bindings( is_installed = function(pkg) pkg == "foo", .package = "stanflow" )
deps <- stanflow_deps(recursive = TRUE, check_updates = FALSE)
expect_true(dep_calls$recursive) expect_true(all(is.na(deps$remote))) expect_false(any(deps$behind)) expect_true(all(c("foo", "bar", "baz", "qux") %in% deps$package))})
test_that("stanflow_deps returns direct deps when check_updates = FALSE and non-recursive", { local_mocked_bindings( packageDescription = function(pkg) { list(Depends = "R", Imports = "foo, bar (>= 1.0)", Suggests = "baz") }, .package = "utils" )
deps <- stanflow_deps(recursive = FALSE, check_updates = FALSE) expect_true(all(c("foo", "bar", "baz") %in% deps$package)) expect_false(any(grepl("qux", deps$package, fixed = TRUE)))})
test_that("stanflow_deps falls back when dependencies are empty", { fake_available <- matrix( c("1.0.0"), nrow = 1, dimnames = list(c("foo"), "Version") ) called <- list(primary = FALSE, fallback = FALSE)
local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", .package = "stanflow" ) local_mocked_bindings( available.packages = function(repos) fake_available, packageDescription = function(pkg) { list(Depends = "R", Imports = "foo", Suggests = "") }, .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { if (identical(pkgs, "stanflow")) { called$primary <<- TRUE return(list(stanflow = character())) } called$fallback <<- TRUE list(stanflow = "foo") }, .package = "tools" ) local_mocked_bindings( is_installed = function(pkg) FALSE, .package = "stanflow" )
deps <- stanflow_deps(recursive = FALSE, check_updates = TRUE)
expect_true(called$primary) expect_false(called$fallback) expect_equal(deps$package, "foo")})
test_that("stanflow_deps uses recursive fallback dependencies", { fake_available <- matrix( c("1.0.0", "1.0.0"), nrow = 2, dimnames = list(c("foo", "bar"), "Version") ) called <- list(primary = FALSE, fallback = FALSE)
local_mocked_bindings( stan_repos = function(dev) "https://fake.repo", .package = "stanflow" ) local_mocked_bindings( available.packages = function(repos) fake_available, packageDescription = function(pkg) { list(Depends = "R", Imports = "foo", Suggests = "") }, .package = "utils" ) local_mocked_bindings( package_dependencies = function(pkgs, db, recursive, ...) { if (identical(pkgs, "stanflow")) { called$primary <<- TRUE return(list(stanflow = character())) } called$fallback <<- TRUE list(foo = "bar") }, .package = "tools" ) local_mocked_bindings( is_installed = function(pkg) FALSE, .package = "stanflow" )
deps <- stanflow_deps(recursive = TRUE, check_updates = TRUE)
expect_true(called$primary) expect_true(called$fallback) expect_true(all(c("foo", "bar") %in% deps$package))})
test_that("stanflow_update aborts in non-interactive sessions", { withr::local_options(list( stanflow.testing = FALSE, stanflow.force_interactive = FALSE )) expect_error(stanflow_update(), "must be run interactively")})
test_that("stanflow_update surfaces transitive dependencies (loo -> matrixStats)", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") ))
recursive <- data.frame( package = c("cmdstanr", "posterior", "loo", "matrixStats"), remote = c("1.2.0", "1.6.0", "2.8.0", "1.3.0"), local = c("1.1.0", "1.5.0", "2.8.0", "1.2.8"), behind = c(TRUE, TRUE, FALSE, TRUE), stringsAsFactors = FALSE )
expect_snapshot_output( run_update_with( recursive, function() stanflow_update(recursive = TRUE), check = function(recursive_flag, dev_flag) { expect_true(recursive_flag) expect_false(dev_flag) } ) )
withr::local_output_sink(withr::local_tempfile()) result <- run_update_with( recursive, function() stanflow_update(recursive = TRUE), check = function(recursive_flag, dev_flag) { expect_true(recursive_flag) expect_false(dev_flag) } )
expect_identical(result, recursive[recursive$behind, , drop = FALSE])})
test_that("stanflow_update reports packages that need reinstall after warnings", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org"), stanflow.testing = FALSE, stanflow.force_interactive = TRUE ))
behind <- data.frame( package = c("cmdstanr", "posterior"), remote = c("1.2.0", "1.6.0"), local = c("1.1.0", "1.5.0"), behind = c(TRUE, TRUE), stringsAsFactors = FALSE )
expect_snapshot_output( with_mocked_bindings( stanflow_deps = function(...) behind, with_mocked_bindings( menu = function(...) 1, install.packages = function(...) { warning("cannot remove prior installation of package 'cmdstanr'") warning("some other warning") warning("cannot remove prior installation of package 'cmdstanr'") warning("cannot remove prior installation of package 'posterior'") }, suppressWarnings(stanflow_update()), .package = "utils" ), .package = "stanflow" ) )})
test_that("stanflow_update uses Stan universe when dev = TRUE", { withr::local_options(list( repos = c(CRAN = "https://cloud.r-project.org") ))
behind <- data.frame( package = c("cmdstanr", "posterior"), remote = c("1.2.0", "1.6.0"), local = c("1.1.0", "1.5.0"), behind = c(TRUE, TRUE), stringsAsFactors = FALSE )
expect_snapshot_output( run_update_with( behind, function() stanflow_update(dev = TRUE), check = function(recursive_flag, dev_flag) { expect_false(recursive_flag) expect_true(dev_flag) } ) )
withr::local_output_sink(withr::local_tempfile()) result <- run_update_with( behind, function() stanflow_update(dev = TRUE), check = function(recursive_flag, dev_flag) { expect_false(recursive_flag) expect_true(dev_flag) } )
expect_identical(result, behind)})