3939# '
4040rstan_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 }
0 commit comments