Skip to content

Commit

Permalink
Add helpful instructions to direct user if cmdstanr is not installed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc authored Mar 25, 2024
1 parent 1db0fc4 commit e4fe089
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Imports:
checkmate,
dplyr,
stringr,
cmdstanr,
ggplot2,
ggplot2.utils,
statmod,
Expand All @@ -46,7 +45,8 @@ Imports:
digest,
posterior,
stats
Suggests:
Suggests:
cmdstanr,
bayesplot,
knitr,
rmarkdown,
Expand Down
2 changes: 1 addition & 1 deletion R/JointModelSamples.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ setOldClass("CmdStanMCMC")
#'
#' @slot model ([`JointModel`])\cr the model that the samples were drawn from.
#' @slot data ([`DataJoint`])\cr the data that the model was fitted on.
#' @slot results ([`CmdStanMCMC`])\cr the STAN samples.
#' @slot results ([`cmdstanr::CmdStanMCMC`])\cr the STAN samples.
#'
#' @aliases JointModelSamples
#' @export
Expand Down
4 changes: 4 additions & 0 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,7 @@ decompose_patients <- function(patients, all_pts) {
indexes = patients_index
)
}

is_cmdstanr_available <- function() {
requireNamespace("cmdstanr", quietly = TRUE)
}
35 changes: 35 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@
set_options()
}

.onAttach <- function(libname, pkgname) {
if (!is_cmdstanr_available()) {
packageStartupMessage(
"jmpost uses cmdstanr for compiling and sampling from models, but it does not seem to be installed.\n",
"To install:\n",
"install.packages(\"cmdstanr\", repos = c(\"https://mc-stan.org/r-packages/\", getOption(\"repos\")))"
)
} else if (is.null(cmdstanr::cmdstan_version(error_on_NA = FALSE))) {
possible_paths <- unique(c(
cmdstanr::cmdstan_default_install_path(),
Sys.getenv("CMDSTAN"),
Sys.getenv("CMDSTAN_PATH"),
"/root/.cmdstan",
"~/.cmdstan"
))
possible_paths <- possible_paths[dir.exists(possible_paths)]

if (length(possible_paths)) {
for (try_path in possible_paths) {
new_path <- tryCatch(
suppressMessages(cmdstanr::set_cmdstan_path(try_path)),
warning = function(w) NULL,
error = function(e) NULL
)
}
if (!is.null(new_path)) {
packageStartupMessage("CmdStan path set to: ", new_path)
}
} else {
packageStartupMessage("jmpost could not identify CmdStan path. Please use cmdstanr::set_cmdstan_path()")
}
}
return(invisible(NULL))
}


# This only exists to silence the false positive R CMD CHECK warning about
# importing but not using the posterior package. posterior is a dependency
Expand Down
2 changes: 1 addition & 1 deletion man/JointModelSamples-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e4fe089

Please sign in to comment.