Skip to content

Commit 13639a5

Browse files
committed
Catching things up
1 parent 2a27a54 commit 13639a5

8 files changed

Lines changed: 91 additions & 74 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Description: This package provides helper functions that can be used for integra
1313
need for Rcpp and RStan packages(and their dependencies). Using CmdStanR
1414
will also afford users the opportunity to use the latest developments
1515
within CmdStan. However, building these packages requires some work and this
16-
package provides tools to assist with that,
16+
package provides tools to assist with that process.
1717
License: MIT + file LICENSE
1818
URL: https://github.com/medewitt/staninside, https://michaeldewittjr.com/staninside/
1919
BugReports: https://github.com/medewitt/staninside/issues
2020
Encoding: UTF-8
2121
LazyData: true
2222
Roxygen: list(markdown = TRUE)
23-
RoxygenNote: 7.1.2
23+
RoxygenNote: 7.3.2
2424
Depends:
2525
R (>= 3.1)
2626
Suggests:

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
push:
22
Rscript --vanilla _push.R
3+
4+
docs:
5+
Rscript -e "pkgdown::build_docs()"
6+
7+
check:
8+
Rscript -e "devtools::check()"

R/copy_models.R

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,52 @@
1111
#'
1212
#' @examples
1313
#' local_location <- tempdir()
14-
#' copy_models(pkgname = "staninside", local_location = local_location)
14+
#' copy_models(pkgname = "staninside", local_location = local_location)
15+
copy_models <- function(pkgname = "staninside", local_location = NULL) {
16+
if (is.null(local_location)) {
17+
local_location <- rappdirs::user_cache_dir(appname = pkgname)
18+
cli::cli_alert("`{local_location}` will be used")
19+
}
1520

16-
copy_models <- function(pkgname = "staninside", local_location = NULL){
21+
stan_code_to_copy <- find_stan_code(pkgname)
1722

18-
if(is.null(local_location)){
19-
local_location <- rappdirs::user_cache_dir(appname = pkgname)
20-
cli::cli_alert("`{local_location}` will be used")
21-
}
23+
pkg_location <- names(stan_code_to_copy$base_stan_location)
2224

23-
stan_code_to_copy <- find_stan_code(pkgname)
25+
new_locations_base <- gsub(pattern = ".+/stan/", "", pkg_location)
2426

25-
pkg_location <- names(stan_code_to_copy$base_stan_location)
27+
new_locations <- file.path(local_location, new_locations_base)
2628

27-
new_locations_base <- gsub(pattern = ".+/stan/","",pkg_location)
29+
dirs_needed <- unique(dirname(new_locations_base))
2830

29-
new_locations <- file.path(local_location,new_locations_base)
31+
dirs_needed <- dirs_needed[dirs_needed != "."]
3032

31-
dirs_needed <- unique(dirname(new_locations_base))
33+
dirs_needed <- dirs_needed[!grepl(dirs_needed, pattern = ".stan")]
3234

33-
dirs_needed <- dirs_needed[dirs_needed!="."]
3435

35-
dirs_needed <- dirs_needed[!grepl(dirs_needed, pattern = ".stan")]
36+
dirs_needed <- dirs_needed[!vapply(file.path(local_location, dirs_needed),
37+
function(x) fs::dir_exists(path = x),
38+
FUN.VALUE = logical(1)
39+
)]
3640

41+
if (!fs::dir_exists(local_location)) {
42+
fs::dir_create(local_location)
43+
}
3744

38-
dirs_needed <- dirs_needed[!vapply(file.path(local_location,dirs_needed),
39-
function(x) fs::dir_exists(path = x), FUN.VALUE = logical(1))]
4045

41-
if(!fs::dir_exists(local_location)){
42-
fs::dir_create(local_location)
43-
}
46+
if (length(dirs_needed) > 0) {
47+
lapply(
48+
file.path(local_location, dirs_needed),
49+
function(x) fs::dir_create(path = x, recurse = TRUE)
50+
)
51+
}
4452

53+
fs::file_copy(
54+
path = pkg_location,
55+
new_path = new_locations,
56+
overwrite = TRUE
57+
)
4558

46-
if(length(dirs_needed)>0){
47-
lapply(file.path(local_location,dirs_needed),
48-
function(x) fs::dir_create(path = x, recurse = TRUE))
49-
}
59+
cli::cli_alert_success("All models have been copied to your specified location")
5060

51-
fs::file_copy(path = pkg_location,
52-
new_path =new_locations,
53-
overwrite = TRUE)
54-
55-
cli::cli_alert_success("All models have been copied to your specified location")
56-
57-
invisible(new_locations)
61+
invisible(new_locations)
5862
}

R/find_stan_code.R

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,48 @@
66
#'
77
#' @param pkgname a string, the name of the package in which to
88
#' look for the Stan code.
9+
#' @return a list with the Stan code location within the package
10+
#' and the text of the code,
911
#' @export
1012
#' @examples
1113
#' find_stan_code(pkgname = "staninside")
12-
13-
find_stan_code <- function(pkgname = "staninside"){
14-
15-
if(!pkgname %in% rownames(installed.packages())){
16-
stop(sprintf("`%s` is not an installed package.\n", pkgname))
17-
}
18-
19-
stan_pkg_location <- fs::dir_ls(system.file(package = pkgname),
20-
glob = "*.stan", recurse = TRUE)
21-
22-
stan_pkg_contents <- lapply(stan_pkg_location, readLines)
23-
24-
cleaner_regex <- paste(".+",pkgname,"stan", "(.+\\.stan)", sep = "/")
25-
base_stan_location <- vapply(X = stan_pkg_location,
26-
function(x) gsub(pattern = cleaner_regex,
27-
replacement = "\\1",
28-
x),
29-
FUN.VALUE = character(1))
30-
31-
names(stan_pkg_contents) <- base_stan_location
32-
33-
for(i in seq_along(stan_pkg_contents)){
34-
class(stan_pkg_contents[[i]]) <- c("character", "stancode")
35-
}
36-
37-
return(list(base_stan_location=base_stan_location,
38-
stan_pkg_contents = stan_pkg_contents))
14+
find_stan_code <- function(pkgname = "staninside") {
15+
if (!pkgname %in% rownames(installed.packages())) {
16+
stop(sprintf("`%s` is not an installed package.\n", pkgname))
17+
}
18+
19+
stan_pkg_location <- fs::dir_ls(system.file(package = pkgname),
20+
glob = "*.stan", recurse = TRUE
21+
)
22+
23+
stan_pkg_contents <- lapply(stan_pkg_location, readLines)
24+
25+
cleaner_regex <- paste(".+", pkgname, "stan", "(.+\\.stan)", sep = "/")
26+
base_stan_location <- vapply(
27+
X = stan_pkg_location,
28+
function(x) {
29+
gsub(
30+
pattern = cleaner_regex,
31+
replacement = "\\1",
32+
x
33+
)
34+
},
35+
FUN.VALUE = character(1)
36+
)
37+
38+
names(stan_pkg_contents) <- base_stan_location
39+
40+
for (i in seq_along(stan_pkg_contents)) {
41+
class(stan_pkg_contents[[i]]) <- c("character", "stancode")
42+
}
43+
44+
return(list(
45+
base_stan_location = base_stan_location,
46+
stan_pkg_contents = stan_pkg_contents
47+
))
3948
}
4049

41-
#'@export
42-
print.stancode <- function(x, ...){
43-
cat(x, sep = "\n")
50+
#' @export
51+
print.stancode <- function(x, ...) {
52+
cat(x, sep = "\n")
4453
}

man/copy_models.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/find_stan_code.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/staninside-package.Rd

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/using-staninside.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ library(staninside)
2020

2121
# Motivation
2222

23-
Stan is a powerful language and using R (and specifically rstan) as glue to allow R users to easily interface existing models.
24-
Sometimes a user might wish to use a different backend (CmdStanR) or examine and modify the written Stan code directly.
23+
Stan is a powerful language and using R (and specifically `rstan`) as glue to allow R users to easily interface existing models.
24+
Sometimes a user might wish to use a different backend (`CmdStanR`) or examine and modify the written Stan code directly.
2525
This package seeks to allow users the ability to:
2626

27-
* Build R packages that are not pre-compiled by rstan
27+
* Build R packages that are not pre-compiled by `rstan`
2828
* Allow users to use CmdStanR in their R packages
2929
* Allow users to easily see the Stan code within packages
3030

3131
## Building a Stan Package
3232

3333
### Package Infrastructure
3434

35-
If a package developer were to build a package with stanside, they first could create the package directory in the usual ways.
35+
If a package developer were to build a package with `staninside`, they first could create the package directory in the usual ways.
3636

3737
Next, to build the appropriate structure the author could call
3838

@@ -62,7 +62,7 @@ These separate `.stan` files can then be accessed within the Stan code with the
6262
### Model Fitting
6363

6464
Generally speaking, there will be some function in an R package that will fit the data to a model.
65-
In order to faciliate this fitting with a CmdStanR backend, a process like that below could be used.
65+
In order to facilitate this fitting with a CmdStanR backend, a process like that below could be used.
6666

6767
```r
6868

0 commit comments

Comments
 (0)