From 1e886a02a6f1ab1712c5eebb3139f9b34662b904 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sat, 18 May 2024 14:51:41 +0300 Subject: [PATCH] Cleanup more deprecations --- .github/workflows/R-CMD-check-wsl.yaml | 9 +- .github/workflows/R-CMD-check.yaml | 9 +- R/args.R | 23 +--- R/cmdstanr-package.R | 2 +- R/data.R | 8 -- R/example.R | 13 -- R/install.R | 28 +--- R/model.R | 182 +++++-------------------- R/run.R | 18 +-- R/utils.R | 70 ---------- R/zzz.R | 6 +- 11 files changed, 56 insertions(+), 312 deletions(-) diff --git a/.github/workflows/R-CMD-check-wsl.yaml b/.github/workflows/R-CMD-check-wsl.yaml index d7fa1bc8f..328718634 100644 --- a/.github/workflows/R-CMD-check-wsl.yaml +++ b/.github/workflows/R-CMD-check-wsl.yaml @@ -2,15 +2,12 @@ # Github Actions workflow to check CmdStanR # yamllint disable rule:line-length -name: Unit tests - WSL Backend +name: v1.0 Branch Unit tests - WSL Backend 'on': push: branches: - - master - pull_request: - branches: - - master + - v1.0 jobs: WSL-R-CMD-check: @@ -61,7 +58,7 @@ jobs: - name: Install cmdstan run: | cmdstanr::check_cmdstan_toolchain(fix = TRUE) - cmdstanr::install_cmdstan(cores = 2, wsl = TRUE, overwrite = TRUE) + cmdstanr::install_cmdstan(cores = 2, wsl = TRUE, overwrite = TRUE, version = "2.35.0-rc2") shell: Rscript {0} - name: Session info diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index c43b91e6c..294567c49 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,15 +2,12 @@ # Github Actions workflow to check CmdStanR # yamllint disable rule:line-length -name: Unit tests +name: v1.0 Branch Unit tests 'on': push: branches: - - master - pull_request: - branches: - - master + - v1.0 jobs: R-CMD-check: @@ -63,7 +60,7 @@ jobs: - name: Install cmdstan run: | cmdstanr::check_cmdstan_toolchain(fix = TRUE) - cmdstanr::install_cmdstan(cores = 2) + cmdstanr::install_cmdstan(cores = 2, version = "2.35.0-rc2") shell: Rscript {0} - name: Session info diff --git a/R/args.R b/R/args.R index 4c64d9c62..6d4bfd6bc 100644 --- a/R/args.R +++ b/R/args.R @@ -68,14 +68,8 @@ CmdStanArgs <- R6::R6Class( self$output_dir <- ifelse(is.null(output_dir), file.path(wsl_dir_prefix(), wsl_tempdir()), wsl_safe_path(output_dir)) - } else if (getRversion() < "3.5.0") { - self$output_dir <- output_dir %||% tempdir() } else { - if (getRversion() < "3.5.0") { - self$output_dir <- output_dir %||% tempdir() - } else { - self$output_dir <- output_dir %||% tempdir(check = TRUE) - } + self$output_dir <- output_dir %||% tempdir(check = TRUE) } self$output_dir <- repair_path(self$output_dir) self$output_basename <- output_basename @@ -698,9 +692,6 @@ validate_cmdstan_args <- function(self) { checkmate::assert_integerish(self$refresh, lower = 0, null.ok = TRUE) checkmate::assert_integerish(self$sig_figs, lower = 1, upper = 18, null.ok = TRUE) checkmate::assert_integerish(self$save_cmdstan_config, lower = 0, upper = 1, len = 1, null.ok = TRUE) - if (!is.null(self$sig_figs) && cmdstan_version() < "2.25") { - warning("The 'sig_figs' argument is only supported with cmdstan 2.25+ and will be ignored!", call. = FALSE) - } if (!is.null(self$refresh)) { self$refresh <- as.integer(self$refresh) } @@ -716,9 +707,6 @@ validate_cmdstan_args <- function(self) { validate_init(self$init, num_inits) validate_seed(self$seed, num_procs) if (!is.null(self$opencl_ids)) { - if (cmdstan_version() < "2.26") { - stop("Runtime selection of OpenCL devices is only supported with CmdStan version 2.26 or newer.", call. = FALSE) - } checkmate::assert_vector(self$opencl_ids, len = 2) } invisible(TRUE) @@ -836,9 +824,6 @@ validate_optimize_args <- function(self) { choices = c("bfgs", "lbfgs", "newton")) checkmate::assert_flag(self$jacobian, null.ok = TRUE) if (!is.null(self$jacobian)) { - if (cmdstan_version() < "2.32") { - warning("The 'jacobian' argument is only supported with cmdstan 2.32+ and will be ignored!", call. = FALSE) - } self$jacobian <- as.integer(self$jacobian) } @@ -1476,11 +1461,7 @@ validate_seed <- function(seed, num_procs) { if (is.null(seed)) { return(invisible(TRUE)) } - if (cmdstan_version() < "2.26") { - lower_seed <- 1 - } else { - lower_seed <- 0 - } + lower_seed <- 1 checkmate::assert_integerish(seed, lower = lower_seed) if (length(seed) > 1 && length(seed) != num_procs) { stop("If 'seed' is specified it must be a single integer or one per chain.", diff --git a/R/cmdstanr-package.R b/R/cmdstanr-package.R index 00455cabc..79ce25b9d 100644 --- a/R/cmdstanr-package.R +++ b/R/cmdstanr-package.R @@ -35,4 +35,4 @@ #' "_PACKAGE" -if (getRversion() >= "2.15.1") utils::globalVariables(c("self", "private", "super")) +utils::globalVariables(c("self", "private", "super")) diff --git a/R/data.R b/R/data.R index a1e4ab7c6..43bc098b4 100644 --- a/R/data.R +++ b/R/data.R @@ -156,14 +156,6 @@ process_data <- function(data, model_variables = NULL) { } else if (is.character(data)) { path <- absolute_path(data) } else if (is.list(data) && !is.data.frame(data)) { - if (cmdstan_version() < "2.22" && any_zero_dims(data)) { - stop( - "Data includes 0-dimensional data structures. To use this data please ", - "either update your CmdStan installation with install_cmdstan() ", - "or specify data as a file created by rstan::stan_rdump().", - call. = FALSE - ) - } if (!is.null(model_variables)) { data_variables <- model_variables$data is_data_supplied <- names(data_variables) %in% names(data) diff --git a/R/example.R b/R/example.R index 5bb98ae3e..085ac7910 100644 --- a/R/example.R +++ b/R/example.R @@ -185,16 +185,3 @@ write_stan_file <- function(code, } file } - - -#' Write Stan code to a temporary file -#' -#' This function is deprecated. Please use [write_stan_file()] instead. -#' @keywords internal -#' @export -#' @inheritParams write_stan_file -write_stan_tempfile <- function(code, dir = tempdir()) { - warning("write_stan_tempfile() is deprecated. Please use write_stan_file() instead.", - call. = FALSE) - write_stan_file(code, dir) -} diff --git a/R/install.R b/R/install.R index 847134e41..bb1c08def 100644 --- a/R/install.R +++ b/R/install.R @@ -136,7 +136,6 @@ install_cmdstan <- function(dir = NULL, warning("version and release_url shouldn't both be specified!", "\nrelease_url will be ignored.", call. = FALSE) } - release_url <- paste0("https://github.com/stan-dev/cmdstan/releases/download/v", version, "/cmdstan-", version, cmdstan_arch_suffix(version), ".tar.gz") } @@ -151,6 +150,10 @@ install_cmdstan <- function(dir = NULL, tar_name <- utils::tail(split_url[[1]], n = 1) tar_name <- gsub("-linux-(.*).tar.gz", ".tar.gz", tar_name) cmdstan_ver <- substr(tar_name, 0, nchar(tar_name) - 7) + if (cmdstan_ver < "2.35.0") { + warning("CmdStanR 1.0 and later only officially support CmdStan >= 2.35", + call. = FALSE) + } tar_gz_file <- paste0(cmdstan_ver, ".tar.gz") dir_cmdstan <- file.path(dir, cmdstan_ver) dest_file <- file.path(dir, tar_gz_file) @@ -224,15 +227,6 @@ install_cmdstan <- function(dir = NULL, ) } - # Building fails on Apple silicon with < v2.31 due to a makefiles setting - # for stanc3, so manually implement the patch if needed from: - # https://github.com/stan-dev/cmdstan/pull/1127 - stanc_makefile <- readLines(file.path(dir_cmdstan, "make", "stanc")) - stanc_makefile <- gsub("\\bxattr -d com.apple.quarantine bin/stanc", - "-xattr -d com.apple.quarantine bin/stanc", - stanc_makefile) - writeLines(stanc_makefile, con = file.path(dir_cmdstan, "make", "stanc")) - # Suppress noisy warnings from Boost cmdstan_make_local( dir = dir_cmdstan, @@ -321,12 +315,8 @@ cmdstan_make_local <- function(dir = cmdstan_path(), #' @rdname install_cmdstan #' @export -#' @param fix For `check_cmdstan_toolchain()`, should CmdStanR attempt to fix -#' any detected toolchain problems? Currently this option is only available on -#' Windows. The default is `FALSE`, in which case problems are only reported -#' along with suggested fixes. #' -check_cmdstan_toolchain <- function(fix = FALSE, quiet = FALSE) { +check_cmdstan_toolchain <- function(quiet = FALSE) { if (os_is_windows()) { if (os_is_wsl()) { check_wsl_toolchain() @@ -645,11 +635,6 @@ check_unix_cpp_compiler <- function() { cmdstan_arch_suffix <- function(version = NULL) { os_needs_arch <- os_is_linux() || os_is_wsl() - if ((!is.null(version) && version < "2.26") || !os_needs_arch) { - # pre-CmdStan 2.26, only the x86 tarball was provided - return(NULL) - } - arch <- NULL if (os_is_wsl()) { arch <- wsl_compatible_run(command = "uname", args = "-m")$stdout @@ -719,9 +704,6 @@ rtools4x_home_path <- function() { rtools_home_path <- function() { path <- NULL - if (R.version$major == "3") { - path <- Sys.getenv("RTOOLS_HOME") - } if (R.version$major == "4") { path <- rtools4x_home_path() } diff --git a/R/model.R b/R/model.R index c08e26a10..be04ca852 100644 --- a/R/model.R +++ b/R/model.R @@ -159,9 +159,7 @@ #' } #' cmdstan_model <- function(stan_file = NULL, exe_file = NULL, compile = TRUE, ...) { - if (cmdstan_version() < "2.27.0" && !is.null(exe_file)) { - stop("'exe_file' argument is only supported with CmdStan 2.27 and newer.", call. = FALSE) - } + if (is.null(exe_file) && is.null(stan_file)) { stop("Unable to create a `CmdStanModel` object. Both 'stan_file' and 'exe_file' are undefined.", call. = FALSE) } @@ -418,8 +416,6 @@ CmdStanModel <- R6::R6Class( #' @param compile_model_methods (logical) Compile additional model methods #' (`log_prob()`, `grad_log_prob()`, `constrain_variables()`, #' `unconstrain_variables()`). -#' @param compile_hessian_method (logical) Should the (experimental) `hessian()` method be -#' be compiled with the model methods? #' @param compile_standalone (logical) Should functions in the Stan model be #' compiled for use in R? If `TRUE` the functions will be available via the #' `functions` field in the compiled model object. This can also be done after @@ -428,9 +424,6 @@ CmdStanModel <- R6::R6Class( #' @param dry_run (logical) If `TRUE`, the code will do all checks before compilation, #' but skip the actual C++ compilation. Used to speedup tests. #' -#' @param threads Deprecated and will be removed in a future release. Please -#' turn on threading via `cpp_options = list(stan_threads = TRUE)` instead. -#' #' @return The `$compile()` method is called for its side effect of creating the #' executable and adding its path to the [`CmdStanModel`] object, but it also #' returns the [`CmdStanModel`] object invisibly. @@ -479,10 +472,7 @@ compile <- function(quiet = TRUE, force_recompile = getOption("cmdstanr_force_recompile", default = FALSE), compile_model_methods = FALSE, compile_standalone = FALSE, - dry_run = FALSE, - #deprecated - compile_hessian_method = FALSE, - threads = FALSE) { + dry_run = FALSE) { if (length(self$stan_file()) == 0) { stop("'$compile()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE) @@ -512,17 +502,6 @@ compile <- function(quiet = TRUE, } } - # temporary deprecation warnings - if (isTRUE(threads)) { - warning("'threads' is deprecated. Please use 'cpp_options = list(stan_threads = TRUE)' instead.") - cpp_options[["stan_threads"]] <- TRUE - } - - # temporary deprecation warnings - if (isTRUE(compile_hessian_method)) { - warning("'compile_hessian_method' is deprecated. The hessian method is compiled with all models.") - } - if (length(self$exe_file()) == 0) { if (is.null(dir)) { exe_base <- self$stan_file() @@ -787,9 +766,6 @@ CmdStanModel$set("public", name = "compile", value = compile) #' } #' variables <- function() { - if (cmdstan_version() < "2.27.0") { - stop("$variables() is only supported for CmdStan 2.27 or newer.", call. = FALSE) - } if (length(self$stan_file()) == 0) { stop("'$variables()' cannot be used because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE) } @@ -1008,18 +984,6 @@ format <- function(overwrite_file = FALSE, backup = TRUE, max_line_length = NULL, quiet = FALSE) { - if (cmdstan_version() < "2.29.0" && !is.null(max_line_length)) { - stop( - "'max_line_length' is only supported with CmdStan 2.29.0 or newer.", - call. = FALSE - ) - } - if (cmdstan_version() < "2.29.0" && !is.logical(canonicalize)) { - stop( - "A list can be supplied to the 'canonicalize' argument with CmdStan 2.29.0 or newer.", - call. = FALSE - ) - } if (length(self$stan_file()) == 0) { stop( "'$format()' cannot be used because the 'CmdStanModel'", @@ -1039,9 +1003,6 @@ format <- function(overwrite_file = FALSE, } if (isTRUE(canonicalize)) { stanc_options["print-canonical"] <- TRUE - if (cmdstan_version() < "2.29.0") { - stanc_options["auto-format"] <- NULL - } } else if (is.list(canonicalize) && length(canonicalize) > 0){ stanc_options["canonicalize"] <- paste0(canonicalize, collapse = ",") } @@ -1167,63 +1128,9 @@ sample <- function(data = NULL, show_messages = TRUE, show_exceptions = TRUE, diagnostics = c("divergences", "treedepth", "ebfmi"), - save_metric = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }, - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }, - # deprecated - cores = NULL, - num_cores = NULL, - num_chains = NULL, - num_warmup = NULL, - num_samples = NULL, - validate_csv = NULL, - save_extra_diagnostics = NULL, - max_depth = NULL, - stepsize = NULL) { - # temporary deprecation warnings - if (!is.null(cores)) { - warning("'cores' is deprecated. Please use 'parallel_chains' instead.") - parallel_chains <- cores - } - if (!is.null(num_cores)) { - warning("'num_cores' is deprecated. Please use 'parallel_chains' instead.") - parallel_chains <- num_cores - } - if (!is.null(num_chains)) { - warning("'num_chains' is deprecated. Please use 'chains' instead.") - chains <- num_chains - } - if (!is.null(num_warmup)) { - warning("'num_warmup' is deprecated. Please use 'iter_warmup' instead.") - iter_warmup <- num_warmup - } - if (!is.null(num_samples)) { - warning("'num_samples' is deprecated. Please use 'iter_sampling' instead.") - iter_sampling <- num_samples - } - if (!is.null(max_depth)) { - warning("'max_depth' is deprecated. Please use 'max_treedepth' instead.") - max_treedepth <- max_depth - } - if (!is.null(stepsize)) { - warning("'stepsize' is deprecated. Please use 'step_size' instead.") - step_size <- stepsize - } - if (!is.null(save_extra_diagnostics)) { - warning("'save_extra_diagnostics' is deprecated. Please use 'save_latent_dynamics' instead.") - save_latent_dynamics <- save_extra_diagnostics - } - if (!is.null(validate_csv)) { - warning("'validate_csv' is deprecated. Please use 'diagnostics' instead.") - if (is.logical(validate_csv)) { - if (validate_csv) { - diagnostics <- c("divergences", "treedepth", "ebfmi") - } else { - diagnostics <- NULL - } - } - } - - if (cmdstan_version() >= "2.27.0" && !fixed_param) { + save_metric = TRUE, + save_cmdstan_config = TRUE) { + if (!fixed_param) { if (self$has_stan_file() && file.exists(self$stan_file())) { if (!is.null(self$variables()) && length(self$variables()$parameters) == 0) { stop("Model contains no parameters. Please use 'fixed_param = TRUE'.", call. = FALSE) @@ -1379,21 +1286,7 @@ sample_mpi <- function(data = NULL, show_messages = TRUE, show_exceptions = TRUE, diagnostics = c("divergences", "treedepth", "ebfmi"), - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }, - # deprecated - validate_csv = TRUE) { - - if (!is.null(validate_csv)) { - warning("'validate_csv' is deprecated. Please use 'diagnostics' instead.") - if (is.logical(validate_csv)) { - if (validate_csv) { - diagnostics <- c("divergences", "treedepth", "ebfmi") - } else { - diagnostics <- NULL - } - } - } - + save_cmdstan_config = TRUE) { if (fixed_param) { chains <- 1 save_warmup <- FALSE @@ -1525,7 +1418,7 @@ optimize <- function(data = NULL, history_size = NULL, show_messages = TRUE, show_exceptions = TRUE, - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }) { + save_cmdstan_config = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, show_stderr_messages = show_exceptions, @@ -1659,10 +1552,7 @@ laplace <- function(data = NULL, draws = NULL, show_messages = TRUE, show_exceptions = TRUE, - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }) { - if (cmdstan_version() < "2.32") { - stop("This method is only available in cmdstan >= 2.32", call. = FALSE) - } + save_cmdstan_config = TRUE) { if (!is.null(mode) && !is.null(opt_args)) { stop("Cannot specify both 'opt_args' and 'mode' arguments.", call. = FALSE) } @@ -1815,7 +1705,7 @@ variational <- function(data = NULL, draws = NULL, show_messages = TRUE, show_exceptions = TRUE, - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }) { + save_cmdstan_config = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, show_stderr_messages = show_exceptions, @@ -1960,7 +1850,7 @@ pathfinder <- function(data = NULL, calculate_lp = NULL, show_messages = TRUE, show_exceptions = TRUE, - save_cmdstan_config = if (cmdstan_version() > "2.34.0") { TRUE } else { NULL }) { + save_cmdstan_config = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, show_stderr_messages = show_exceptions, @@ -2385,40 +2275,38 @@ model_variables <- function(stan_file, include_paths = NULL, allow_undefined = F model_compile_info <- function(exe_file) { info <- NULL - if (cmdstan_version() > "2.26.1") { - withr::with_path( - c( - toolchain_PATH_env_var(), - tbb_path() - ), - ret <- wsl_compatible_run( - command = wsl_safe_path(exe_file), - args = "info", - error_on_status = FALSE - ) + withr::with_path( + c( + toolchain_PATH_env_var(), + tbb_path() + ), + ret <- wsl_compatible_run( + command = wsl_safe_path(exe_file), + args = "info", + error_on_status = FALSE ) - if (ret$status == 0) { - info <- list() - info_raw <- strsplit(strsplit(ret$stdout, "\n")[[1]], "=") - for (key_val in info_raw) { - if (length(key_val) > 1) { - key_val <- trimws(key_val) - val <- key_val[2] - if (!is.na(as.logical(val))) { - val <- as.logical(val) - } - info[[toupper(key_val[1])]] <- val + ) + if (ret$status == 0) { + info <- list() + info_raw <- strsplit(strsplit(ret$stdout, "\n")[[1]], "=") + for (key_val in info_raw) { + if (length(key_val) > 1) { + key_val <- trimws(key_val) + val <- key_val[2] + if (!is.na(as.logical(val))) { + val <- as.logical(val) } + info[[toupper(key_val[1])]] <- val } - info[["STAN_VERSION"]] <- paste0(info[["STAN_VERSION_MAJOR"]], ".", info[["STAN_VERSION_MINOR"]], ".", info[["STAN_VERSION_PATCH"]]) - info[["STAN_VERSION_MAJOR"]] <- NULL - info[["STAN_VERSION_MINOR"]] <- NULL - info[["STAN_VERSION_PATCH"]] <- NULL } + info[["STAN_VERSION"]] <- paste0(info[["STAN_VERSION_MAJOR"]], ".", info[["STAN_VERSION_MINOR"]], ".", info[["STAN_VERSION_PATCH"]]) + info[["STAN_VERSION_MAJOR"]] <- NULL + info[["STAN_VERSION_MINOR"]] <- NULL + info[["STAN_VERSION_PATCH"]] <- NULL } info } is_variables_method_supported <- function(mod) { - cmdstan_version() >= "2.27.0" && mod$has_stan_file() && file.exists(mod$stan_file()) + mod$has_stan_file() && file.exists(mod$stan_file()) } diff --git a/R/run.R b/R/run.R index c766f733b..9c84c3628 100644 --- a/R/run.R +++ b/R/run.R @@ -23,13 +23,11 @@ CmdStanRun <- R6::R6Class( self$args <- args self$procs <- procs private$output_files_ <- self$new_output_files() - if (cmdstan_version() >= "2.26.0") { - private$profile_files_ <- self$new_profile_files() - } - if (cmdstan_version() >= "2.34.0" && !is.null(self$args$save_cmdstan_config) && self$args$save_cmdstan_config) { + private$profile_files_ <- self$new_profile_files() + if (isTRUE(self$args$save_cmdstan_config)) { private$config_files_ <- self$new_config_files() } - if (cmdstan_version() >= "2.34.0" && !is.null(self$args$method_args$save_metric) && self$args$method_args$save_metric) { + if (isTRUE(self$args$method_args$save_metric)) { private$metric_files_ <- self$new_metric_files() } if (self$args$save_latent_dynamics) { @@ -400,16 +398,12 @@ CmdStanRun <- R6::R6Class( self$output_files(include_failed = TRUE), if (self$args$save_latent_dynamics && !private$latent_dynamics_files_saved_) self$latent_dynamics_files(include_failed = TRUE), - if (cmdstan_version() > "2.25.0" && !private$profile_files_saved_) + if (!private$profile_files_saved_) private$profile_files_, - if (cmdstan_version() > "2.34.0" && - !is.null(self$args$save_cmdstan_config) && - self$args$save_cmdstan_config && + if (isTRUE(self$args$save_cmdstan_config) !private$config_files_saved_) self$config_files(include_failed = TRUE), - if (cmdstan_version() > "2.34.0" && - !(is.null(self$args$method_args$save_metric)) && - self$args$method_args$save_metric && + if (isTRUE(self$args$method_args$save_metric) && !private$metric_files_saved_) self$metric_files(include_failed = TRUE) ) diff --git a/R/utils.R b/R/utils.R index 342924c69..5cddc9e2e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -219,33 +219,6 @@ generate_file_names <- new_names } -# threading helpers (deprecated) ------------------------------------------ - - -#' Set or get the number of threads used to execute Stan models -#' -#' DEPRECATED. Please use the `threads_per_chain` argument when fitting the model. -#' @keywords internal -#' @name stan_threads -NULL - -#' @rdname stan_threads -#' @export -#' @return The value of the environment variable `STAN_NUM_THREADS`. -num_threads <- function() { - warning("'num_threads()' is deprecated. Please use the 'metadata()' method of the fit object to obtain the number of threads used.") - as.integer(Sys.getenv("STAN_NUM_THREADS")) -} - -#' @rdname stan_threads -#' @export -#' @param num_threads (positive integer) The number of threads to set. -set_num_threads <- function(num_threads) { - stop("Please use the 'threads_per_chain' argument in the $sample() method instead of set_num_threads().", - call. = FALSE) -} - - # hmc diagnostics ------------------------------------------------------ check_divergences <- function(post_warmup_sampler_diagnostics) { num_divergences_per_chain <- NULL @@ -826,42 +799,6 @@ get_function_name <- function(fun_start, fun_end, model_lines) { sub("\\(.*", "", fun_name, perl = TRUE) } -# Construct the plain return type for a standalone function by -# looking up the return type of the functor declaration and replacing -# the template types (i.e., T0__) with double -get_plain_rtn <- function(fun_start, fun_end, model_lines) { - fun_name <- get_function_name(fun_start, fun_end, model_lines) - - # Depending on the version of stanc3, the standalone functions - # with a plain return type can either be wrapped in a struct as a functor, - # or as a separate forward declaration - struct_name <- paste0("struct ", fun_name, "_functor") - - if (any(grepl(struct_name, model_lines))) { - struct_start <- grep(struct_name, model_lines) - struct_op_start <- grep("operator()", model_lines[-(1:struct_start)])[1] + struct_start - rtn_type <- paste0(model_lines[struct_start:struct_op_start], collapse = " ") - rm_operator <- gsub("operator().*", "", rtn_type) - rm_prev <- gsub(".*\\{", "", rm_operator) - } else { - # Find first declaration of function (will be the forward declaration) - first_decl <- grep(paste0(fun_name,"\\("), model_lines)[1] - - # The return type will be between the function name and the semicolon terminating - # the previous line - last_scolon <- grep(";", model_lines[1:first_decl]) - last_scolon <- ifelse(last_scolon[length(last_scolon)] == first_decl, - last_scolon[length(last_scolon) - 1], - last_scolon[length(last_scolon)]) - rtn_type_full <- paste0(model_lines[last_scolon:first_decl], collapse = " ") - rm_fun_name <- gsub(paste0(fun_name, ".*"), "", rtn_type_full) - rm_prev <- gsub(".*;", "", rm_fun_name) - } - rm_template <- gsub("template ", "", rm_prev) - gsub("T([0-9])*__", "double", rm_template) -} - - # Prepare the c++ code for a standalone function so that it can be exported to R: # - Replace the auto return type with the plain type # - Add Rcpp::export attribute @@ -870,9 +807,6 @@ get_plain_rtn <- function(fun_start, fun_end, model_lines) { # that instantiates an RNG prep_fun_cpp <- function(fun_start, fun_end, model_lines) { fun_body <- paste(model_lines[fun_start:fun_end], collapse = " ") - if (cmdstan_version() < "2.33") { - fun_body <- gsub("auto", get_plain_rtn(fun_start, fun_end, model_lines), fun_body) - } fun_body <- gsub("// [[stan::function]]", "// [[Rcpp::export]]\n", fun_body, fixed = TRUE) fun_body <- gsub("std::ostream\\*\\s*pstream__\\s*=\\s*nullptr", "", fun_body) if (grepl("(stan::rng_t|boost::ecuyer1988)", fun_body)) { @@ -966,10 +900,6 @@ expose_stan_functions <- function(function_env, global = FALSE, verbose = FALSE) stop("Exporting standalone functions is not possible with a pre-compiled Stan model!", call. = FALSE) } - if (function_env$external && cmdstan_version() < "2.32") { - stop("Exporting standalone functions with external C++ is not available before CmdStan 2.32", - call. = FALSE) - } if (!is.null(function_env$hpp_code) && !any(grepl("[[stan::function]]", function_env$hpp_code, fixed = TRUE))) { warning("No standalone functions found to compile and expose to R!", call. = FALSE) diff --git a/R/zzz.R b/R/zzz.R index 860eb8944..6160ca28e 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -71,11 +71,7 @@ cmdstanr_initialize <- function() { } } - if (getRversion() < "3.5.0") { - .cmdstanr$TEMP_DIR <- tempdir() - } else { - .cmdstanr$TEMP_DIR <- tempdir(check = TRUE) - } + .cmdstanr$TEMP_DIR <- tempdir(check = TRUE) invisible(TRUE) }