Skip to content

Commit 102663b

Browse files
authored
Merge pull request #413 from stan-dev/utils-cleanup
Utils cleanup
2 parents 46ae80f + cf68482 commit 102663b

File tree

6 files changed

+52
-63
lines changed

6 files changed

+52
-63
lines changed

.github/workflows/Test-coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747

4848
- name: Install dependencies
4949
run: |
50-
install.packages(c("posterior", "cmdstanr", "remotes"),repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
50+
install.packages(c("posterior", "cmdstanr", "remotes"),repos = c("https://mc-stan.org/r-packages/", getOption("repos")), dependencies = TRUE)
5151
cmdstanr::install_cmdstan(cores = 2, overwrite = TRUE, release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.25.0/cmdstan-2.25.0.tar.gz")
5252
remotes::install_deps(dependencies = TRUE)
5353
remotes::install_cran("covr")
@@ -95,7 +95,7 @@ jobs:
9595

9696
- name: Install dependencies
9797
run: |
98-
install.packages(c("posterior", "cmdstanr", "remotes"),repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
98+
install.packages(c("posterior", "cmdstanr", "remotes"),repos = c("https://mc-stan.org/r-packages/", getOption("repos")), dependencies = TRUE)
9999
cmdstanr::install_cmdstan(cores = 2, overwrite = TRUE, release_url = "https://github.com/stan-dev/cmdstan/releases/download/v2.25.0/cmdstan-2.25.0.tar.gz")
100100
remotes::install_deps(dependencies = TRUE)
101101
remotes::install_cran("covr")

R/fit.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,7 @@ CmdStanMCMC <- R6::R6Class(
861861
#' }
862862
#'
863863
loo <- function(variables = "log_lik", r_eff = TRUE, ...) {
864-
if (!requireNamespace("loo", quietly = TRUE)) {
865-
stop("Please install the loo package to use this method.", call. = FALSE)
866-
}
864+
suggest_package("loo")
867865
LLarray <- self$draws(variables)
868866
if (is.logical(r_eff)) {
869867
if (isTRUE(r_eff)) {

R/knitr.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@
3939
#' * [knitr's built-in Stan language engine](https://bookdown.org/yihui/rmarkdown/language-engines.html#stan)
4040
#'
4141
register_knitr_engine <- function(override = TRUE) {
42-
if (!requireNamespace("knitr", quietly = TRUE)) {
43-
stop("Please install the knitr package.", call. = FALSE)
44-
}
42+
suggest_package("knitr")
4543
if (override) {
4644
knitr::knit_engines$set(stan = eng_cmdstan)
4745
} else {
@@ -64,6 +62,7 @@ register_knitr_engine <- function(override = TRUE) {
6462
#' }
6563
#' @export
6664
eng_cmdstan <- function(options) {
65+
suggest_package("knitr")
6766
output_var <- options$output.var
6867
if (!is.character(output_var) || length(output_var) != 1L) {
6968
stop(

R/run.R

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ CmdStanRun <- R6::R6Class(
225225

226226

227227
# run helpers -------------------------------------------------
228+
check_target_exe <- function(exe) {
229+
exe_path <- file.path(cmdstan_path(), exe)
230+
if (!file.exists(exe_path)) {
231+
run_log <- processx::run(
232+
command = make_cmd(),
233+
args = exe,
234+
wd = cmdstan_path(),
235+
echo_cmd = TRUE,
236+
echo = TRUE,
237+
error_on_status = TRUE
238+
)
239+
}
240+
}
241+
228242
.run_sample <- function(mpi_cmd = NULL, mpi_args = NULL) {
229243
procs <- self$procs
230244
on.exit(procs$cleanup(), add = TRUE)
@@ -257,11 +271,11 @@ CmdStanRun <- R6::R6Class(
257271
if (is.null(mpi_n_process)) {
258272
start_msg <- paste0("Running MCMC with ", procs$num_procs(), " chains using MPI")
259273
} else {
260-
start_msg <- paste0("Running MCMC with ", procs$num_procs(), " chains using MPI with ", mpi_n_process, " processes")
261-
}
274+
start_msg <- paste0("Running MCMC with ", procs$num_procs(), " chains using MPI with ", mpi_n_process, " processes")
275+
}
262276
} else {
263277
start_msg <- paste0("Running MCMC with ", procs$num_procs(), " sequential chains")
264-
}
278+
}
265279
} else {
266280
start_msg <- paste0("Running MCMC with ", procs$num_procs(), " chains, at most ", procs$parallel_procs(), " in parallel")
267281
}
@@ -436,8 +450,8 @@ CmdStanRun$set("private", name = "run_generate_quantities_", value = .run_genera
436450
CmdStanRun$set("private", name = "run_optimize_", value = .run_other)
437451
CmdStanRun$set("private", name = "run_variational_", value = .run_other)
438452

439-
# CmdStanProcs ------------------------------------------------------------
440453

454+
# CmdStanProcs ------------------------------------------------------------
441455
# System processes for model fitting
442456
CmdStanProcs <- R6::R6Class(
443457
classname = "CmdStanProcs",
@@ -658,15 +672,15 @@ CmdStanProcs <- R6::R6Class(
658672
}
659673
if (private$proc_state_[[id]] == 3.5) {
660674
message(line)
661-
} else if ((private$show_stdout_messages_ && private$proc_state_[[id]] >= 3) || is_verbose_mode()) {
675+
} else if ((private$show_stdout_messages_ && private$proc_state_[[id]] >= 3) || is_verbose_mode()) {
662676
cat(line, collapse = "\n")
663677
}
664678
} else {
665679
# after the metadata is printed and we found a blank line
666680
# this represents the start of fitting
667681
if (self$proc_state(id) == 2.5) {
668682
self$set_proc_state(id, new_state = 3)
669-
}
683+
}
670684
}
671685
}
672686
invisible(self)
@@ -686,7 +700,7 @@ CmdStanProcs <- R6::R6Class(
686700
ret <- c(ret, self$get_proc(id)$get_exit_status())
687701
}
688702
ret
689-
}
703+
}
690704
),
691705
private = list(
692706
processes_ = NULL, # will be list of processx::process objects
@@ -885,7 +899,7 @@ CmdStanGQProcs <- R6::R6Class(
885899
# this represents the start of fitting
886900
if (self$proc_state(id) == 1.5) {
887901
self$set_proc_state(id, new_state = 2)
888-
}
902+
}
889903
}
890904
}
891905
invisible(self)

R/utils.R

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
# misc --------------------------------------------------------------------
22

3-
#' Check for Windows
4-
#' @return `TRUE` if OS is Windows, `FALSE` if not.
5-
#' @noRd
3+
suggest_package <- function(pkg) {
4+
if (!requireNamespace(pkg, quietly = TRUE)) {
5+
stop("Please install the ", pkg, " package to use this function.",
6+
call. = FALSE)
7+
}
8+
}
9+
10+
is_verbose_mode <- function() {
11+
getOption("cmdstanr_verbose", default = FALSE)
12+
}
13+
14+
# Famous helper for switching on `NULL` or zero length
15+
`%||%` <- function(x, y) {
16+
if (is.null(x) || length(x) == 0) y else x
17+
}
18+
19+
20+
# checks for OS and hardware ----------------------------------------------
21+
622
os_is_windows <- function() {
723
isTRUE(.Platform$OS.type == "windows")
824
}
925

10-
#' Check for macOS
11-
#' @return `TRUE` if OS is macOS, `FALSE` if not.
12-
#' @noRd
1326
os_is_macos <- function() {
1427
isTRUE(Sys.info()[["sysname"]] == "Darwin")
1528
}
1629

17-
is_verbose_mode <- function() {
18-
getOption("cmdstanr_verbose", default = FALSE)
19-
}
20-
21-
#' Check if running R in Rosetta 2 translation environment, which is an
22-
#' Intel-to-ARM translation layer.
23-
#' @return `TRUE` if it is running rosetta2, `FALSE` if not.
24-
#' @noRd
30+
# Check if running R in Rosetta 2 translation environment, which is an
31+
# Intel-to-ARM translation layer.
2532
is_rosetta2 <- function() {
2633
rosetta2 <- FALSE
2734
if (os_is_macos()) {
2835
rosetta2_check <- processx::run("/usr/sbin/sysctl",
29-
args = c("-n", "sysctl.proc_translated"),
30-
error_on_status = FALSE)
36+
args = c("-n", "sysctl.proc_translated"),
37+
error_on_status = FALSE)
3138
rosetta2 <- rosetta2_check$stdout == "1\n"
3239
}
3340
rosetta2
3441
}
3542

36-
#' Famous helper for switching on `NULL` or zero length
37-
#' @noRd
38-
#' @param x,y Any \R objects.
39-
#' @return `x` if not `NULL` or length zero, otherwise `y` regardless of whether
40-
#' `y` is `NULL`.
41-
`%||%` <- function(x, y) {
42-
if (is.null(x) || length(x) == 0) y else x
43-
}
44-
45-
#' Returns the type of make command to use to compile
46-
#' @return Returns "mingw32-make" if using cmdstan 2.21+ and running Windows
47-
#' @noRd
43+
# Returns the type of make command to use to compile depending on the OS
4844
make_cmd <- function() {
4945
# Cmdstan 2.21 introduced TBB that requires mingw32-make on Windows
5046
ver <- .cmdstanr$VERSION
@@ -55,9 +51,7 @@ make_cmd <- function() {
5551
}
5652
}
5753

58-
#' Returns the stanc exe path
59-
#' @return Returns "bin/stanc.exe" if running Windows, "bin/stanc" otherwise
60-
#' @noRd
54+
# Returns the stanc exe path depending on the OS
6155
stanc_cmd <- function() {
6256
if (os_is_windows()) {
6357
"bin/stanc.exe"
@@ -66,20 +60,6 @@ stanc_cmd <- function() {
6660
}
6761
}
6862

69-
check_target_exe <- function(exe) {
70-
exe_path <- file.path(cmdstan_path(), exe)
71-
if (!file.exists(exe_path)) {
72-
run_log <- processx::run(
73-
command = make_cmd(),
74-
args = exe,
75-
wd = cmdstan_path(),
76-
echo_cmd = TRUE,
77-
echo = TRUE,
78-
error_on_status = TRUE
79-
)
80-
}
81-
}
82-
8363

8464
# paths and extensions ----------------------------------------------------
8565

tests/testthat/test-knitr.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
context("knitr engine")
22

33
test_that("eng_cmdstan throws correct errors", {
4-
skip_on_cran()
54
skip_if_not_installed("knitr")
65
expect_error(eng_cmdstan(list(output.var = 1)), "must be a character string")
76
expect_error(eng_cmdstan(list(output.var = c("A", "B"))), "must be a character string")
@@ -23,7 +22,6 @@ test_that("eng_cmdstan works", {
2322
})
2423

2524
test_that("register_knitr_engine works with and without override", {
26-
skip_on_cran()
2725
skip_if_not_installed("knitr")
2826

2927
knitr::knit_engines$delete(keys = "stan")

0 commit comments

Comments
 (0)