Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert rstan to a suggests to avoid windows installation issues #405

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@

^CONTRIBUTING.md$
^local$
^inst/stan/MMRM.rds$
55 changes: 0 additions & 55 deletions .github/actions/build-src/action.yml

This file was deleted.

10 changes: 2 additions & 8 deletions .github/workflows/on_pr_main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name: On Pull Request
jobs:

rcmdcheck:

strategy:
fail-fast: true
matrix:
Expand All @@ -29,10 +29,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Build src
uses: ./.github/actions/build-src


- name: Check
uses: ./.github/actions/rcmdcheck
with:
Expand All @@ -54,9 +51,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Build src
uses: ./.github/actions/build-src

- name: Build Vignettes
run: |
Rscript ./vignettes/build.R
10 changes: 1 addition & 9 deletions .github/workflows/on_push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ jobs:

- name: Checkout
uses: actions/checkout@v3

- name: Build src
uses: ./.github/actions/build-src


- name: testthat
run: |
options(crayon.enabled = TRUE, cli.dynamic = FALSE)
Expand All @@ -36,11 +33,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Build src
uses: ./.github/actions/build-src
with:
refresh: false

- name: Document Code
run: |
options(crayon.enabled = TRUE, cli.dynamic = FALSE)
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ docs


local/

inst/stan/*.rds
inst/stan/*.RDS
inst/stan/*.Rds
16 changes: 2 additions & 14 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,16 @@ Suggests:
lubridate,
purrr,
ggplot2,
R.rsp
Biarch: true
R.rsp,
rstan (>= 2.26.0)
Config/testthat/edition: 3
Imports:
mmrm,
pkgload,
Matrix,
methods,
Rcpp (>= 0.12.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.26.0),
rstantools (>= 2.1.1),
R6,
assertthat
LinkingTo:
BH (>= 1.66.0),
Rcpp (>= 0.12.0),
RcppEigen (>= 0.3.3.3.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.26.0),
StanHeaders (>= 2.26.0)
SystemRequirements: GNU make
Depends:
R (>= 3.4.0)
License: Apache License (>= 2)
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ export(strategy_MAR)
export(validate)
export(validate_analyse_pars)
import(R6)
import(Rcpp)
import(methods)
importFrom(assertthat,assert_that)
importFrom(mmrm,VarCorr)
importFrom(rstan,extract)
importFrom(rstan,sampling)
importFrom(rstan,summary)
importFrom(stats,as.formula)
importFrom(stats,binomial)
importFrom(stats,coef)
Expand All @@ -100,4 +96,3 @@ importFrom(stats,var)
importFrom(stats,vcov)
importFrom(utils,capture.output)
importFrom(utils,relist)
useDynLib(rbmi, .registration = TRUE)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# rbmi (development version)

* Downgraded rstan to a `Suggests` dependency and opt to compile "just in time" rather than at installation time. This is intended to make installation easier for user who don't use `method_bayes()` (#405)

# rbmi 1.2.6

* Updated unit tests to fix false-positive error on CRAN's testing servers
Expand Down
62 changes: 53 additions & 9 deletions R/mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
#' - `samples`: a named list containing the draws for each parameter. It corresponds to the output of [extract_draws()].
#' - `fit`: a `stanfit` object.
#'
#'
#' @import Rcpp
#' @import methods
#' @importFrom rstan sampling
#' @useDynLib rbmi, .registration = TRUE
fit_mcmc <- function(
designmat,
outcome,
Expand All @@ -59,6 +55,8 @@ fit_mcmc <- function(
quiet = FALSE
) {

ensure_stan_is_available()

n_imputations <- method$n_samples
burn_in <- method$burn_in
seed <- method$seed
Expand Down Expand Up @@ -96,7 +94,7 @@ fit_mcmc <- function(
)

sampling_args <- list(
object = stanmodels$MMRM,
object = get_stan_model(),
data = stan_data,
pars = c("beta", "Sigma"),
chains = 1,
Expand All @@ -123,7 +121,7 @@ fit_mcmc <- function(
sampling_args$seed <- seed

stan_fit <- record({
do.call(sampling, sampling_args)
do.call(rstan::sampling, sampling_args)
})

if (!is.null(stan_fit$errors)) {
Expand Down Expand Up @@ -240,10 +238,9 @@ split_dim <- function(a, n) {
#' of the list is a list with length equal to 1 if `same_cov = TRUE` or equal to the
#' number of groups if `same_cov = FALSE`.
#'
#' @importFrom rstan extract
extract_draws <- function(stan_fit) {

pars <- extract(stan_fit, pars = c("beta", "Sigma"))
pars <- rstan::extract(stan_fit, pars = c("beta", "Sigma"))
names(pars) <- c("beta", "sigma")

##################### from array to list
Expand All @@ -268,7 +265,6 @@ extract_draws <- function(stan_fit) {
#' @return
#' A named vector containing the ESS for each parameter of the model.
#'
#' @importFrom rstan summary
get_ESS <- function(stan_fit) {
return(rstan::summary(stan_fit, pars = c("beta", "Sigma"))$summary[, "n_eff"])
}
Expand Down Expand Up @@ -646,3 +642,51 @@ validate.stan_data <- function(x, ...) {
msg = "Invalid Stan Data Object"
)
}



#' Ensure rstan is available
#'
#' This function checks if the rstan package is installed.
#' If it is not installed, an error message is thrown.
#'
#' @keywords internal
ensure_stan_is_available <- function() {
if (!requireNamespace("rstan", quietly = TRUE)) {
stop("`method_bayes()` requires that the `rstan` package is installed. Please install it and try again.")
}
}


# Environment variable to store the compiled stan model
# This is needed to prevent rstan from recompiling the model each time
# https://discourse.mc-stan.org/t/rstan-sometimes-recompiles-to-avoid-crashing-r-session/4911
STAN_ENV <- new.env()


#' Get the compiled Stan model
#'
#' @description
#' Get the compiled Stan model.
#' If the model has already been compiled, return it from memory.
#' If not, compile the model and return it.
#'
#' @param model_env An environment where the compiled model is stored.
#' @return
#' A `stanfit` object.
#' @keywords internal
get_stan_model <- function(model_env = STAN_ENV) {
model_file <- if (file.exists("inst/stan/MMRM.stan")) {
"inst/stan/MMRM.stan"
} else if (file.exists("stan/MMRM.stan")) {
"stan/MMRM.stan"
} else {
system.file("stan/MMRM.stan", package = "rbmi")
}
model_env$model <- rstan::stan_model(
file = model_file,
auto_write = TRUE,
save_dso = TRUE
)
model_env$model
}
25 changes: 0 additions & 25 deletions R/stanmodels.R

This file was deleted.

Binary file modified R/sysdata.rda
Binary file not shown.
18 changes: 0 additions & 18 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -512,21 +512,3 @@ as_dataframe <- function(x) {
row.names(x2) <- NULL
return(x2)
}



#' Do not run this function
#'
#' This function only exists to suppress the false positive
#' from R CMD Check about unused libraries
#'
#' Both rstantools and RcppParallel are required but are only used at
#' installation time. In the case of RcppParallel it is used in the
#' `src/Makevars` file which is created on the fly during installation
#' by rstantools. rstantools is used in the `configure` file.
#'
do_not_run <- function() {
rstantools::use_rstan()
RcppParallel::CxxFlags()
}

4 changes: 0 additions & 4 deletions configure

This file was deleted.

4 changes: 0 additions & 4 deletions configure.win

This file was deleted.

1 change: 0 additions & 1 deletion inst/include/stan_meta_header.hpp

This file was deleted.

14 changes: 0 additions & 14 deletions inst/stan/include/license.stan

This file was deleted.

18 changes: 0 additions & 18 deletions man/do_not_run.Rd

This file was deleted.

Loading
Loading