Skip to content
Merged
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
5 changes: 4 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
^\.pre-commit-config\.yaml$
^doc$
^Meta$
^README\.html$
^README\.html$
^cran-comments\.md$
^CRAN-SUBMISSION$
^.vscode$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
inst/doc
/doc/
/Meta/
.vscode/
CRAN-SUBMISSION
15 changes: 7 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: staninside
Title: Facilitating the Use of Stan Within Packages
Title: "Facilitating the Use of 'Stan' Within Packages"
Version: 0.0.4
Authors@R:
person(given = "Michael",
Expand All @@ -8,13 +8,12 @@ Authors@R:
email = "me.dewitt.jr@gmail.com",
comment = c(ORCID = "0000-0001-8940-1967"))
Description: Infrastructure and functions that can be used for
integrating Stan code into stand alone R packages which in turn use the
CmdStan engine (often accessed through CmdStanR). Using CmdStanR and
pre-written Stan code can make package installation easy and less prone to
fail because it removes the need for Rcpp and RStan packages(and their dependencies).
Using CmdStanR will also afford users the opportunity to use the latest developments
within CmdStan. However, building these packages requires some work and this
package provides tools to assist with that process.
integrating 'Stan' (Carpenter et al., 2017 <doi:10.18637/jss.v076.i01>) code into
stand alone R packages which in turn use the 'CmdStan' engine (often accessed through
'CmdStanR' (Stan Development Team, 2025 <https://mc-stan.org/users/interfaces/cmdstanr/>)).
Using 'CmdStanR' and pre-written 'Stan' code can make package installation easy. Using 'staninside'
offers a way to cache user-compiled 'Stan' models in user-specified directories reducing the need
to recompile the same model multiple times.
License: MIT + file LICENSE
URL: https://github.com/medewitt/staninside, https://medewitt.github.io/staninside/
BugReports: https://github.com/medewitt/staninside/issues
Expand Down
11 changes: 10 additions & 1 deletion R/copy_models.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#'
#' @param pkgname a string, the name of the package
#' @param local_location a file path representing the desired location
#' of the local copy.
#' of the local copy. If "default" is provided, the function will use
#' the user cache directory for the package.
#' @returns file path of newly created files
#' @importFrom fs dir_create dir_exists file_copy
#' @export
Expand All @@ -15,8 +16,16 @@
#' copy_models(pkgname = "staninside", local_location = local_location)
copy_models <- function(pkgname = "staninside", local_location = NULL) {
if (is.null(local_location)) {
stop("A location must be provided")
}

if (local_location == "default") {
local_location <- rappdirs::user_cache_dir(appname = pkgname)
cli::cli_alert("`{local_location}` will be used")
} else {
if (!dir.exists(local_location)) {
stop("The directory you have indicated does not exist")
}
}

stan_code_to_copy <- find_stan_code(pkgname)
Expand Down
4 changes: 1 addition & 3 deletions R/find_stan_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
#' @examples
#' find_stan_code(pkgname = "staninside")
find_stan_code <- function(pkgname = "staninside") {
if (!pkgname %in% rownames(installed.packages())) {
stop(sprintf("`%s` is not an installed package.\n", pkgname))
}
requireNamespace(pkgname)

stan_pkg_location <- fs::dir_ls(system.file(package = pkgname),
glob = "*.stan", recurse = TRUE
Expand Down
15 changes: 7 additions & 8 deletions R/setup_stan_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#' Create directory and helper files for a Stan package using
#' CmdStanR
#'
#' @param loc a file.path indicating package root with a default of the
#' current working directory
#' @param loc a file.path indicating package root.
#' @param use_all a logical indicating if each section of Stan code
#' should have it's own directory with a default of \code{TRUE}
#' @returns invisible null
Expand All @@ -22,12 +21,12 @@
#' @export

setup_stan_package <- function(loc = NULL, use_all = TRUE) {
if (!is.null(loc)) {
if (!dir.exists(loc)) {
stop("The directory you have indicated does not exist,")
}
} else {
loc <- getwd()
if (is.null(loc)) {
stop("A location must be provided")
}

if (!dir.exists(loc)) {
stop("The directory you have indicated does not exist,")
}

target_path <- file.path(loc, "inst", "stan")
Expand Down
14 changes: 14 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## R CMD check results

0 errors | 0 warnings | 0 notes

* This is a new package and a resubmission
* This package has been tested on local MAC and rub using the Rhub Github Actions tools to test Linux, Mac, Windows.

### Issue resolved from submission
- Properly cited methods in the DESCRIPTION
- Update packages, software, and APIs with single quotations
- Removed all `getwd` statements as defaults and require user to explicity enter file path
- Vignettes do not utilize any local, package, or user directories
- Title has double quotes
- Removed use of `installed.packages()` to use `requireNamespace`
44 changes: 24 additions & 20 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
aut
BugReports
cli
CMD
CmdStan
CmdStanR
Codecov
Github
ORCID
RStan
Rcpp
TMPDIR
al
cmdstanr
doi
et
filespace
jss
pre
testthat
tinytest
BugReports
CRAN
Config
DeWitt
LazyData
Roxygen
RoxygenNote
VignetteBuilder
aut
cli
covr
cran
CRAN
cre
dewitt
DeWitt
dplyr
filespace
fs
github
Github
gmail
https
knitr
LazyData
medewitt
michaeldewittjr
ORCID
pre
rappdirs
Rcpp
rmarkdown
Roxygen
RoxygenNote
rstan
RStan
staninside
testthat
tidyverse
tinytest
TMPDIR
VignetteBuilder
cmdstanr
tidyverse
3 changes: 2 additions & 1 deletion man/copy_models.Rd

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

3 changes: 1 addition & 2 deletions man/setup_stan_package.Rd

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

4 changes: 2 additions & 2 deletions man/staninside-package.Rd

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

22 changes: 8 additions & 14 deletions tests/testthat/test-clear_stan_cache.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
test_clearing_cache <- function() {
# Copy over models
app_loc <- tempdir()
loc <- copy_models(pkgname = "staninside", local_location = app_loc)

copied_over_files <- all(c("test.stan", "func.stan") %in% basename(loc))

clear_stan_cache(pkgname = "staninside")

test_clearing_cache <- function(){

# Copy over models
app_loc <- rappdirs::user_cache_dir("staninside")
loc <- copy_models(pkgname = "staninside", local_location = app_loc)

copied_over_files <- all(c("test.stan", "func.stan") %in% basename(loc))

clear_stan_cache(pkgname = "staninside")

all_removed <- !isTRUE(lapply(loc, file.exists))

all(copied_over_files,all_removed)

all_removed <- !isTRUE(lapply(loc, file.exists))

all(copied_over_files, all_removed)
}

test_that("stan files can be copied over and cleared", {
Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-copy_models.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Make model available

local_location <- tempdir()
copy_models(pkgname = "staninside", local_location = local_location)
copy_models(pkgname = "staninside", local_location = local_location)

test_stan <- list.files(local_location, full.names = TRUE, pattern = "test.stan")

Expand All @@ -10,7 +10,9 @@ test_that("models from test package have been copied", {
})

test_that("test that all stan directories are available", {
expect_true(dir.exists(file.path(local_location, "functions")))
expect_true(dir.exists(file.path(local_location, "functions")))
})


test_that("test that when no location is provided, an error is thrown", {
expect_error(copy_models(pkgname = "staninside"))
})
8 changes: 6 additions & 2 deletions tests/testthat/test-setup_stan_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ test_that("full process functional", {
expect_true(dir.exists(file.path(test_dir, "inst", "stan")))
})
test_that("sub directories created functional", {
expect_true(dir.exists(file.path(test_dir, "inst", "stan", "data")))
expect_true(dir.exists(file.path(test_dir, "inst", "stan", "data")))
})

test_that("fails on invalid path", {
expect_error(setup_stan_package("blerg"))
expect_error(setup_stan_package("blerg"))
})

test_that("fails on no location", {
expect_error(setup_stan_package())
})
2 changes: 2 additions & 0 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.html
*.R
CRAN-SUBMISSION
CRAN*
5 changes: 3 additions & 2 deletions vignettes/using-staninside.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ If a package developer were to build a package with `staninside`, they first cou
Next, to build the appropriate structure the author could call

```r
setup_stan_package()
temp_dir <- tempdir()
setup_stan_package(loc = temp_dir)
```

This function will create the following directory tree:
Expand Down Expand Up @@ -77,7 +78,7 @@ fit_my_data <- function(my_data){
"Use `staninside::clear_stan_cache('mypackage')` if you need to refresh")
} else {
cli::cli_alert_info("Copying Stan models to cache")
staninside::copy_models(this_pkg())
staninside::copy_models(pkgname = "mypackage", local_location = local_location)
cli::cli_alert_success("Models copied!")
}

Expand Down
Loading