Skip to content

Commit 84fd505

Browse files
committed
Add handling for models with cpp changes
1 parent 38f764a commit 84fd505

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

R/rstan_config.R

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@
3939
#'
4040
rstan_config <- function(pkgdir = ".") {
4141
pkgdir <- .check_pkgdir(pkgdir) # check if package root directory
42+
pkg_dcf <- read.dcf(file.path(pkgdir, "DESCRIPTION"))
43+
pkg_name <- pkg_dcf[1, "Package"]
44+
pkg_ver <- pkg_dcf[1, "Version"]
45+
4246
# get stan model files
4347
stan_files <- list.files(file.path(pkgdir, "inst", "stan"),
4448
full.names = TRUE,
4549
pattern = "(\\.stan$)|(\\.stanfunctions$)")
4650
if (length(stan_files) != 0) {
47-
if (is_excepted(pkgdir) && (utils::packageVersion("StanHeaders") >= "2.36")) {
48-
.update_deprecations(pkgdir, stan_files)
51+
is_excepted <- isTRUE(stanc_exceptions[[pkg_name]] == pkg_ver) && (utils::packageVersion("StanHeaders") >= "2.36")
52+
53+
if (is_excepted) {
54+
.update_deprecations(pkg_name, stan_files)
4955
}
56+
5057
# add R & src folders in case run from configure[.win] script
5158
.add_standir(pkgdir, "R", msg = FALSE, warn = FALSE)
5259
.add_standir(pkgdir, "src", msg = FALSE, warn = FALSE)
5360
# convert all .stan files to .cc/.hpp pairs
54-
sapply(stan_files, .make_cc, pkgdir = pkgdir)
61+
sapply(stan_files, .make_cc, pkgdir = pkgdir, pkg_name = pkg_name,
62+
is_excepted = is_excepted)
5563
# update package Makevars
5664
acc <- .setup_Makevars(pkgdir, add = TRUE)
5765
## .add_Makevars(pkgdir)
@@ -68,7 +76,7 @@ rstan_config <- function(pkgdir = ".") {
6876
# register exported modules as native routines
6977
Rcpp::compileAttributes(pkgdir)
7078
# update R/stanmodels.R with current set of models
71-
stanmodels <- .update_stanmodels(pkgdir)
79+
stanmodels <- .update_stanmodels(pkgdir, pkg_name, is_excepted)
7280
acc <- acc | .add_stanfile(stanmodels, pkgdir, "R", "stanmodels.R")
7381
invisible(acc)
7482
}
@@ -170,7 +178,7 @@ rstan_config <- function(pkgdir = ".") {
170178
# If the .stan file has a functions block but no parameters block, then there
171179
# is no module definition but the functions are compiled and exported to the
172180
# package's namespace.
173-
.make_cc <- function(file_name, pkgdir) {
181+
.make_cc <- function(file_name, pkgdir, pkg_name, is_excepted) {
174182
model_name <- sub("[.]stan$", "", basename(file_name)) # model name
175183
## path to src/stan_files
176184
## stan_path <- file.path(pkgdir, "src", "stan_files")
@@ -248,6 +256,9 @@ rstan_config <- function(pkgdir = ".") {
248256
if (utils::packageVersion('StanHeaders') >= "2.34") {
249257
cppcode <- gsub("boost::ecuyer1988", "stan::rng_t", cppcode, fixed = TRUE)
250258
}
259+
if (is_excepted && !is.null(cpp_pre_process[[pkg_name]])) {
260+
cppcode <- cpp_pre_process[[pkg_name]](cppcode)
261+
}
251262
# Stan header file
252263
hdr_name <- .stan_prefix(model_name, ".h")
253264
# get license file (if any)
@@ -319,7 +330,7 @@ rstan_config <- function(pkgdir = ".") {
319330
}
320331

321332
# rewrites stanmodels.R reflecting current list of stan files
322-
.update_stanmodels <- function(pkgdir) {
333+
.update_stanmodels <- function(pkgdir, pkg_name, is_excepted) {
323334
model_names <- list.files(file.path(pkgdir, "inst", "stan"),
324335
pattern = "*.stan$")
325336
only_functions <- sapply(model_names, FUN = function(nm) {
@@ -357,6 +368,23 @@ rstan_config <- function(pkgdir = ".") {
357368
stanmodels[(model_line+2):load_line],
358369
load_module,
359370
stanmodels[(load_line+2):nlines])
371+
if (is_excepted && !is.null(cpp_pre_process[[pkg_name]])) {
372+
process_fun <- c("process_fun <- ", deparse(cpp_pre_process[[pkg_name]]))
373+
374+
process_text <- c(
375+
"process_fun <- ", deparse(cpp_pre_process[[pkg_name]]),
376+
"stanfit$model_code <- process_fun(stanfit$model_code)",
377+
"stanfit$model_cpp$model_cppcode <- process_fun(stanfit$model_cpp$model_cppcode)"
378+
)
379+
380+
insert_loc <- grep("# create stanmodel object$", stanmodels)
381+
nlines <- length(stanmodels)
382+
stanmodels <- c(
383+
stanmodels[1:(insert_loc-1)],
384+
process_text,
385+
stanmodels[insert_loc:nlines]
386+
)
387+
}
360388
}
361389
stanmodels
362390
}
@@ -406,11 +434,8 @@ rstan_config <- function(pkgdir = ".") {
406434
gsub("auto", rtn_type, cpp_lines[decl_line], fixed = TRUE)
407435
}
408436

409-
.update_deprecations <- function(pkgdir, stan_files) {
410-
pkg_dcf <- read.dcf(file.path(pkgdir, "DESCRIPTION"))
411-
pkg_nm <- pkg_dcf[1, "Package"]
412-
413-
post_process <- post_processing[[pkg_nm]]
437+
.update_deprecations <- function(pkg_name, stan_files) {
438+
post_process <- stan_post_process[[pkg_name]]
414439
if (is.null(post_process)) {
415440
post_process <- function(x) x
416441
}

R/stanc_exceptions.R

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
is_excepted <- function(pkgdir) {
2-
pkg_dcf <- read.dcf(file.path(pkgdir, "DESCRIPTION"))
3-
pkg_nm <- pkg_dcf[1, "Package"]
4-
pkg_ver <- pkg_dcf[1, "Version"]
5-
6-
isTRUE(stanc_exceptions[[pkg_nm]] == pkg_ver)
7-
}
8-
91
stanc_exceptions <- list(
102
AovBay = "0.1.0",
113
baggr = "0.8.2",
@@ -52,7 +44,7 @@ stanc_exceptions <- list(
5244
)
5345

5446
# Additional deprecations not covered by 2.32 stanc3 canonicalise
55-
post_processing <- list(
47+
stan_post_process <- list(
5648
publipha = function(model_code) {
5749
model_code <- gsub("real (lower|upper)", "real \\1_par", model_code)
5850
model_code <- gsub("normal_(cdf|lccdf|lcdf)\\((-)?(upper|lower)(,| \\|)", "normal_\\1(\\2\\3_par |", model_code)
@@ -67,3 +59,14 @@ post_processing <- list(
6759
model_code
6860
}
6961
)
62+
63+
cpp_pre_process <- list(
64+
survstan = function(cpp_code) {
65+
cpp_code <- gsub("offset_par", "offset", cpp_code, fixed = TRUE)
66+
cpp_code
67+
},
68+
cbq = function(cpp_code) {
69+
cpp_code <- gsub("offset_par", "offset", cpp_code, fixed = TRUE)
70+
cpp_code
71+
}
72+
)

0 commit comments

Comments
 (0)