diff --git a/DESCRIPTION b/DESCRIPTION index 00c0c2a..f092bab 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", @@ -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, diff --git a/NAMESPACE b/NAMESPACE index bc85837..98c8baf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 1ee5c12..0a8428e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) diff --git a/R/init_cpp.R b/R/init_cpp.R new file mode 100644 index 0000000..8ba0b40 --- /dev/null +++ b/R/init_cpp.R @@ -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 ", + "#include ", + "#include ", + "#include ", + "#include ", + 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 + ) +} diff --git a/R/rstan_package_skeleton.R b/R/rstan_package_skeleton.R index 6d04a5f..9cee499 100644 --- a/R/rstan_package_skeleton.R +++ b/R/rstan_package_skeleton.R @@ -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 @@ -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") diff --git a/man/init_cpp.Rd b/man/init_cpp.Rd new file mode 100644 index 0000000..a041832 --- /dev/null +++ b/man/init_cpp.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/init_cpp.R +\name{init_cpp} +\alias{init_cpp} +\title{Register functions implemented in C++} +\usage{ +init_cpp(name, path) +} +\arguments{ +\item{name}{The name of your package as a string.} + +\item{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.} +} +\value{ +This function is only called for its side effect of writing the + necessary \code{init.cpp} file to the package's \code{src/} directory. +} +\description{ +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. +} diff --git a/tests/testthat/test-rstan_package_skeleton.R b/tests/testthat/test-rstan_package_skeleton.R index e7f55cb..99e3ed1 100644 --- a/tests/testthat/test-rstan_package_skeleton.R +++ b/tests/testthat/test-rstan_package_skeleton.R @@ -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(