Skip to content

Commit 467054f

Browse files
committed
Merge branch 'master' into laplace-sample
2 parents e13fb1a + 22ee5fc commit 467054f

28 files changed

Lines changed: 291 additions & 409 deletions

.github/workflows/R-CMD-check-wsl.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
token: ${{ secrets.GITHUB_TOKEN }}
3636
if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
3737

38-
- uses: actions/checkout@v3
38+
- uses: actions/checkout@v4
3939

4040
- uses: r-lib/actions/setup-r@v2.6.4
4141
with:

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
token: ${{ secrets.GITHUB_TOKEN }}
4949
if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
5050

51-
- uses: actions/checkout@v3
51+
- uses: actions/checkout@v4
5252

5353
- name: Install system dependencies
5454
if: runner.os == 'Linux'

.github/workflows/Test-coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
token: ${{ secrets.GITHUB_TOKEN }}
3333
workflow: Test-coverage.yml
3434
if: "!startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/master'"
35-
- uses: actions/checkout@v3
35+
- uses: actions/checkout@v4
3636

3737
- uses: r-lib/actions/setup-r@v2.6.4
3838
- uses: r-lib/actions/setup-pandoc@v2.6.4
@@ -83,7 +83,7 @@ jobs:
8383
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
8484
NOT_CRAN: true
8585
steps:
86-
- uses: actions/checkout@v3
86+
- uses: actions/checkout@v4
8787

8888
- uses: r-lib/actions/setup-r@v2.6.4
8989
with:

.github/workflows/cmdstan-tarball-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
NOT_CRAN: true
3333

3434
steps:
35-
- uses: actions/checkout@v3
35+
- uses: actions/checkout@v4
3636
- name: Install system dependencies
3737
if: runner.os == 'Linux'
3838
run: |

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Authors@R:
1515
person(given = "Mike", family = "Lawrence", role = "ctb"),
1616
person(given = c("William", "Michael"), family = "Landau", role = "ctb",
1717
email = "will.landau@gmail.com", comment = c(ORCID = "0000-0003-1878-3253")),
18-
person(given = "Jacob", family = "Socolar", role = "ctb"))
18+
person(given = "Jacob", family = "Socolar", role = "ctb"),
19+
person(given = "Martin", family = "Modrák", role = "ctb"))
1920
Description: A lightweight interface to 'Stan' <https://mc-stan.org>.
2021
The 'CmdStanR' interface is an alternative to 'RStan' that calls the command
2122
line interface for compilation and running algorithms instead of interfacing
@@ -39,12 +40,12 @@ Imports:
3940
posterior (>= 1.4.1),
4041
processx (>= 3.5.0),
4142
R6 (>= 2.4.0),
42-
withr (>= 2.5.0)
43+
withr (>= 2.5.0),
44+
rlang (>= 0.4.7)
4345
Suggests:
4446
bayesplot,
4547
knitr (>= 1.37),
4648
loo (>= 2.0.0),
47-
rlang (>= 0.4.7),
4849
rmarkdown,
4950
testthat (>= 2.1.0),
5051
Rcpp,

R/example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ print_example_program <-
124124
#' stan_program <- "
125125
#' data {
126126
#' int<lower=0> N;
127-
#' int<lower=0,upper=1> y[N];
127+
#' array[N] int<lower=0,upper=1> y;
128128
#' }
129129
#' parameters {
130130
#' real<lower=0,upper=1> theta;

R/fit.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes)
11781178
#' mcmc_program <- write_stan_file(
11791179
#' 'data {
11801180
#' int<lower=0> N;
1181-
#' int<lower=0,upper=1> y[N];
1181+
#' array[N] int<lower=0,upper=1> y;
11821182
#' }
11831183
#' parameters {
11841184
#' real<lower=0,upper=1> theta;
@@ -1189,7 +1189,7 @@ CmdStanFit$set("public", name = "return_codes", value = return_codes)
11891189
#' }
11901190
#' }
11911191
#' generated quantities {
1192-
#' int y_rep[N];
1192+
#' array[N] int y_rep;
11931193
#' profile("gq") {
11941194
#' y_rep = bernoulli_rng(rep_vector(theta, N));
11951195
#' }

R/model.R

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ compile <- function(quiet = TRUE,
571571
}
572572

573573
if (!force_recompile) {
574-
if (interactive()) {
574+
if (rlang::is_interactive()) {
575575
message("Model executable is up to date!")
576576
}
577577
private$cpp_options_ <- cpp_options
@@ -582,7 +582,7 @@ compile <- function(quiet = TRUE,
582582
self$exe_file(exe)
583583
return(invisible(self))
584584
} else {
585-
if (interactive()) {
585+
if (rlang::is_interactive()) {
586586
message("Compiling Stan program...")
587587
}
588588
}
@@ -633,89 +633,92 @@ compile <- function(quiet = TRUE,
633633

634634
stancflags_val <- paste0("STANCFLAGS += ", stancflags_val, paste0(" ", stancflags_combined, collapse = " "))
635635

636-
if (dry_run) {
637-
return(invisible(self))
638-
}
636+
if (!dry_run) {
639637

640-
if (compile_standalone) {
641-
expose_stan_functions(self$functions, !quiet)
642-
}
638+
if (compile_standalone) {
639+
expose_stan_functions(self$functions, !quiet)
640+
}
643641

644-
withr::with_path(
645-
c(
646-
toolchain_PATH_env_var(),
647-
tbb_path()
648-
),
649-
run_log <- wsl_compatible_run(
650-
command = make_cmd(),
651-
args = c(wsl_safe_path(tmp_exe),
652-
cpp_options_to_compile_flags(cpp_options),
653-
stancflags_val),
654-
wd = cmdstan_path(),
655-
echo = !quiet || is_verbose_mode(),
656-
echo_cmd = is_verbose_mode(),
657-
spinner = quiet && interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
658-
stderr_callback = function(x, p) {
659-
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
660-
message(x)
661-
}
662-
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
663-
warning(
664-
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
665-
"If your model failed to compile please run rebuild_cmdstan().\n",
666-
"If the issue persists please open a bug report.",
667-
call. = FALSE
668-
)
669-
}
670-
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
671-
warning(
672-
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
673-
"See the above error for more details.",
674-
call. = FALSE
675-
)
676-
}
677-
if (os_is_macos()) {
678-
if (R.version$arch == "aarch64"
679-
&& grepl("but the current translation unit is being compiled for target", x)) {
642+
withr::with_path(
643+
c(
644+
toolchain_PATH_env_var(),
645+
tbb_path()
646+
),
647+
run_log <- wsl_compatible_run(
648+
command = make_cmd(),
649+
args = c(wsl_safe_path(tmp_exe),
650+
cpp_options_to_compile_flags(cpp_options),
651+
stancflags_val),
652+
wd = cmdstan_path(),
653+
echo = !quiet || is_verbose_mode(),
654+
echo_cmd = is_verbose_mode(),
655+
spinner = quiet && rlang::is_interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
656+
stderr_callback = function(x, p) {
657+
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
658+
message(x)
659+
}
660+
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
680661
warning(
681-
"The C++ compiler has errored due to incompatibility between the x86 and ",
682-
"Apple Silicon architectures.\n",
683-
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
684-
"make sure the IDE is a native Apple Silicon app.\n",
662+
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
663+
"If your model failed to compile please run rebuild_cmdstan().\n",
664+
"If the issue persists please open a bug report.",
685665
call. = FALSE
686666
)
687667
}
688-
}
689-
},
690-
error_on_status = FALSE
691-
)
692-
)
693-
if (is.na(run_log$status) || run_log$status != 0) {
694-
stop("An error occured during compilation! See the message above for more information.",
695-
call. = FALSE)
696-
}
697-
if (file.exists(exe)) {
698-
file.remove(exe)
699-
}
700-
file.copy(tmp_exe, exe, overwrite = TRUE)
701-
if (os_is_wsl()) {
702-
res <- processx::run(
703-
command = "wsl",
704-
args = c("chmod", "+x", wsl_safe_path(exe)),
705-
error_on_status = FALSE
668+
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
669+
warning(
670+
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
671+
"See the above error for more details.",
672+
call. = FALSE
673+
)
674+
}
675+
if (os_is_macos()) {
676+
if (R.version$arch == "aarch64"
677+
&& grepl("but the current translation unit is being compiled for target", x)) {
678+
warning(
679+
"The C++ compiler has errored due to incompatibility between the x86 and ",
680+
"Apple Silicon architectures.\n",
681+
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
682+
"make sure the IDE is a native Apple Silicon app.\n",
683+
call. = FALSE
684+
)
685+
}
686+
}
687+
},
688+
error_on_status = FALSE
689+
)
706690
)
707-
}
691+
if (is.na(run_log$status) || run_log$status != 0) {
692+
stop("An error occured during compilation! See the message above for more information.",
693+
call. = FALSE)
694+
}
695+
if (file.exists(exe)) {
696+
file.remove(exe)
697+
}
698+
file.copy(tmp_exe, exe, overwrite = TRUE)
699+
if (os_is_wsl()) {
700+
res <- processx::run(
701+
command = "wsl",
702+
args = c("chmod", "+x", wsl_safe_path(exe)),
703+
error_on_status = FALSE
704+
)
705+
}
706+
} # End - if(!dry_run)
707+
708708
private$exe_file_ <- exe
709709
private$cpp_options_ <- cpp_options
710710
private$precompile_cpp_options_ <- NULL
711711
private$precompile_stanc_options_ <- NULL
712712
private$precompile_include_paths_ <- NULL
713713
private$model_methods_env_ <- new.env()
714-
suppressWarnings(private$model_methods_env_$hpp_code_ <- readLines(private$hpp_file_, warn = FALSE))
715-
if (compile_model_methods) {
716-
expose_model_methods(env = private$model_methods_env_,
717-
verbose = !quiet,
718-
hessian = compile_hessian_method)
714+
715+
if(!dry_run) {
716+
suppressWarnings(private$model_methods_env_$hpp_code_ <- readLines(private$hpp_file_, warn = FALSE))
717+
if (compile_model_methods) {
718+
expose_model_methods(env = private$model_methods_env_,
719+
verbose = !quiet,
720+
hessian = compile_hessian_method)
721+
}
719722
}
720723
invisible(self)
721724
}
@@ -807,7 +810,7 @@ CmdStanModel$set("public", name = "variables", value = variables)
807810
#' file <- write_stan_file("
808811
#' data {
809812
#' int N;
810-
#' int y[N];
813+
#' array[N] int y;
811814
#' }
812815
#' parameters {
813816
#' // should have <lower=0> but omitting to demonstrate pedantic mode
@@ -877,7 +880,7 @@ check_syntax <- function(pedantic = FALSE,
877880
wd = cmdstan_path(),
878881
echo = is_verbose_mode(),
879882
echo_cmd = is_verbose_mode(),
880-
spinner = quiet && interactive(),
883+
spinner = quiet && rlang::is_interactive(),
881884
stderr_callback = function(x, p) {
882885
message(x)
883886
},
@@ -933,7 +936,7 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax)
933936
#' file <- write_stan_file("
934937
#' data {
935938
#' int N;
936-
#' int y[N];
939+
#' array[N] int y;
937940
#' }
938941
#' parameters {
939942
#' real lambda;
@@ -1829,7 +1832,7 @@ CmdStanModel$set("public", name = "variational", value = variational)
18291832
#' mcmc_program <- write_stan_file(
18301833
#' "data {
18311834
#' int<lower=0> N;
1832-
#' int<lower=0,upper=1> y[N];
1835+
#' array[N] int<lower=0,upper=1> y;
18331836
#' }
18341837
#' parameters {
18351838
#' real<lower=0,upper=1> theta;
@@ -1848,13 +1851,13 @@ CmdStanModel$set("public", name = "variational", value = variational)
18481851
#' gq_program <- write_stan_file(
18491852
#' "data {
18501853
#' int<lower=0> N;
1851-
#' int<lower=0,upper=1> y[N];
1854+
#' array[N] int<lower=0,upper=1> y;
18521855
#' }
18531856
#' parameters {
18541857
#' real<lower=0,upper=1> theta;
18551858
#' }
18561859
#' generated quantities {
1857-
#' int y_rep[N] = bernoulli_rng(rep_vector(theta, N));
1860+
#' array[N] int y_rep = bernoulli_rng(rep_vector(theta, N));
18581861
#' }"
18591862
#' )
18601863
#'

R/utils.R

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,12 @@ wsl_installed <- function() {
543543
tryCatch({
544544
# Call can hang indefinitely on Github actions, so explicitly kill
545545
p <- processx::process$new("wsl", "uname")
546-
Sys.sleep(1)
546+
for(i in 1:50) {
547+
Sys.sleep(0.1)
548+
if(!p$is_alive()) {
549+
break
550+
}
551+
}
547552
if (p$is_alive()) {
548553
p$kill()
549554
FALSE
@@ -816,7 +821,20 @@ get_standalone_hpp <- function(stan_file, stancflags) {
816821

817822
get_function_name <- function(fun_start, fun_end, model_lines) {
818823
fun_string <- paste(model_lines[(fun_start+1):fun_end], collapse = " ")
819-
fun_name <- gsub("auto ", "", fun_string, fixed = TRUE)
824+
types <- c(
825+
"auto",
826+
"int",
827+
"double",
828+
"Eigen::Matrix<(.*)>",
829+
"std::vector<(.*)>"
830+
)
831+
pattern <- paste0(
832+
# Only match if the type occurs at start of string
833+
"^(\\s*)?(",
834+
paste0(types, collapse="|"),
835+
# Only match if type followed by a function name and opening bracket
836+
")\\s*(?=\\w*\\()")
837+
fun_name <- gsub(pattern, "", fun_string, perl = TRUE)
820838
sub("\\(.*", "", fun_name, perl = TRUE)
821839
}
822840

@@ -864,7 +882,9 @@ get_plain_rtn <- function(fun_start, fun_end, model_lines) {
864882
# that instantiates an RNG
865883
prep_fun_cpp <- function(fun_start, fun_end, model_lines) {
866884
fun_body <- paste(model_lines[fun_start:fun_end], collapse = " ")
867-
fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body)
885+
if (cmdstan_version() < "2.33") {
886+
fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body)
887+
}
868888
fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE)
869889
fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body)
870890
fun_body <- gsub("boost::ecuyer1988&\\s*base_rng__", "SEXP base_rng_ptr", fun_body)

_pkgdown.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ articles:
7272
- cmdstanr-internals
7373
- posterior
7474
- r-markdown
75-
- deprecations
7675
- profiling
7776
- articles-online-only/opencl
7877

0 commit comments

Comments
 (0)