Professional Documents
Culture Documents
Brian J. Knaus
Brian J. Knaus’s blog about genomics and biology.
The use of header files is best illustrated by adding them to an R package. In order
to illustrarte their use, we’ll need to create a minimal R package to work with. I
suggest using the R package pkgKitten which can be installed from CRAN if you do Links
not already have it. This site on GitHub
vcfR
R> library(pkgKitten)
vcfR on GitHub
R> kitten(name = "myRpackage")
Once we’ve created our package we can validate that it passes CRAN tests as
follows.
New submission
bash$ cd myRpackage
bash$ R
R> library(devtools)
R> use_rcpp()
You’ll want to follow the directions at the prompt. My solution was to add lines to
the hello.R file. I’ve also modified this file from the one created by pkgKitten to
include Roxygen comments.
File: R/hello.R
We can now add a header file as well as some Rcpp code files to our package. We’ll
add a total of three files. The first file will be named modString.h. This is our header
file and it declares our function. The second file will be named modString.cpp. This is
the function that is declared in the header file. Lastly, we’ll create myFunction.cpp.
This is Rcpp code that will call the function that is declared in the header file. These
files are presented below.
File: modString.h
#ifndef MODSTRING_H
#define MODSTRING_H
#include <Rcpp.h>
Rcpp::StringVector modString(Rcpp::StringVector myStringV);
#endif
#ifndef MODSTRING_H
#define MODSTRING_H
...
#endif
This is the ‘header guard’ and protects you from making multiple definitions of this
code. This should always be a part of your header files.
File: modString.cpp
#include <string>
#include <sstream>
#include <Rcpp.h>
File: myFunction.cpp
#include <Rcpp.h>
#include "modString.h"
NAMESPACE file
The pkgKitten creates a NAMESPACE file for us. However, I like to use the Roxygen
functionality to manage my NAMESPACE files. Because the pkgKitten NAMESPACE
file was not created using Roxygen it will complain when we use
devtools::document(). This is a problem because we do need to export the function
we created to our namespace. I suggest creating your own NAMESPACE file as
illustrated below. Once you’ve created this file it will be updated by Roxygen when
documenting your package.
FILE: NAMESPACE
export(hello)
export(myFunction)
importFrom(Rcpp,sourceCpp)
useDynLib(myRpackage)
Because we’ve added Roxygen comments to the hello.R file, we’ll want to remove its
*.Rd so that Roxygen can generate one. Then we’ll want to document the project
using devtools.
bash$ rm man/hello.Rd
bash$ R
R> devtools::document()
R> q()
bash$ cd ..
I generate the same NOTE mentioned above, and we can ignore it again.
Usage
We should now be able to install our package and test it.
Links
These are sources for information I used to create this post.
In order to find examples of header files used in R packages you can search GitHub
using the following query.
user:cran extension:.h
Make sure you search for ‘Code’ instead of the default search of ‘Repositories’ so
that you return code examples.
Share
2 Comments knausb.github.io
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy
Back to top