From 2dc073cbd85a5e623a1647b606fa01cdd8f4c989 Mon Sep 17 00:00:00 2001 From: wlandau-lilly Date: Tue, 9 Jan 2024 09:38:50 -0500 Subject: [PATCH] Sketch #16 --- .github/workflows/check-cran.yaml | 10 --- .github/workflows/check-fixed.yaml | 1 - ...heck-cmdstanr.yaml => check-implicit.yaml} | 1 - .github/workflows/install-internal.yaml | 57 --------------- .github/workflows/lint.yaml | 6 -- DESCRIPTION | 17 ++--- R/stan_cmdstan_path.R | 36 ++++------ R/stan_cmdstan_version.R | 6 +- R/stan_package.R | 11 ++- R/stan_package_compile.R | 6 +- R/stan_package_model.R | 6 +- R/utils_assert.R | 6 +- R/utils_cmdstan.R | 6 +- README.Rmd | 20 +++--- README.md | 69 ++++++++----------- man/instantiate-package.Rd | 11 ++- man/stan_cmdstan_exists.Rd | 8 +-- man/stan_cmdstan_path.Rd | 8 +-- man/stan_cmdstan_version.Rd | 8 +-- man/stan_package_compile.Rd | 8 +-- man/stan_package_model.Rd | 8 +-- src/install.libs.R | 38 ++-------- 22 files changed, 94 insertions(+), 253 deletions(-) rename .github/workflows/{check-cmdstanr.yaml => check-implicit.yaml} (96%) delete mode 100644 .github/workflows/install-internal.yaml diff --git a/.github/workflows/check-cran.yaml b/.github/workflows/check-cran.yaml index 02e507e..a170dab 100644 --- a/.github/workflows/check-cran.yaml +++ b/.github/workflows/check-cran.yaml @@ -33,19 +33,9 @@ jobs: - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} - extra-repositories: 'https://mc-stan.org/r-packages/' http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - - name: Repos - run: | - cat("\noptions(repos=c(stan='https://mc-stan.org/r-packages/',CRAN ='https://cloud.r-project.org'))\n", file = "~/.Rprofile", append = TRUE) - shell: Rscript {0} - - - name: Remove cmdstanr - shell: Rscript {0} - run: try(remove.packages("cmdstanr"), silent = TRUE) - - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: any::rcmdcheck, local::. diff --git a/.github/workflows/check-fixed.yaml b/.github/workflows/check-fixed.yaml index c7651bf..dfd83c0 100644 --- a/.github/workflows/check-fixed.yaml +++ b/.github/workflows/check-fixed.yaml @@ -29,7 +29,6 @@ jobs: - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} - extra-repositories: 'https://mc-stan.org/r-packages/' http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true diff --git a/.github/workflows/check-cmdstanr.yaml b/.github/workflows/check-implicit.yaml similarity index 96% rename from .github/workflows/check-cmdstanr.yaml rename to .github/workflows/check-implicit.yaml index f3eab67..c4abba1 100644 --- a/.github/workflows/check-cmdstanr.yaml +++ b/.github/workflows/check-implicit.yaml @@ -34,7 +34,6 @@ jobs: with: r-version: ${{ matrix.config.r }} rtools-version: '42' - extra-repositories: 'https://mc-stan.org/r-packages/' http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true diff --git a/.github/workflows/install-internal.yaml b/.github/workflows/install-internal.yaml deleted file mode 100644 index 2f4bcfd..0000000 --- a/.github/workflows/install-internal.yaml +++ /dev/null @@ -1,57 +0,0 @@ -name: install-internal - -on: - push: - branches: - - main - -jobs: - install-internal: - runs-on: ubuntu-latest - - env: - GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - R_KEEP_PKG_SOURCE: yes - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - - - name: Change the script so CmdStan always installs internally. - run: sed -i '1s/.*/choice <- "internal"/' src/install.libs.R - - - name: Remove GitHub Actions workflow files - run: rm -rf .github/workflows - - - name: Deploy to install-internal branch - if: github.event_name != 'pull_request' - uses: JamesIves/github-pages-deploy-action@v4.4.3 - with: - clean: false - branch: install-internal - folder: . - - - uses: r-lib/actions/setup-r@v2 - with: - use-public-rspm: true - extra-repositories: 'https://mc-stan.org/r-packages/' - - - name: Build instantiate tarball - run: R CMD build . - - - name: Find tar.gz as artifact - run: | - TAR_PATH=$(find . -name "instantiate*.tar.gz" | head -n 1) - if [[ -n "$TAR_PATH" ]]; then - echo "Found tarball at $TAR_PATH" - echo "TAR_PATH=$TAR_PATH" >> $GITHUB_ENV - else - echo "No tarball found." - exit 1 - fi - - - name: Upload tar.gz artifact - uses: actions/upload-artifact@v3.1.2 - with: - name: install-internal - path: ${{ env.TAR_PATH }} diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 9906bdc..d14d6e1 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -15,12 +15,6 @@ jobs: - uses: r-lib/actions/setup-r@v2 with: use-public-rspm: true - extra-repositories: 'https://mc-stan.org/r-packages/' - - - name: Repos - run: | - cat("\noptions(repos=c(stan='https://mc-stan.org/r-packages/',CRAN ='https://cloud.r-project.org'))\n", file = "~/.Rprofile", append = TRUE) - shell: Rscript {0} - uses: r-lib/actions/setup-r-dependencies@v2 with: diff --git a/DESCRIPTION b/DESCRIPTION index 52952cb..0f733e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,16 +6,12 @@ Description: Similar to 'rstantools' for 'rstan', The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of - 'cmdstanr' without any additional compilation. - This approach saves time, allows R package developers - to migrate from 'rstan' to the more modern - 'cmdstanr', and fits well with centrally maintained - R installations where users have trouble installing - their own packages, diagnosing compilation errors, - and setting environment variables. + 'cmdstanr' without any additional compilation after package installation. + This approach saves time and helps R package developers + migrate from 'rstan' to the more modern 'cmdstanr'. Packages 'rstantools', 'cmdstanr', 'stannis', and 'stanapi' are similar Stan clients with different objectives. -Version: 0.1.0.9000 +Version: 0.2.0 License: MIT + file LICENSE URL: https://wlandau.github.io/instantiate/, https://github.com/wlandau/instantiate @@ -39,16 +35,11 @@ Imports: rlang, utils Suggests: - cmdstanr (>= 0.5.2), knitr (>= 1.30), markdown (>= 1.1), rmarkdown (>= 2.4), testthat (>= 3.0.0), withr -Additional_repositories: - https://mc-stan.org/r-packages/ -SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) -StagedInstall: no Encoding: UTF-8 Language: en-US Config/testthat/edition: 3 diff --git a/R/stan_cmdstan_path.R b/R/stan_cmdstan_path.R index cebbf5f..7136974 100644 --- a/R/stan_cmdstan_path.R +++ b/R/stan_cmdstan_path.R @@ -12,8 +12,8 @@ #' 1. `""` (default): look at the original value that the `CMDSTAN_INSTALL` #' environment variable contained when `instantiate` at the time #' when it was installed. -#' If it was `"implicit"`, `"fixed"`, or `"internal"`, then choose -#' the corresponding option below. Otherwise, default to `"internal"`. +#' If it was `"implicit"` or `"fixed"`, then choose +#' the corresponding option below. Otherwise, default to `"implicit"`. #' 2. `"implicit"`: Let the `cmdstanr::cmdstan_path()` decide where #' to look for CmdStan. As explained in the `cmdstanr` documentation, #' the output of `cmdstanr::cmdstan_path()` depends @@ -24,32 +24,20 @@ #' 3. `"fixed"`: Use the path to CmdStan that was originally #' contained in `Sys.getenv("CMDSTAN")` at the time when `instantiate` #' was installed. -#' 4. `"internal"`: Use the copy of CmdStan installed internally inside the -#' `instantiate` package file system. To use this option, -#' `instantiate` needs to have been originally installed with the -#' `CMDSTAN_INSTALL` environment variable set to `"internal"`. #' @examples #' stan_cmdstan_path() stan_cmdstan_path <- function( cmdstan_install = Sys.getenv("CMDSTAN_INSTALL", unset = "") ) { - # It is not feasible to test all these cases in a single test coverage run. - # Covered in several GitHub Actions workflows. - # nocov start - install <- cmdstan_install - if (identical(install, "")) { - install <- .Call(c_cmdstan_path_install, PACKAGE = "instantiate") - } + install <- if_any( + identical(cmdstan_install, ""), + .Call(c_cmdstan_path_install, PACKAGE = "instantiate"), + cmdstan_install + ) stan_assert_install(install) - if (identical(install, "implicit") || identical(install, "")) { - out <- cmdstanr_path() - } else if (identical(install, "fixed")) { - out <- .Call(c_cmdstan_path_fixed, PACKAGE = "instantiate") - } else if (identical(install, "internal")) { - bin <- system.file("bin", package = "instantiate", mustWork = FALSE) - parent <- file.path(bin, "cmdstan") - out <- file.path(parent, list.files(parent)) %||% "" - } - # nocov end - out + if_any( + identical(install, "fixed"), + .Call(c_cmdstan_path_fixed, PACKAGE = "instantiate"), + cmdstanr_path() + ) } diff --git a/R/stan_cmdstan_version.R b/R/stan_cmdstan_version.R index 4f74d5e..7964778 100644 --- a/R/stan_cmdstan_version.R +++ b/R/stan_cmdstan_version.R @@ -19,9 +19,9 @@ stan_cmdstan_version <- function( stan_assert_cmdstanr() path_old <- cmdstanr_path() if (cmdstan_valid(path_old)) { - on.exit(suppressMessages(cmdstanr::set_cmdstan_path(path = path_old))) + on.exit(suppressMessages(cmdstanr("set_cmdstan_path")(path = path_old))) } path_new <- stan_cmdstan_path(cmdstan_install = cmdstan_install) - suppressMessages(cmdstanr::set_cmdstan_path(path = path_new)) - cmdstanr::cmdstan_version(error_on_NA = error_on_NA) + suppressMessages(cmdstanr("set_cmdstan_path")(path = path_new)) + cmdstanr("cmdstan_version")(error_on_NA = error_on_NA) } diff --git a/R/stan_package.R b/R/stan_package.R index a473b5b..3861730 100644 --- a/R/stan_package.R +++ b/R/stan_package.R @@ -11,13 +11,10 @@ #' the executables live inside the file systems of their #' respective packages, and users have the full power #' and convenience of [`CmdStanR`](https://mc-stan.org/cmdstanr/) -#' without any additional compilation. This approach saves time, -#' allows R package developers to migrate from -#' [`rstan`](https://mc-stan.org/rstan/) -#' to the more modern [`CmdStanR`](https://mc-stan.org/cmdstanr/), -#' and fits well with centrally maintained R installations -#' where users may have trouble installing their own packages, -#' diagnosing compilation errors, and setting environment variables. +#' without any additional compilation after package installation. +#' This approach saves time and helps R package developers +#' migrate from [`rstan`](https://mc-stan.org/rstan/) +#' to the more modern [`CmdStanR`](https://mc-stan.org/cmdstanr/). #' @useDynLib instantiate, .registration = TRUE #' @importFrom fs dir_copy #' @importFrom rlang abort check_installed inform is_installed warn diff --git a/R/stan_package_compile.R b/R/stan_package_compile.R index 00a2c1c..8081dd8 100644 --- a/R/stan_package_compile.R +++ b/R/stan_package_compile.R @@ -79,10 +79,10 @@ stan_package_compile <- function( } path_old <- cmdstanr_path() if (cmdstan_valid(path_old)) { - on.exit(suppressMessages(cmdstanr::set_cmdstan_path(path = path_old))) + on.exit(suppressMessages(cmdstanr("set_cmdstan_path")(path = path_old))) } path_new <- stan_cmdstan_path(cmdstan_install = cmdstan_install) - suppressMessages(cmdstanr::set_cmdstan_path(path = path_new)) + suppressMessages(cmdstanr("set_cmdstan_path")(path = path_new)) lapply( X = models, FUN = stan_compile_model, @@ -118,7 +118,7 @@ stan_compile_model <- function( on.exit(options("cmdstanr_verbose" = old)) options("cmdstanr_verbose" = TRUE) } - cmdstanr::cmdstan_model( + cmdstanr("cmdstan_model")( stan_file = model, compile = TRUE, quiet = quiet, diff --git a/R/stan_package_model.R b/R/stan_package_model.R index b360a08..497647a 100644 --- a/R/stan_package_model.R +++ b/R/stan_package_model.R @@ -64,11 +64,11 @@ stan_package_model <- function( exe_file <- if_any(stan_on_windows(), paste0(exe_file, ".exe"), exe_file) path_old <- cmdstanr_path() if (cmdstan_valid(path_old)) { - on.exit(suppressMessages(cmdstanr::set_cmdstan_path(path = path_old))) + on.exit(suppressMessages(cmdstanr("set_cmdstan_path")(path = path_old))) } path_new <- stan_cmdstan_path(cmdstan_install = cmdstan_install) - suppressMessages(cmdstanr::set_cmdstan_path(path = path_new)) - cmdstanr::cmdstan_model( + suppressMessages(cmdstanr("set_cmdstan_path")(path = path_new)) + cmdstanr("cmdstan_model")( stan_file = stan_file, exe_file = exe_file, compile = FALSE diff --git a/R/utils_assert.R b/R/utils_assert.R index 435568d..e08b2a2 100644 --- a/R/utils_assert.R +++ b/R/utils_assert.R @@ -25,8 +25,8 @@ stan_assert <- function( stan_assert_install <- function(install) { message <- paste( - "The CMDSTAN_INSTALL environment variable and the install argument", - "must be \"implicit\", \"fixed\", \"internal\", \"\", or unset", + "The CMDSTAN_INSTALL environment variable and the cmdstan_install", + "argument must be \"implicit\", \"fixed\", \"\", or unset", "both when {instantiate} is installed and when {instantiate} is used.", "If you are using {instantiate} and you get this error even though", "CMDSTAN_INSTALL is an allowable value, please reinstall {instantiate}", @@ -40,7 +40,7 @@ stan_assert_install <- function(install) { message = message ) stan_assert( - install %in% c("implicit", "fixed", "internal", ""), + install %in% c("implicit", "fixed", ""), message = message ) invisible() diff --git a/R/utils_cmdstan.R b/R/utils_cmdstan.R index 11c5048..6fc4c6b 100644 --- a/R/utils_cmdstan.R +++ b/R/utils_cmdstan.R @@ -1,7 +1,11 @@ +cmdstanr <- function(name) { + eval(parse(text = paste0("cmdstanr::", name))) +} + cmdstanr_path <- function() { if_any( rlang::is_installed("cmdstanr"), - tryCatch(cmdstanr::cmdstan_path(), error = function(condition) ""), + tryCatch(cmdstanr("cmdstan_path")(), error = function(condition) ""), "" ) } diff --git a/README.Rmd b/README.Rmd index 80ffd0a..6e9beb3 100644 --- a/README.Rmd +++ b/README.Rmd @@ -22,7 +22,7 @@ knitr::opts_chunk$set( [![codecov](https://codecov.io/gh/wlandau/instantiate/branch/main/graph/badge.svg)](https://app.codecov.io/gh/wlandau/instantiate) [![lint](https://github.com/wlandau/instantiate/workflows/lint/badge.svg)](https://github.com/wlandau/instantiate/actions?query=workflow%3Alint) -Similar to [`rstantools`](https://mc-stan.org/rstantools/) for [`rstan`](https://mc-stan.org/rstan/), the `instantiate` package builds pre-compiled [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) models into CRAN-ready statistical modeling R packages. The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of [`CmdStanR`](https://mc-stan.org/cmdstanr/) without any additional compilation. This approach saves time, allows R package developers to migrate from [`rstan`](https://mc-stan.org/rstan/) to the more modern [`CmdStanR`](https://mc-stan.org/cmdstanr/), and fits well with centrally maintained R installations where users may have trouble installing their own packages, diagnosing compilation errors, and setting environment variables. +Similar to [`rstantools`](https://mc-stan.org/rstantools/) for [`rstan`](https://mc-stan.org/rstan/), the `instantiate` package builds pre-compiled [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) models into CRAN-ready statistical modeling R packages. The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of [`CmdStanR`](https://mc-stan.org/cmdstanr/) without any additional compilation after package installation. This approach saves time and helps R package developers migrate from [`rstan`](https://mc-stan.org/rstan/) to the more modern [`CmdStanR`](https://mc-stan.org/cmdstanr/). # Documentation @@ -51,20 +51,18 @@ The `instantiate` package uses environment variables to manage the installation # Administering CmdStan -With `instantiate`, you have three different options for managing [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). These options are called "installation types". To choose an installation type, set the `CMDSTAN_INSTALL` environment variable before installing `instantiate`. Your choice determines +By default, `instantiate` looks for the copy of [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) located at `cmdstanr::install_cmdstan()`. If you upgrade [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan), then the path returned by `cmdstanr::install_cmdstan()` will change, which may not be desirable in some cases. To permanently lock the path that `instantiate` uses, follow these steps: -1. Whether [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) will be installed inside `instantiate` during `install.packages("instantiate")`. -2. The location where `instantiate` will automatically look for [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) at runtime. +1. Set the `CMDSTAN` environment variable to the desired path to [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). +2. Set the `CMDSTAN_INSTALL` environment variable to `"fixed"`. +3. Install `instantiate`. -The following table describes the role of the `CMDSTAN_INSTALL` environment variable at installation time (i.e. during `install.packages("instantiate")`). Implicit installation is recommended for most situations. +Henceforth, `instantiate` will automatically use the [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) path from (1), regardless of the value of `CMDSTAN` after (3). To prefer `cmdstanr::cmdstan_path()` instead, you could do one of the following: -Installation type | `CMDSTAN_INSTALL` | Install CmdStan? | Default path to CmdStan at runtime ----|---|---|--- -Implicit (default) | `"implicit"` or `""` or unset | No | `cmdstanr::cmdstan_path()`, which depends on the current value of the `CMDSTAN` environment value at runtime. -Fixed | `"fixed"` | No | The value that the `CMDSTAN` environment variable originally contained at installation time, regardless of the current value of `CMDSTAN` at runtime. -Internal | `"internal"` | Yes | The internal copy of [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) inside `instantiate`. +* Reinstall `instantiate` with `CMDSTAN_INSTALL` not equal to `"fixed"`, or +* Set `CMDSTAN_INSTALL` to `"implicit"` at runtime, or +* Set the `cmdstan_install` argument to `"implicit"` for the current `instantiate` package function you are using. -At runtime `instantiate`, the `CMDSTAN_INSTALL` environment variable takes on a different role: it overrides the automatic preference that `instantiate` has for the path to [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). (So does the `cmdstan_install` function argument.) For example, if you installed `instantiate` with `CMDSTAN_INSTALL` equal `"internal"`, you can set `Sys.setenv(CMDSTAN_INSTALL = "implicit")` at runtime to let `cmdstanr::cmdstan_path()` choose the path to [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). You can always choose implicit or fixed installation, but you can only choose internal installation if `instantiate` was installed with an internal copy of [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). # Packaging Stan models diff --git a/README.md b/README.md index c22e77a..47afc31 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,10 @@ models into CRAN-ready statistical modeling R packages. The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of [`CmdStanR`](https://mc-stan.org/cmdstanr/) without any -additional compilation. This approach saves time, allows R package -developers to migrate from [`rstan`](https://mc-stan.org/rstan/) to the -more modern [`CmdStanR`](https://mc-stan.org/cmdstanr/), and fits well -with centrally maintained R installations where users may have trouble -installing their own packages, diagnosing compilation errors, and -setting environment variables. +additional compilation after package installation. This approach saves +time and helps R package developers migrate from +[`rstan`](https://mc-stan.org/rstan/) to the more modern +[`CmdStanR`](https://mc-stan.org/cmdstanr/). # Documentation @@ -73,42 +71,29 @@ variables: # Administering CmdStan -With `instantiate`, you have three different options for managing -[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). These options -are called “installation types”. To choose an installation type, set the -`CMDSTAN_INSTALL` environment variable before installing `instantiate`. -Your choice determines - -1. Whether [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) - will be installed inside `instantiate` during - `install.packages("instantiate")`. -2. The location where `instantiate` will automatically look for - [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) at - runtime. - -The following table describes the role of the `CMDSTAN_INSTALL` -environment variable at installation time (i.e. during -`install.packages("instantiate")`). Implicit installation is recommended -for most situations. - -| Installation type | `CMDSTAN_INSTALL` | Install CmdStan? | Default path to CmdStan at runtime | -|--------------------|-------------------------------|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------| -| Implicit (default) | `"implicit"` or `""` or unset | No | `cmdstanr::cmdstan_path()`, which depends on the current value of the `CMDSTAN` environment value at runtime. | -| Fixed | `"fixed"` | No | The value that the `CMDSTAN` environment variable originally contained at installation time, regardless of the current value of `CMDSTAN` at runtime. | -| Internal | `"internal"` | Yes | The internal copy of [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) inside `instantiate`. | - -At runtime `instantiate`, the `CMDSTAN_INSTALL` environment variable -takes on a different role: it overrides the automatic preference that -`instantiate` has for the path to -[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). (So does the -`cmdstan_install` function argument.) For example, if you installed -`instantiate` with `CMDSTAN_INSTALL` equal `"internal"`, you can set -`Sys.setenv(CMDSTAN_INSTALL = "implicit")` at runtime to let -`cmdstanr::cmdstan_path()` choose the path to -[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). You can -always choose implicit or fixed installation, but you can only choose -internal installation if `instantiate` was installed with an internal -copy of [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). +By default, `instantiate` looks for the copy of +[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) located at +`cmdstanr::install_cmdstan()`. If you upgrade +[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan), then the path +returned by `cmdstanr::install_cmdstan()` will change, which may not be +desirable in some cases. To permanently lock the path that `instantiate` +uses, follow these steps: + +1. Set the `CMDSTAN` environment variable to the desired path to + [`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan). +2. Set the `CMDSTAN_INSTALL` environment variable to `"fixed"`. +3. Install `instantiate`. + +Henceforth, `instantiate` will automatically use the +[`CmdStan`](https://mc-stan.org/users/interfaces/cmdstan) path from (1), +regardless of the value of `CMDSTAN` after (3). To prefer +`cmdstanr::cmdstan_path()` instead, you could do one of the following: + +- Reinstall `instantiate` with `CMDSTAN_INSTALL` not equal to `"fixed"`, + or +- Set `CMDSTAN_INSTALL` to `"implicit"` at runtime, or +- Set the `cmdstan_install` argument to `"implicit"` for the current + `instantiate` package function you are using. # Packaging Stan models diff --git a/man/instantiate-package.Rd b/man/instantiate-package.Rd index 41e9505..19f3adc 100644 --- a/man/instantiate-package.Rd +++ b/man/instantiate-package.Rd @@ -14,12 +14,9 @@ The models compile once during installation, the executables live inside the file systems of their respective packages, and users have the full power and convenience of \href{https://mc-stan.org/cmdstanr/}{\code{CmdStanR}} -without any additional compilation. This approach saves time, -allows R package developers to migrate from -\href{https://mc-stan.org/rstan/}{\code{rstan}} -to the more modern \href{https://mc-stan.org/cmdstanr/}{\code{CmdStanR}}, -and fits well with centrally maintained R installations -where users may have trouble installing their own packages, -diagnosing compilation errors, and setting environment variables. +without any additional compilation after package installation. +This approach saves time and helps R package developers +migrate from \href{https://mc-stan.org/rstan/}{\code{rstan}} +to the more modern \href{https://mc-stan.org/cmdstanr/}{\code{CmdStanR}}. } \concept{help} diff --git a/man/stan_cmdstan_exists.Rd b/man/stan_cmdstan_exists.Rd index 581244e..0208bff 100644 --- a/man/stan_cmdstan_exists.Rd +++ b/man/stan_cmdstan_exists.Rd @@ -16,8 +16,8 @@ Choices: \item \code{""} (default): look at the original value that the \code{CMDSTAN_INSTALL} environment variable contained when \code{instantiate} at the time when it was installed. -If it was \code{"implicit"}, \code{"fixed"}, or \code{"internal"}, then choose -the corresponding option below. Otherwise, default to \code{"internal"}. +If it was \code{"implicit"} or \code{"fixed"}, then choose +the corresponding option below. Otherwise, default to \code{"implicit"}. \item \code{"implicit"}: Let the \code{cmdstanr::cmdstan_path()} decide where to look for CmdStan. As explained in the \code{cmdstanr} documentation, the output of \code{cmdstanr::cmdstan_path()} depends @@ -28,10 +28,6 @@ returns the empty string \code{""}. \item \code{"fixed"}: Use the path to CmdStan that was originally contained in \code{Sys.getenv("CMDSTAN")} at the time when \code{instantiate} was installed. -\item \code{"internal"}: Use the copy of CmdStan installed internally inside the -\code{instantiate} package file system. To use this option, -\code{instantiate} needs to have been originally installed with the -\code{CMDSTAN_INSTALL} environment variable set to \code{"internal"}. }} } \value{ diff --git a/man/stan_cmdstan_path.Rd b/man/stan_cmdstan_path.Rd index 43b3d37..5540432 100644 --- a/man/stan_cmdstan_path.Rd +++ b/man/stan_cmdstan_path.Rd @@ -14,8 +14,8 @@ Choices: \item \code{""} (default): look at the original value that the \code{CMDSTAN_INSTALL} environment variable contained when \code{instantiate} at the time when it was installed. -If it was \code{"implicit"}, \code{"fixed"}, or \code{"internal"}, then choose -the corresponding option below. Otherwise, default to \code{"internal"}. +If it was \code{"implicit"} or \code{"fixed"}, then choose +the corresponding option below. Otherwise, default to \code{"implicit"}. \item \code{"implicit"}: Let the \code{cmdstanr::cmdstan_path()} decide where to look for CmdStan. As explained in the \code{cmdstanr} documentation, the output of \code{cmdstanr::cmdstan_path()} depends @@ -26,10 +26,6 @@ returns the empty string \code{""}. \item \code{"fixed"}: Use the path to CmdStan that was originally contained in \code{Sys.getenv("CMDSTAN")} at the time when \code{instantiate} was installed. -\item \code{"internal"}: Use the copy of CmdStan installed internally inside the -\code{instantiate} package file system. To use this option, -\code{instantiate} needs to have been originally installed with the -\code{CMDSTAN_INSTALL} environment variable set to \code{"internal"}. }} } \value{ diff --git a/man/stan_cmdstan_version.Rd b/man/stan_cmdstan_version.Rd index adec581..3ecef9e 100644 --- a/man/stan_cmdstan_version.Rd +++ b/man/stan_cmdstan_version.Rd @@ -17,8 +17,8 @@ Choices: \item \code{""} (default): look at the original value that the \code{CMDSTAN_INSTALL} environment variable contained when \code{instantiate} at the time when it was installed. -If it was \code{"implicit"}, \code{"fixed"}, or \code{"internal"}, then choose -the corresponding option below. Otherwise, default to \code{"internal"}. +If it was \code{"implicit"} or \code{"fixed"}, then choose +the corresponding option below. Otherwise, default to \code{"implicit"}. \item \code{"implicit"}: Let the \code{cmdstanr::cmdstan_path()} decide where to look for CmdStan. As explained in the \code{cmdstanr} documentation, the output of \code{cmdstanr::cmdstan_path()} depends @@ -29,10 +29,6 @@ returns the empty string \code{""}. \item \code{"fixed"}: Use the path to CmdStan that was originally contained in \code{Sys.getenv("CMDSTAN")} at the time when \code{instantiate} was installed. -\item \code{"internal"}: Use the copy of CmdStan installed internally inside the -\code{instantiate} package file system. To use this option, -\code{instantiate} needs to have been originally installed with the -\code{CMDSTAN_INSTALL} environment variable set to \code{"internal"}. }} \item{error_on_NA}{(logical) Should an error be thrown if CmdStan is not diff --git a/man/stan_package_compile.Rd b/man/stan_package_compile.Rd index e435d92..92d4cc1 100644 --- a/man/stan_package_compile.Rd +++ b/man/stan_package_compile.Rd @@ -32,8 +32,8 @@ Choices: \item \code{""} (default): look at the original value that the \code{CMDSTAN_INSTALL} environment variable contained when \code{instantiate} at the time when it was installed. -If it was \code{"implicit"}, \code{"fixed"}, or \code{"internal"}, then choose -the corresponding option below. Otherwise, default to \code{"internal"}. +If it was \code{"implicit"} or \code{"fixed"}, then choose +the corresponding option below. Otherwise, default to \code{"implicit"}. \item \code{"implicit"}: Let the \code{cmdstanr::cmdstan_path()} decide where to look for CmdStan. As explained in the \code{cmdstanr} documentation, the output of \code{cmdstanr::cmdstan_path()} depends @@ -44,10 +44,6 @@ returns the empty string \code{""}. \item \code{"fixed"}: Use the path to CmdStan that was originally contained in \code{Sys.getenv("CMDSTAN")} at the time when \code{instantiate} was installed. -\item \code{"internal"}: Use the copy of CmdStan installed internally inside the -\code{instantiate} package file system. To use this option, -\code{instantiate} needs to have been originally installed with the -\code{CMDSTAN_INSTALL} environment variable set to \code{"internal"}. }} \item{verbose}{Logical of length 1, whether to set the diff --git a/man/stan_package_model.Rd b/man/stan_package_model.Rd index 75ca61a..5a31696 100644 --- a/man/stan_package_model.Rd +++ b/man/stan_package_model.Rd @@ -29,8 +29,8 @@ Choices: \item \code{""} (default): look at the original value that the \code{CMDSTAN_INSTALL} environment variable contained when \code{instantiate} at the time when it was installed. -If it was \code{"implicit"}, \code{"fixed"}, or \code{"internal"}, then choose -the corresponding option below. Otherwise, default to \code{"internal"}. +If it was \code{"implicit"} or \code{"fixed"}, then choose +the corresponding option below. Otherwise, default to \code{"implicit"}. \item \code{"implicit"}: Let the \code{cmdstanr::cmdstan_path()} decide where to look for CmdStan. As explained in the \code{cmdstanr} documentation, the output of \code{cmdstanr::cmdstan_path()} depends @@ -41,10 +41,6 @@ returns the empty string \code{""}. \item \code{"fixed"}: Use the path to CmdStan that was originally contained in \code{Sys.getenv("CMDSTAN")} at the time when \code{instantiate} was installed. -\item \code{"internal"}: Use the copy of CmdStan installed internally inside the -\code{instantiate} package file system. To use this option, -\code{instantiate} needs to have been originally installed with the -\code{CMDSTAN_INSTALL} environment variable set to \code{"internal"}. }} } \value{ diff --git a/src/install.libs.R b/src/install.libs.R index 4c2aaf0..bea853a 100644 --- a/src/install.libs.R +++ b/src/install.libs.R @@ -1,5 +1,3 @@ -choice <- tolower(Sys.getenv("CMDSTAN_INSTALL", "")) - libs <- file.path(R_PACKAGE_DIR, "libs", R_ARCH) dir.create(libs, recursive = TRUE, showWarnings = FALSE) for (file in c("symbols.rds", Sys.glob(paste0("*", SHLIB_EXT)))) { @@ -8,37 +6,15 @@ for (file in c("symbols.rds", Sys.glob(paste0("*", SHLIB_EXT)))) { } } +choice <- tolower(Sys.getenv("CMDSTAN_INSTALL", "")) if (identical(choice, "internal")) { - rlang::check_installed( - pkg = "cmdstanr", - reason = "The {cmdstanr} package is required to auto-install CmdStan.", - action = function(pkg, ...) { - install.packages( - pkgs = "cmdstanr", - repos = c("https://mc-stan.org/r-packages/", getOption("repos")) - ) - } - ) - cmdstan <- file.path(R_PACKAGE_DIR, "bin", "cmdstan") - if (!file.exists(cmdstan)) { - dir.create(cmdstan, recursive = TRUE, showWarnings = FALSE) - } - message( - sprintf( - c( - "Internal installation: installing CmdStan inside {instantiate} ", - "\"%s\". {instantiate} will prefer this internal copy of CmdStan ", - "when the CMDSTAN_INSTALL environment variable is unset." - ), - cmdstan - ) + warning( + "'internal' installation in {instantiate} is no longer supported. ", + "Falling back on 'implicit' installation." ) - cmdstanr::install_cmdstan(dir = cmdstan) - cmdstan <- max(list.files(cmdstan, full.names = TRUE)) - cmdstanr::set_cmdstan_path(path = cmdstan) - example <- file.path(cmdstan, "examples", "bernoulli", "bernoulli.stan") - cmdstanr::cmdstan_model(stan_file = example, compile = TRUE) -} else if (identical(choice, "fixed")) { + choice <- "implicit" +} +if (identical(choice, "fixed")) { message( sprintf( c(