tak0kadaの何でもノート

発声練習、生存確認用。

医学関連は 医学ノート

R Package development using Rcpp

Minimal procedure to make R package

tak0kada@mypc:~$ r
> library(devtools)
> devtools::create("~/myPkg")
> setwd("~/myPkg") # working directory
> usethis::use_rcpp() # setting for rcpp, not work in rstudio, for me

After calling use_rcpp, R program requires you to fill in the roxygen generated file. Then, write down the following statements, to complete NAMESPACE.

exportPattern("^[^\\.]")
useDynLib(myPkg, .registration=TRUE)
importFrom(Rcpp, sourceCpp)

And put for now put sample.cpp below in the ~/myPkg/src.

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
}

Then get back to R console, type

> devtools::load_all() # Compile

Complete!!

When you use your personal library,

> library(devtools)
> devtools::load_all("~/myPkg")
> myPkg::your_function()