Importing an Rcpp header file in NAMESPACE within an R Package - r

This is my first package in R, I already have working package but I would remove some rewriting function in cpp file, so I do an header file that work with single function.
How can I put this header in package?
Note that header.h and header.cpp are in src/ directory of package
and the #include "header.h" is in the .cpp file where I use this function
I tried to modify the NAMESPACE file with:
import(myheader)
But, when I do:
R CMD INSTALL mypackage
I receive this error:
Error: package or namespace load failed for 'mypackage' in namespaceExport(ns, exports):
undefined exports: myheader
How can I solve this error?

As #RalfStubner pointed out in the comments, the NAMESPACE file is meant for exporting and importing R functions and data.
The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:
A single function from Rcpp package is imported for registration reasons.
Generally, either evalCpp or sourceCpp is used.
Provide the name of the shared object via useDynLib(),
This is the name of the R package being built.
importFrom(Rcpp, sourceCpp)
useDynLib(<PACKAGE_NAME_HERE>, .registration = TRUE)
where <PACKAGE_NAME_HERE> is the name of the package without <>.
If you're interested in using headers to share code between R packages, consider looking at:
https://github.com/r-pkg-examples/rcpp-shared-cpp-functions
The main design pattern is using inst/include directory to place a header-only library. Then, in src/ write bindings to the library. Ensure that src/Makevars and src/Makevars.win has:
# Register where the header files for the package can be found
PKG_CXXFLAGS=-I../inst/include/
If you want to share function definitions between .cpp files in the same R package, see:
https://github.com/r-pkg-examples/rcpp-headers-src
This avoids a single monolithic .cpp file, but does not allow for sharing the compiled code routines between R packages outside of the exported R wrapper.

Related

Add RcppParallel requisites to the NAMESPACE of an R package automatically when compiling the package

I'm using RcppParallel in my own R package.
I know that I need to add Imports: RcppParallel to the DESCRIPTION file and importFrom(RcppParallel, RcppParallelLibs) to the NAMESPACE file.
My current workflow to compile my R package is:
run Rcpp::compileAttributes()
run devtools::document()
manually add importFrom(RcppParallel, RcppParallelLibs) to the NAMESPACE file
run devtools::install("MyPackage",quick = T,upgrade="never")
My question is what changes should I make to my R package, so that I can skip the manual step 3? I already add Imports: RcppParallel to the DESCRIPTION file and why does importFrom(RcppParallel, RcppParallelLibs) not show up in the NAMESPACE file after step 2?
In one of your C++ source files add this to an existing entry
//' #importFrom RcppParallel RcppParallelLibs
When you run Rcpp::compileAttributes() this gets carried over to an R file where the roxygen2 package, when running in 'full mode' also rewriting NAMESPACE will add the entry.
Use the devtools package for this. Each package you want to add to your own package add the command use_package
library(devtools)
use_package("RcppParallelw", min_version = T)
The use_package function will automatically add any entries needed in DESCRIPTION for you.
Additionally, the min_version = T option will ensure that your package requires RcppParallelw at a version not lower than you currently have installed.

How to properly use Fortran shared object in R package

I'm writing an R package with the help of devtools.
I have a R function which uses Fortran subroutines (called with .Fortran() function) from a packagename.so file located in the src folder.
I put #useDynLib packagename in R/packagename.R and ran devtools::document() to add useDynLib(packagename) to NAMESPACE but when I run devtools::check() an error occures.
I read the R packages documentation and googled the question but I haven't found a solution yet.
Two related questions come to my mind:
do I need the Fortran source code and let R compile it?
should the shared object have the same name of the package or not?
TL;DR
How do I get rid of the following error after running devtools::check()?
Error: package or namespace load failed for ‘ROCkerMeth’ in library.dynam(lib, package, package.lib):
shared object ‘ROCkerMeth.so’ not found

Add dependencies for my Rcpp package

Rcpp beginner's question:
I want to improve my execution efficiency in R. So I write some code in cpp and use Rcpp to help me compile them.
Question is that I use some other R packages in my .cpp files and I want those packages to be installed and imported automatically when a user installs my package.
e.g. If I use the R package 'gtools' in my files, I don't want the error:
* installing to library 'C:/Program Files/R/R-3.4.1/library'
* installing *source* package 'pkgname' ...
make: Nothing to be done for `all`.
** libs
installing to C:/Program Files/R/R-3.4.1/library/pkgname/libs/i386
** R
** preparing package for lazy loading
Error in library(gtools) : there is no package called 'gtools'
Error : unable to load R code in package 'pkgname'
ERROR: lazy loading failed for package 'pkgname'
* removing 'C:/Program Files/R/R-3.4.1/library/pkgname'
Exited with status 1.
I tried to add depended package name to the DESCRIPTION file. i.e.
Imports: Rcpp (>= 0.12.12),gtools
LinkingTo: Rcpp, gtools
But it gives me following error:
ERROR: dependency 'gtools' is not available for package 'pkgname'
I don't find any similar questions and please tell me if there are.
First, you should probably make sure gtools is installed on your system. I say this because of the following error:
Error in library(gtools) : there is no package called 'gtools'
With this being said, the main issue you are running into is uncertainty between the LinkingTo: and Imports: fields in the DESCRIPTION file. This is covered in Section 1.1.3: Package Dependencies of Writing R Extensions.
Specifically, we have:
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’ (see below). Ideally this field will include all the standard packages that are used, and it is important to include S4-using packages (as their class definitions can change and the DESCRIPTION file is used to decide which packages to re-install when this happens). Packages declared in the ‘Depends’ field should not also be in the ‘Imports’ field. Version requirements can be specified and are checked when the namespace is loaded (since R >= 3.0.0).
And the LinkingTo field:
A package that wishes to make use of header files in other packages needs to
declare them as a comma-separated list in the field ‘LinkingTo’ in the
DESCRIPTION file. For example
LinkingTo: link1, link2
The ‘LinkingTo’ field can have a version requirement which is checked at installation.
Specifying a package in ‘LinkingTo’ suffices if these are C++ headers containing source code or static linking is done at installation: the packages do not need to be (and usually should not be) listed in the ‘Depends’ or ‘Imports’ fields. This includes CRAN package BH and almost all users of RcppArmadillo and RcppEigen.
For another use of ‘LinkingTo’ see Linking to native routines in other packages.
So, the Imports: is meant to specify packages that contain R functions that you wish to import. In particular, the function from a given package or the entire package itself must be specified in the NAMESPACE file. For packages that use Rcpp, you can typically expect R functions to be available if the author has exported the routine from C++.
Now, regarding the LinkingTo:, this is a bit more specific. If an author wishes to make available a C++ API via header files they must explicitly declare the statements as is given in native methods of Writing R Extensions. Generally, packages that proceed in this manner are "header-only". These packages place the header definitions under inst/include, e.g.
|- pkgname
|- inst/
|- include/
|- pkgname.h
|- R/
|- man/
|- DESCRIPTION
|- NAMESPACE
However, another trend is to allow for "non-header" packages. This leads to a bit more complicated of topic as you have to understand shared objects and dynamic libraries. CRAN presents an overview of how to "Link" packages in Section 5.8: Linking to other packages of Writing R Extensions
If the author does not make available a C++ API, then there are four options:
Ask the author nicely to support calling the C++ API or submit a patch that enables access to the C++ API.
Call an R function from C++. (This negates any performance gain from writing your code in C++ though.)
Copy the implementation from the author's package while respecting their intellectual property.
Implement the desired functionality from scratch to avoid licensing issues.
Unfortunately, this is the case for gtools. As the author(s) do not provide a means to "link" to the C++ version of package's code.

install_github install package but I can't call the functions

I can see my package is installed with library(), but when I load the package from the library, I can't call any of the functions.
> install_github("pdfHarvester", "hansthompson")
> library(pdfHarvester)
> Convert
Error: object 'Convert' not found
> Parse_Tables
Error: object 'Convert' not found
You need to make sure you package actually exports the functions you want to use. This is a package development issue - not an issue with install_github.
Note that your NAMESPACE file is empty: https://github.com/hansthompson/pdfHarvester/blob/master/NAMESPACE
It looks like you're using roxygen for your .R files. If you're using devtools for the package creation you need to use document() to generate your help file and populate the NAMESPACE file.

How to define a Rcpp package based on several different cpp files?

I have define several .cpp files.
One of them needs the package RcppArmadillo;
The others need the package Rcpp.
After my Rcpp package is generated and when i install it, I compile several errors as follows:
RcppExports.cpp:49: error: ‘arma’ has not been declared
RcppExports.cpp:49: error: ‘arma’ has not been declared
RcppExports.cpp:49: error: expected `;' before ‘__result’
RcppExports.cpp:50: error: ‘__result’ was not declared in this scope
make: *** [RcppExports.o] Error 1
When I check RcppExports.cpp file in src, the include head is like this:
#include <Rcpp.h>
What should I do? How to handle this problem? Thank you very much!
This is extensively documented, and there are thirty CRAN packages using RcppArmadillo you could look at for working examples and guidance.
Start with
RcppArmadillo.package.skeleton()
to create an (almost empty) working package for RcppArmadillo, then drop your files in the src/ directory of that package.
The RcppArmadillo.package.skeleton() has options, so consider reading its help page. The Rcpp package has a lot of documentation you may want to look at too, including one entire vignette about package building.

Resources