Skip to content

Commit

Permalink
Merge pull request #6 from stan-dev/init_cpp
Browse files Browse the repository at this point in the history
add init_cpp function and also call it from rstan_package_skeleton
  • Loading branch information
jgabry authored Mar 29, 2017
2 parents 5fe526b + ddcd8a6 commit 533d90f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 4 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: rstantools
Type: Package
Title: Tools for Developing R Packages Interfacing with 'Stan'
Version: 1.2.0
Date: 2017-03-17
Version: 1.2.1
Date: 2017-03-29
Authors@R: c(person("Jonah", "Gabry", email = "jsg2201@columbia.edu",
role = c("aut", "cre")),
person("Ben", "Goodrich", email = "benjamin.goodrich@columbia.edu",
Expand All @@ -27,7 +27,7 @@ Suggests:
rstan (>= 2.14.1),
rstanarm (>= 2.14.1),
shinystan (>= 2.2.1),
loo (>= 1.0.0),
loo (>= 1.1.0),
testthat,
covr,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(posterior_interval,default)
S3method(predictive_error,default)
S3method(predictive_interval,default)
S3method(prior_summary,default)
export(init_cpp)
export(log_lik)
export(loo_linpred)
export(loo_pit)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rstantools 1.2.1
(Github issue/PR numbers in parentheses)

* Add `init_cpp` function for generating `src/init.cpp` in order to pass R CMD
check in R 3.4.x. `rstan_package_skeleton` calls `init_cpp` internally. (#6)

# rstantools 1.2.0

(Github issue/PR numbers in parentheses)
Expand Down
53 changes: 53 additions & 0 deletions R/init_cpp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#' Register functions implemented in C++
#'
#' If you set up your package using \code{\link{rstan_package_skeleton}} before
#' version \code{1.2.1} of \pkg{rstantools} it may be necessary for you to call
#' this function yourself in order to pass \code{R CMD check} in \R \code{>=
#' 3.4}. If you used \code{rstan_package_skeleton} in \pkg{rstantools} version
#' \code{1.2.1} or later then this has already been done automatically.
#'
#' @export
#' @param name The name of your package as a string.
#' @param path The path to the root directory for your package as a string. If
#' not specified it is assumed that this is already the current working
#' directory.
#' @return This function is only called for its side effect of writing the
#' necessary \code{init.cpp} file to the package's \code{src/} directory.
#'
init_cpp <- function(name, path) {
file <- file.path("src", "init.cpp")
if (!missing(path))
file <- file.path(path, file)

cat(
"// Generated by the rstantools package\n\n",
"#include <R.h>",
"#include <Rinternals.h>",
"#include <R_ext/Rdynload.h>",
"#include <R_ext/Visibility.h>",
"#include <Rversion.h>",
sep = "\n",
file = file,
append = FALSE
)

cat(
"\n\nstatic const R_CallMethodDef CallEntries[] = {",
" {NULL, NULL, 0}",
"};",
sep = "\n",
file = file,
append = TRUE
)

cat(
paste0("\n\nvoid attribute_visible R_init_", name, "(DllInfo *dll) {"),
" // next line is necessary to avoid a NOTE from R CMD check",
" R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);",
" R_useDynamicSymbols(dll, TRUE); // necessary for .onLoad() to work",
"}",
sep = "\n",
file = file,
append = TRUE
)
}
4 changes: 3 additions & 1 deletion R/rstan_package_skeleton.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of rstantools
# Copyright (C) 2015, 2016 Trustees of Columbia University
# Copyright (C) 2015, 2016, 2017 Trustees of Columbia University
#
# rstantools is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -163,6 +163,8 @@ rstan_package_skeleton <-
destfile = file.path(SRC, "Makevars.win"),
quiet = TRUE
)
# register cpp (src/init.cpp)
init_cpp(name, path = DIR)

message("Updating R directory ...", domain = NA)
R <- file.path(DIR, "R")
Expand Down
26 changes: 26 additions & 0 deletions man/init_cpp.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-rstan_package_skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ test_that(".stan file included", {
test_that("R/stanmodels.R file included", {
expect_true("R/stanmodels.R" %in% pkg_files)
})
test_that("src/init.cpp file included", {
expect_true("src/init.cpp" %in% pkg_files)
init <- readLines(file.path(pkg_path, "src/init.cpp"))
expect_true(any(grepl("R_init_testPackage", init)))
})

test_that("messages are generated", {
expect_message(
Expand Down

0 comments on commit 533d90f

Please sign in to comment.