Rcpp and Roxygen2

August 27, 2016

(This article was first published on r - Brandon Bertelsen, and kindly contributed to R-bloggers)

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


  • 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


  • 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

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;
  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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


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)