Skip to content

Commit 6eee408

Browse files
authored
Merge pull request #643 from stan-dev/cmdstan-variable-refactor
Expand the use of CMDSTAN variable
2 parents 59af030 + a3d1d22 commit 6eee408

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

R/path.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
#'
2222
#' * If the [environment variable][Sys.setenv()] `"CMDSTAN"` exists at load time
2323
#' then its value will be automatically set as the default path to CmdStan for
24-
#' the \R session.
24+
#' the \R session. If the environment variable `"CMDSTAN"` is set, but a valid
25+
#' CmdStan is not found in the supplied path, the path is treated as a top
26+
#' folder that contains CmdStan installations. In that case, the CmdStan
27+
#' installation with the largest version number will be set as the path to
28+
#' CmdStan for the \R session.
2529
#' * If no environment variable is found when loaded but any directory in the
2630
#' form `".cmdstan/cmdstan-[version]"` (e.g., `".cmdstan/cmdstan-2.23.0"`),
2731
#' exists in the user's home directory (`Sys.getenv("HOME")`, *not* the current
@@ -120,11 +124,16 @@ cmdstan_default_install_path <- function(old = FALSE) {
120124
#' @export
121125
#' @keywords internal
122126
#' @param old See [cmdstan_default_install_path()].
127+
#' @param dir Path to a custom install folder with CmdStan installations.
123128
#' @return Path to the CmdStan installation with the most recent release
124129
#' version, or `NULL` if no installation found.
125130
#'
126-
cmdstan_default_path <- function(old = FALSE) {
127-
installs_path <- cmdstan_default_install_path(old)
131+
cmdstan_default_path <- function(old = FALSE, dir = NULL) {
132+
if (!is.null(dir)) {
133+
installs_path <- dir
134+
} else {
135+
installs_path <- cmdstan_default_install_path(old)
136+
}
128137
if (dir.exists(installs_path)) {
129138
cmdstan_installs <- list.dirs(path = installs_path, recursive = FALSE, full.names = FALSE)
130139
# if installed in cmdstan folder with no version move to cmdstan-version folder

R/zzz.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ cmdstanr_initialize <- function() {
3434
if (isTRUE(nzchar(path))) { # CMDSTAN environment variable found
3535
if (dir.exists(path)) {
3636
path <- absolute_path(path)
37-
suppressMessages(set_cmdstan_path(path))
37+
suppressWarnings(suppressMessages(set_cmdstan_path(path)))
38+
if (is.null(cmdstan_version(error_on_NA = FALSE))) {
39+
path <- cmdstan_default_path(dir = path)
40+
if (is.null(path)) {
41+
warning(
42+
"No CmdStan installation found in the path specified ",
43+
"by the environment variable 'CMDSTAN'.",
44+
call. = FALSE
45+
)
46+
.cmdstanr$PATH <- NULL
47+
} else {
48+
set_cmdstan_path(path)
49+
}
50+
}
3851
} else {
3952
warning(
4053
"Can't find directory specified by environment variable 'CMDSTAN'. ",

man/cmdstan_default_path.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cmdstan_model.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_cmdstan_path.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)