From fa17ae55c88b7864e6adde94612c5b38e6eb3d78 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Sat, 16 Mar 2024 19:44:39 +0100 Subject: [PATCH 1/8] Add a plugin to allow the package to be called inline --- NAMESPACE | 1 + R/utils.R | 17 +++++++++++++++++ man/plugin.Rd | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 man/plugin.Rd diff --git a/NAMESPACE b/NAMESPACE index f86f448a..e4766551 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -126,6 +126,7 @@ export(icer) export(icer_tbl) export(id_attributes) export(incr_effect) +export(inlineCxxPlugin) export(input_mats) export(lm_list) export(mean_weibullNMA) diff --git a/R/utils.R b/R/utils.R index 21a16a08..35986166 100644 --- a/R/utils.R +++ b/R/utils.R @@ -377,3 +377,20 @@ get_n_samples.array <- function(x){ stopifnot(is_3d_array(x)) return(dim(x)[1]) } + +#' Code to use the hesim package inline +#' @param ... arguments +#' @rdname plugin +#' @export +inlineCxxPlugin <- function(...) { + ismacos <- Sys.info()[["sysname"]] == "Darwin" + openmpflag <- if (ismacos) "" else "$(SHLIB_OPENMP_CFLAGS)" + plugin <- Rcpp::Rcpp.plugin.maker(include.before = "#include ", + libs = paste(openmpflag, + "$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)"), + package = "hesim") + settings <- plugin() + settings$env$PKG_CPPFLAGS <- paste("-I../inst/include", openmpflag) + ## if (!ismacos) settings$env$USE_CXX11 <- "yes" + settings +} diff --git a/man/plugin.Rd b/man/plugin.Rd new file mode 100644 index 00000000..8f90f836 --- /dev/null +++ b/man/plugin.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{inlineCxxPlugin} +\alias{inlineCxxPlugin} +\title{Code to use the hesim package inline} +\usage{ +inlineCxxPlugin(...) +} +\arguments{ +\item{...}{arguments} +} +\description{ +Code to use the hesim package inline +} From d57a49a2047ce761adb22258dbca6d229e325305 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Sat, 16 Mar 2024 21:59:49 +0100 Subject: [PATCH 2/8] Add an example for the inline plugin --- R/utils.R | 16 +++++++++++++++- man/plugin.Rd | 18 ++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 35986166..776a5163 100644 --- a/R/utils.R +++ b/R/utils.R @@ -378,8 +378,22 @@ get_n_samples.array <- function(x){ return(dim(x)[1]) } -#' Code to use the hesim package inline +#' Code to use the hesim package inline. Not directly called by the user. #' @param ... arguments +#' @examples +#' library(Rcpp) +#' sourceCpp(code=" +#' // [[Rcpp::depends(hesim)]] +#' // [[Rcpp::depends(RcppArmadillo)]] +#' #include +#' // [[Rcpp::export]] +#' double test_inline_gengamma(double mu, double sigma, double Q) { +#' hesim::stats::gengamma gg(mu, sigma, Q); +#' return gg.random(); +#' }") +#' set.seed(12345) +#' test_inline_gengamma(1.0, 1.0, 1.0) + #' @rdname plugin #' @export inlineCxxPlugin <- function(...) { diff --git a/man/plugin.Rd b/man/plugin.Rd index 8f90f836..d20938b8 100644 --- a/man/plugin.Rd +++ b/man/plugin.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/utils.R \name{inlineCxxPlugin} \alias{inlineCxxPlugin} -\title{Code to use the hesim package inline} +\title{Code to use the hesim package inline. Not directly called by the user.} \usage{ inlineCxxPlugin(...) } @@ -10,5 +10,19 @@ inlineCxxPlugin(...) \item{...}{arguments} } \description{ -Code to use the hesim package inline +Code to use the hesim package inline. Not directly called by the user. +} +\examples{ +library(Rcpp) +sourceCpp(code=" +// [[Rcpp::depends(hesim)]] +// [[Rcpp::depends(RcppArmadillo)]] +#include +// [[Rcpp::export]] +double test_inline_gengamma(double mu, double sigma, double Q) { + hesim::stats::gengamma gg(mu, sigma, Q); + return gg.random(); +}") +set.seed(12345) +test_inline_gengamma(1.0, 1.0, 1.0) } From b83415a44c44634e07d7557600fd3e35094a16af Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Mon, 18 Mar 2024 12:18:55 +0100 Subject: [PATCH 3/8] Change the inline plugin to being internal -- issue with .pkgdown --- NAMESPACE | 1 - R/utils.R | 1 - 2 files changed, 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index e4766551..f86f448a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -126,7 +126,6 @@ export(icer) export(icer_tbl) export(id_attributes) export(incr_effect) -export(inlineCxxPlugin) export(input_mats) export(lm_list) export(mean_weibullNMA) diff --git a/R/utils.R b/R/utils.R index 776a5163..43092fec 100644 --- a/R/utils.R +++ b/R/utils.R @@ -395,7 +395,6 @@ get_n_samples.array <- function(x){ #' test_inline_gengamma(1.0, 1.0, 1.0) #' @rdname plugin -#' @export inlineCxxPlugin <- function(...) { ismacos <- Sys.info()[["sysname"]] == "Darwin" openmpflag <- if (ismacos) "" else "$(SHLIB_OPENMP_CFLAGS)" From 979b09820781995803e809ff85b0668760ceb678 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Mon, 18 Mar 2024 13:21:09 +0100 Subject: [PATCH 4/8] Inline plugin: add @keywords internal for .pkgdown --- R/utils.R | 2 +- man/plugin.Rd | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 43092fec..d6542d77 100644 --- a/R/utils.R +++ b/R/utils.R @@ -393,7 +393,7 @@ get_n_samples.array <- function(x){ #' }") #' set.seed(12345) #' test_inline_gengamma(1.0, 1.0, 1.0) - +#' @keywords internal #' @rdname plugin inlineCxxPlugin <- function(...) { ismacos <- Sys.info()[["sysname"]] == "Darwin" diff --git a/man/plugin.Rd b/man/plugin.Rd index d20938b8..372a1892 100644 --- a/man/plugin.Rd +++ b/man/plugin.Rd @@ -26,3 +26,4 @@ double test_inline_gengamma(double mu, double sigma, double Q) { set.seed(12345) test_inline_gengamma(1.0, 1.0, 1.0) } +\keyword{internal} From 761389c3fb625ce01261fc57387f7ec0aa5b2ae9 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Wed, 22 May 2024 19:53:57 +0200 Subject: [PATCH 5/8] Indent roxygen example --- R/utils.R | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/R/utils.R b/R/utils.R index d6542d77..f6482d0b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -381,18 +381,18 @@ get_n_samples.array <- function(x){ #' Code to use the hesim package inline. Not directly called by the user. #' @param ... arguments #' @examples -#' library(Rcpp) -#' sourceCpp(code=" -#' // [[Rcpp::depends(hesim)]] -#' // [[Rcpp::depends(RcppArmadillo)]] -#' #include -#' // [[Rcpp::export]] -#' double test_inline_gengamma(double mu, double sigma, double Q) { -#' hesim::stats::gengamma gg(mu, sigma, Q); -#' return gg.random(); -#' }") -#' set.seed(12345) -#' test_inline_gengamma(1.0, 1.0, 1.0) +#' library(Rcpp) +#' sourceCpp(code=" +#' // [[Rcpp::depends(hesim)]] +#' // [[Rcpp::depends(RcppArmadillo)]] +#' #include +#' // [[Rcpp::export]] +#' double test_inline_gengamma(double mu, double sigma, double Q) { +#' hesim::stats::gengamma gg(mu, sigma, Q); +#' return gg.random(); +#' }") +#' set.seed(12345) +#' test_inline_gengamma(1.0, 1.0, 1.0) #' @keywords internal #' @rdname plugin inlineCxxPlugin <- function(...) { From 94f234663363498dd5d6900d6e00107bcea19058 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Wed, 22 May 2024 19:54:43 +0200 Subject: [PATCH 6/8] Add emacs temp files to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6b00f852..ea853699 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ src/*.gcda doc Meta *_cache* +*~ From a720bb6f560794559bf77d0c410698099764b8e7 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Wed, 22 May 2024 19:55:06 +0200 Subject: [PATCH 7/8] Add test for inline code --- tests/testthat/test-inline.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/testthat/test-inline.R diff --git a/tests/testthat/test-inline.R b/tests/testthat/test-inline.R new file mode 100644 index 00000000..ef2f77e8 --- /dev/null +++ b/tests/testthat/test-inline.R @@ -0,0 +1,17 @@ +context("inline unit tests") +library("Rcpp") + +# Partitioned survival fits --------------------------------------------------- +test_that("Show that inline works for a simple example ", { + sourceCpp(code=" +// [[Rcpp::depends(hesim)]] +// [[Rcpp::depends(RcppArmadillo)]] +#include +// [[Rcpp::export]] +double test_inline_gengamma(double mu, double sigma, double Q) { + hesim::stats::gengamma gg(mu, sigma, Q); + return gg.random(); +}") + set.seed(12345) + expect_true(abs(test_inline_gengamma(1.0, 1.0, 1.0) - 2.717582) < 1e-5) +}) From 5d348445c839135f0c38521dd701c5fa73fa08b2 Mon Sep 17 00:00:00 2001 From: Mark Clements Date: Sat, 6 Jul 2024 12:47:38 +0200 Subject: [PATCH 8/8] Remove test-inline.R -- unable to use test() inline with src --- tests/testthat/test-inline.R | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 tests/testthat/test-inline.R diff --git a/tests/testthat/test-inline.R b/tests/testthat/test-inline.R deleted file mode 100644 index ef2f77e8..00000000 --- a/tests/testthat/test-inline.R +++ /dev/null @@ -1,17 +0,0 @@ -context("inline unit tests") -library("Rcpp") - -# Partitioned survival fits --------------------------------------------------- -test_that("Show that inline works for a simple example ", { - sourceCpp(code=" -// [[Rcpp::depends(hesim)]] -// [[Rcpp::depends(RcppArmadillo)]] -#include -// [[Rcpp::export]] -double test_inline_gengamma(double mu, double sigma, double Q) { - hesim::stats::gengamma gg(mu, sigma, Q); - return gg.random(); -}") - set.seed(12345) - expect_true(abs(test_inline_gengamma(1.0, 1.0, 1.0) - 2.717582) < 1e-5) -})