From e4fe08923f02081a5b62d61b10078268a9be3065 Mon Sep 17 00:00:00 2001 From: Craig Gower-Page Date: Mon, 25 Mar 2024 15:09:21 +0000 Subject: [PATCH] Add helpful instructions to direct user if cmdstanr is not installed (#292) --- DESCRIPTION | 4 ++-- R/JointModelSamples.R | 2 +- R/utilities.R | 4 ++++ R/zzz.R | 35 ++++++++++++++++++++++++++++++++++ man/JointModelSamples-class.Rd | 2 +- 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 85300e44..f5e90721 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,6 @@ Imports: checkmate, dplyr, stringr, - cmdstanr, ggplot2, ggplot2.utils, statmod, @@ -46,7 +45,8 @@ Imports: digest, posterior, stats -Suggests: +Suggests: + cmdstanr, bayesplot, knitr, rmarkdown, diff --git a/R/JointModelSamples.R b/R/JointModelSamples.R index e119535d..fef72be2 100644 --- a/R/JointModelSamples.R +++ b/R/JointModelSamples.R @@ -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 diff --git a/R/utilities.R b/R/utilities.R index f3ae0de2..ad7ad291 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -328,3 +328,7 @@ decompose_patients <- function(patients, all_pts) { indexes = patients_index ) } + +is_cmdstanr_available <- function() { + requireNamespace("cmdstanr", quietly = TRUE) +} diff --git a/R/zzz.R b/R/zzz.R index dd419b1a..6711b308 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -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 diff --git a/man/JointModelSamples-class.Rd b/man/JointModelSamples-class.Rd index b0086ba4..8d1678a7 100644 --- a/man/JointModelSamples-class.Rd +++ b/man/JointModelSamples-class.Rd @@ -16,6 +16,6 @@ Contains samples from a \code{\link{JointModel}}. \item{\code{data}}{(\code{\link{DataJoint}})\cr the data that the model was fitted on.} -\item{\code{results}}{(\code{\link{CmdStanMCMC}})\cr the STAN samples.} +\item{\code{results}}{(\code{\link[cmdstanr:CmdStanMCMC]{cmdstanr::CmdStanMCMC}})\cr the STAN samples.} }}