Rcpp and Roxygen2

[This article was first published on r - Brandon Bertelsen, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Instructions for using Rcpp and Roxygen2 together. This assumes that Roxygen2 is managing your namespace:

DESCRIPTION

  • In your DESCRIPTION file, add the line LinkingTo: Rcpp
  • Also ensure that you import Rcpp, Imports: Rcpp along with all the other packages are imported in your namespace.

Package Documentation

Your package documentation (by convention in zzz.R or package.R) you should have the following:

#' your_package
#' 
#' Description of your package
#' 
#' @docType package
#' @author you 
#' @import Rcpp another_package another
#' @importFrom Rcpp evalCpp
#' @useDynLib your_package
#' @name your_package
NULL  

Functions

  • Add *.cpp files to /src directory.
  • Remember to use //' as your comment so that Roxygen2 picks it up appropriately.
  • Remember to include [[Rcpp:export]] so that it is picked up by Rcpp::compileAttributes() when you build and reload or run roxygenize() manually as per your project options
#include 

using namespace Rcpp;

//' Leading NA
//' 
//' This function returns a logical vector identifying if 
//' there are leading NA, marking the leadings NA as TRUE and
//' everything else as FALSE.
//'
//' @param x An integer vector
//' @export
// [[Rcpp::export]]
LogicalVector leading_na(IntegerVector x) {  
  int n = x.size();
  LogicalVector leading_na(n);

  int i = 0;
  while((i < n) &&(x[i] == NA_INTEGER)) {
    leading_na[i] = TRUE;
    i++;
  }
  return leading_na;
}

To leave a comment for the author, please follow the link and comment on their blog: r - Brandon Bertelsen.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)