How to use a user-defined data C structure into an R package - r

This minimal example compiles when I "source" the file:
/*
read_header.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <RcppCommon.h>
typedef struct {
int my_data;
} MY_HEADER_INFO;
namespace Rcpp {
template <> SEXP wrap(const MY_HEADER_INFO& x);
}
#include <Rcpp.h>
namespace Rcpp {
template <>
SEXP wrap(const MY_HEADER_INFO& x) {
std::vector<std::string> names;
std::vector<SEXP> elements(1);
// do something with the elements and names
names.push_back("my_data");
elements[0] = Rcpp::wrap( x.my_data );
};
}
//' #export
// [[Rcpp::export]]
MY_HEADER_INFO read_header() {
MY_HEADER_INFO *header;
header = (MY_HEADER_INFO*)malloc(sizeof(MY_HEADER_INFO));
memset(header, 0, sizeof(MY_HEADER_INFO));
return *header;
}
When I try to build it in RStudio into a package (CMD + SHIFT + B), I get the following, long, error message, that clearly lists the problem is my user-defined return structure, MY_HEADER_INFO:
DESCRIPTION
Package: myPackage
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Depends: R (>= 4.0.0)
License: GPL-3
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.0
Imports:
Rcpp
Suggests:
knitr,
rmarkdown,
testthat
VignetteBuilder: knitr
LinkingTo:
Rcpp
NAMESPACE
# Generated by roxygen2: do not edit by hand
export(read_header)
I receive the following error:
==> Rcpp::compileAttributes()
* Updated src/RcppExports.cpp
* Updated R/RcppExports.R
==> devtools::document(roclets = c('rd', 'collate', 'namespace'))
Updating myPackage documentation
Loading myPackage
Re-compiling myPackage
─ installing *source* package ‘myPackage’ ...
** using staged installation
** libs
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include -fPIC -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
MY_HEADER_INFO read_header();
^
1 error generated.
make: *** [RcppExports.o] Error 1
ERROR: compilation failed for package ‘myPackage’
─ removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’
Error: System command 'R' failed, exit status: 1, stdout + stderr:
E> * installing *source* package ‘myPackage’ ...
E> ** using staged installation
E> ** libs
E> clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include -fPIC -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
E> RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
E> MY_HEADER_INFO read_header();
E> ^
E> 1 error generated.
E> make: *** [RcppExports.o] Error 1
E> ERROR: compilation failed for package ‘myPackage’
E> * removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’
Stack trace:
1. base:::suppressPackageStartupMessages({ ...
2. base:::withCallingHandlers(expr, packageStartupMessage = function(c) tryInv ...
3. devtools::document(roclets = c("rd", "collate", "namespace"))
4. withr::with_envvar(r_env_vars(), roxygen2::roxygenise(pkg$path, ...
5. base:::force(code)
6. roxygen2::roxygenise(pkg$path, roclets, load_code = load_code)
7. roxygen2:::load_code(base_path)
8. pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)
9. pkgbuild::compile_dll(path, quiet = quiet)
10. withr::with_makevars(compiler_flags(TRUE), assignment = "+=", ...
11. withr:::with_envvar(c(R_MAKEVARS_USER = makevars_file), { ...
12. base:::force(code)
13. base:::force(code)
14. pkgbuild:::install_min(path, dest = install_dir, components = "libs", ...
15. pkgbuild:::rcmd_build_tools("INSTALL", c(path, paste("--library=", ...
16. pkgbuild:::with_build_tools(callr::rcmd_safe(..., env = env, ...
17. callr::rcmd_safe(..., env = env, spinner = FALSE, show = FALSE, ...
18. callr:::run_r(options)
19. base:::with(options, with_envvar(env, do.call(processx::run, ...
20. base:::with.default(options, with_envvar(env, do.call(processx::run, ...
21. base:::eval(substitute(expr), data, enclos = parent.frame())
22. base:::eval(substitute(expr), data, enclos = parent.frame())
23. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmd ...
24. base:::force(code)
25. base:::do.call(processx::run, c(list(bin, args = real_cmdargs, ...
26. (function (command = NULL, args = character(), error_on_status = TRUE, ...
27. throw(new_process_error(res, call = sys.call(), echo = echo, ...
x System command 'R' failed, exit status: 1, stdout + stderr:
E> * installing *source* package ‘myPackage’ ...
E> ** using staged installation
E> ** libs
E> clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include' -I/usr/local/include -fPIC -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
E> RcppExports.cpp:9:1: error: unknown type name 'MY_HEADER_INFO'
E> MY_HEADER_INFO read_header();
E> ^
E> 1 error generated.
E> make: *** [RcppExports.o] Error 1
E> ERROR: compilation failed for package ‘myPackage’
E> * removing ‘/private/var/folders/gl/jvj9b0xn34lgq6_h9370p8q80000gn/T/RtmpLKsw0o/devtools_install_1f333d54be59/myPackage’
Execution halted
Exited with status 1.
The following is written to Rcpp_exports:
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#include <Rcpp.h>
using namespace Rcpp;
// read_header
MY_HEADER_INFO read_header();
RcppExport SEXP _myPackage_read_header() {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
rcpp_result_gen = Rcpp::wrap(read_header());
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_myPackage_read_header", (DL_FUNC) &_myPackage_read_header, 0},
{NULL, NULL, 0}
};
RcppExport void R_init_myPackage(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
If I try to modify this file, however, it all gets written over when I call CMD+SHIFT+B.
Other C programs I've written for this same package that return standard data types (e.g., "std::vector") compile without a problem, both when I "source" the code in the Console and when I build them into a package.
I have read through the Rcpp documentation and related vignettes (and even bought Dirk's book!), but I cannot find how to tell the package builder where the definition of MY_HEADER_INFO is located. How do I tell the package compiler where the definition of this file is located?
Thanks!

That look like another instance of a not-entirely-uncommon problem for which we do have a wonderfully simple answer that is somewho less known than it should be.
In short, for a package <pkg> (where <pkg> is an alias for your package name, with lower or undercase as you please. and obviously no < or >) please such struct (or in the C++ case class) or typedef or ... definitions into a file inst/include/<pkg>_types.h (replacing <pkg> with your package name).
If such a file exists, it is automagically included by RcppExports.cpp and you are good to go.
Details are in the Rcpp Attributes vignette, and a few related forms are allowed as well:
src/<pkg>_types.h
src/<pkg>_types.hpp
inst/include/<pkg>_types.h
inst/include/<pkg>_types.hpp
inst/include/<pkg>.h
but inst/include/<pkg>_types.h may be the most commonly used one. One example of using it via src/ is src/RSQLite_types.h. On the other hand, an example of using it in inst/include/ is inst/include/RcppQuantuccia_types.h and another, much larger one in inst/include/RcppGSL_types.h.

Related

How to fix undefined references for inline functions in Rcpp

I have been continuously chugging away at building an R package to use in house. It uses Rcpp and a bunch of functions defined in C++. I'm getting and error saying there is an undefined reference to two functions. Both of which are inline functions
I've tried commenting out the functions in the RcppExports, but the code is regenerated every run. I'm not sure what esle to do.
'''R package Build
==> Rcpp::compileAttributes()
* Updated src/RcppExports.cpp
* Updated R/RcppExports.R
==> Rcmd.exe INSTALL --no-multiarch --with-keep.source PHit
* installing to library 'C:/Users/kyle.m.joshi.NAE/Documents/R/win-library/3.6'
* installing *source* package 'PHit' ...
** using staged installation
** libs
c:/Rtools/mingw_64/bin/g++ -I"C:/PROGRA~1/R/R-36~1.0/include" -DNDEBUG -I"C:/Users/kyle.m.joshi.NAE/Documents/R/win-library/3.6/Rcpp/include" -O2 -Wall -mtune=generic -c RcppExports.cpp -o RcppExports.o
make: Warning: File 'RcppExports.o' has modification time 135 s in the future
RcppExports.cpp:317:15: warning: inline function 'double mils2meters(double, double)' used but never defined
inline double mils2meters(double mils, double range);
^
RcppExports.cpp:353:15: warning: inline function 'double sqrtsumsq(double, double)' used but never defined
inline double sqrtsumsq(double a, double b);
^
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o PHit.dll tmp.def Phit.o RcppExports.o hcubature.o pcubature.o -LC:/PROGRA~1/R/R-36~1.0/bin/x64 -lR
RcppExports.o:RcppExports.cpp:(.text+0x2818): undefined reference to `mils2meters(double, double)'
RcppExports.o:RcppExports.cpp:(.text+0x35f8): undefined reference to `sqrtsumsq(double, double)'
collect2.exe: error: ld returned 1 exit status
make: warning: Clock skew detected. Your build may be incomplete.
no DLL was created
ERROR: compilation failed for package 'PHit'
* removing 'C:/Users/kyle.m.joshi.NAE/Documents/R/win-library/3.6/PHit'
* restoring previous 'C:/Users/kyle.m.joshi.NAE/Documents/R/win-library/3.6/PHit'
Exited with status 1.
'''
'''RcppExort.cpp example function
inline double mils2meters(double mils, double range);
RcppExport SEXP _PHit_mils2meters(SEXP milsSEXP, SEXP rangeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< double >::type mils(milsSEXP);
Rcpp::traits::input_parameter< double >::type range(rangeSEXP);
rcpp_result_gen = Rcpp::wrap(mils2meters(mils, range));
return rcpp_result_gen;
END_RCPP
}
'''

Error when installing TDA package on R: recipe for target 'diag.o' failed

Using Ubuntu 16.04 and R 3.4.1. I get an error message when installing R package TDA. It appears to be something with making CGAL, diag.cpp, and/or diag.o (full error printout at end).
I looked closely at this:
Error when installing TDA package on R
but I have libgmp3-dev and libmpfr-dev installed (I tried removing them and then I did get that error message). I also tried removing and manually installing the Imports and LinkingTo packages for TDA (https://cran.r-project.org/web/packages/TDA/index.html), but no luck. Also tried
running R with sudo and install.packages
downloading the binaries and installing with sudo R CMD INSTALL TDA
installing CGAL
Any help appreciated.
install.packages("TDA")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'https://cran.revolutionanalytics.com/src/contrib/TDA_1.5.1.tar.gz'
Content type 'application/octet-stream' length 2008762 bytes (1.9 MB)
==================================================
downloaded 1.9 MB
* installing *source* package ‘TDA’ ...
** package ‘TDA’ successfully unpacked and MD5 sums checked
** libs
g++ -std=gnu++11 -I/usr/share/R/include -I. -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppEigen/include" -I"/usr/local/lib/R/site-library/BH/include" -DBOOST_DISABLE_THREADS -DCGAL_EIGEN3_ENABLED -DCGAL_HEADER_ONLY -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I/usr/share/R/include -I. -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/RcppEigen/include" -I"/usr/local/lib/R/site-library/BH/include" -DBOOST_DISABLE_THREADS -DCGAL_EIGEN3_ENABLED -DCGAL_HEADER_ONLY -fpic -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c diag.cpp -o diag.o
In file included from ./CGAL/Triangulation_ds_cell_base_3.h:27:0,
from ./CGAL/Triangulation_data_structure_3.h:47,
from ./CGAL/Triangulation_3.h:43,
from ./CGAL/Delaunay_triangulation_3.h:37,
from ./tdautils/cgalUtils.h:5,
from diag.cpp:25:
./CGAL/Triangulation_ds_cell_base_3.h: In instantiation of ‘void CGAL::Triangulation_ds_cell_base_3<TDS>::set_neighbor(int, CGAL::Triangulation_ds_cell_base_3<TDS>::Cell_handle) [with TDS = CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> >; CGAL::Triangulation_ds_cell_base_3<TDS>::Cell_handle = CGAL::internal::CC_iterator<CGAL::Compact_container<CGAL::Alpha_shape_cell_base_3<CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> > > >, CGAL::Boolean_tag<false>, CGAL::Boolean_tag<false> >, CGAL::Default, CGAL::Default, CGAL::Default>, false>]’:
./CGAL/Triangulation_data_structure_3.h:2782:7: required from ‘CGAL::Triangulation_data_structure_3<Vb, Cb, Concurrency_tag_>::Vertex_handle CGAL::Triangulation_data_structure_3<Vb, Cb, Concurrency_tag_>::insert_increase_dimension(CGAL::Triangulation_data_structure_3<Vb, Cb, Concurrency_tag_>::Vertex_handle) [with Vb = CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>; Cb = CGAL::Alpha_shape_cell_base_3<CGAL::Epick>; Concurrency_tag_ = CGAL::Sequential_tag; CGAL::Triangulation_data_structure_3<Vb, Cb, Concurrency_tag_>::Vertex_handle = CGAL::internal::CC_iterator<CGAL::Compact_container<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick, CGAL::Triangulation_vertex_base_3<CGAL::Epick, CGAL::Triangulation_ds_vertex_base_3<CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> > > >, CGAL::Boolean_tag<false>, CGAL::Boolean_tag<false> >, CGAL::Default, CGAL::Default, CGAL::Default>, false>]’
./CGAL/Triangulation_3.h:623:16: required from ‘void CGAL::Triangulation_3<GT, Tds, Lock_data_structure>::init_tds() [with GT = CGAL::Epick; Tds_ = CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> >; Lock_data_structure_ = CGAL::Default]’
./CGAL/Triangulation_3.h:655:15: required from ‘CGAL::Triangulation_3<GT, Tds, Lock_data_structure>::Triangulation_3(const GT&, CGAL::Triangulation_3<GT, Tds, Lock_data_structure>::Lock_data_structure*) [with GT = CGAL::Epick; Tds_ = CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> >; Lock_data_structure_ = CGAL::Default; CGAL::Triangulation_3<GT, Tds, Lock_data_structure>::Lock_data_structure = void]’
./CGAL/Delaunay_triangulation_3.h:229:26: required from ‘CGAL::Delaunay_triangulation_3<Gt, Tds_, CGAL::Default, Lock_data_structure_>::Delaunay_triangulation_3(const Gt&, CGAL::Delaunay_triangulation_3<Gt, Tds_, CGAL::Default, Lock_data_structure_>::Lock_data_structure*) [with Gt = CGAL::Epick; Tds_ = CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> >; Lock_data_structure_ = CGAL::Default; CGAL::Delaunay_triangulation_3<Gt, Tds_, CGAL::Default, Lock_data_structure_>::Lock_data_structure = void]’
./CGAL/Alpha_shape_3.h:263:51: required from ‘CGAL::Alpha_shape_3<Dt, ExactAlphaComparisonTag>::Alpha_shape_3(const InputIterator&, const InputIterator&, const NT&, CGAL::Alpha_shape_3<Dt, ExactAlphaComparisonTag>::Mode) [with InputIterator = std::_List_iterator<CGAL::Point_3<CGAL::Epick> >; Dt = CGAL::Delaunay_triangulation_3<CGAL::Epick, CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> > >; ExactAlphaComparisonTag = CGAL::Boolean_tag<false>; CGAL::Alpha_shape_3<Dt, ExactAlphaComparisonTag>::NT = double]’
diag.cpp:680:65: required from here
./CGAL/Triangulation_ds_cell_base_3.h:166:39: error: ‘class CGAL::Alpha_shape_cell_base_3<CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<CGAL::Triangulation_data_structure_3<CGAL::Alpha_shape_vertex_base_3<CGAL::Epick>, CGAL::Alpha_shape_cell_base_3<CGAL::Epick> > > >, CGAL::Boolean_tag<false>, CGAL::Boolean_tag<false> >’ has no member named ‘operator()’
CGAL_triangulation_precondition(this != n->operator());
^
./CGAL/triangulation_assertions.h:130:20: note: in definition of macro ‘CGAL_triangulation_precondition’
(CGAL::possibly(EX)?(static_cast<void>(0)): ::CGAL::precondition_fail( # EX
^
/usr/lib/R/etc/Makeconf:168: recipe for target 'diag.o' failed
make: *** [diag.o] Error 1
ERROR: compilation failed for package ‘TDA’
* removing ‘/usr/local/lib/R/site-library/TDA’
The downloaded source packages are in
‘/tmp/RtmpFqA1Fu/downloaded_packages’
Warning message:
In install.packages("TDA") :
installation of package ‘TDA’ had non-zero exit status
>
TDA v1.5.1 has the same issue on my Linux servers. Try install a previous version:
install.packages("devtools")
require(devtools);
install_version('TDA', version='1.4.1')

Rcpp Compilation ERROR: 'clang: error: no such file or directory: '/usr/local/lib/libfontconfig.a'

I was trying to run this peace of code in R (credit to the author):
require(Rcpp)
require(RcppArmadillo)
require(inline)
cosineRcpp <- cxxfunction(
signature(Xs = "matrix"),
plugin = c("RcppArmadillo"),
body='
Rcpp::NumericMatrix Xr(Xs); // creates Rcpp matrix from SEXP
int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
arma::mat Y = arma::trans(X) * X; // matrix product
arma::mat res = (1 - Y / (arma::sqrt(arma::diagvec(Y)) * arma::trans(arma::sqrt(arma::diagvec(Y)))));
return Rcpp::wrap(res);
')
And got, after few fixes, the following error:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!
clang: error: no such file or directory: '/usr/local/lib/libfontconfig.a'
clang: error: no such file or directory: '/usr/local/lib/libreadline.a'
make: *** [file5a681e35ebe1.so] Error 1
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file5a681e35ebe1.cpp 2> file5a681e35ebe1.cpp.err.txt' had status 1
I used to use Rcpp a lot in the past. But between now and then my computer has been reformatted and all the installation re-done using homebrew.
I installed cairo with brew: brew install cairo
the libreadline.a error was solved with:
brew link --force readline
But the same did not work for libfontconfig.a since was already linked:
brew link --force fontconfig
Warning: Already linked: /usr/local/Cellar/fontconfig/2.11.1
To relink: brew unlink fontconfig && brew link fontconfig
I would have assumed that fontconfig is within cairo. In fact, when I type
brew install fontconfig
Warning: fontconfig-2.11.1 already installed
But the truth is that there is no libfontconfig.a at /usr/local/lib/:
ls /usr/local/lib/libfont*
/usr/local/lib/libfontconfig.1.dylib
/usr/local/lib/libfontconfig.dylib
Using the very questionable approach of going here and download it, the code runs, but still gives a the corresponding warning, since the file corresponds to a different os.x architecture (I did not found one for 10.9):
+ . + ld: warning: ignoring file /usr/local/lib/libfontconfig.a, missing required architecture x86_64 in file /usr/local/lib/libfontconfig.a (2 slices)
So at this stage I am a little lost.
How do I install libfontconfig.a or find the 10.9 version?
In case is of any use, I have Xcode installed, I am on a Mac 10.9.5,
and based on this very nice and detailed answer my ~/.R/Makevars file looks like:
CC=clang
CXX=clang++
FLIBS=-L/usr/local/bin/
Your system setup is broken. Neither R nor Rcpp have anything to do with clang (unless you chose clang as your system compiler) or fontconfig.
So start simpler:
R> library(Rcpp)
R> evalCpp("2 + 2")
[1] 4
R>
This just showed that my system has a working compiler R (and Rcpp) can talk to. We can it more explicit:
R> evalCpp("2 + 2", verbose=TRUE)
Generated code for function definition:
--------------------------------------------------------
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP get_value(){ return wrap( 2 + 2 ) ; }
No rebuild required (use rebuild = TRUE to force a rebuild)
[1] 4
R>
and R is clever enough not to rebuild. We can then force a build
R> evalCpp("2 + 2", verbose=TRUE, rebuild=TRUE)
Generated code for function definition:
--------------------------------------------------------
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP get_value(){ return wrap( 2 + 2 ) ; }
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
// get_value
SEXP get_value();
RcppExport SEXP sourceCpp_0_get_value() {
BEGIN_RCPP
Rcpp::RObject __result;
Rcpp::RNGScope __rngScope;
__result = Rcpp::wrap(get_value());
return __result;
END_RCPP
}
Generated R functions
-------------------------------------------------------
`.sourceCpp_0_DLLInfo` <- dyn.load('/tmp/Rtmpeuaiu4/sourcecpp_6a7c7c8295fc/sourceCpp_2.so')
get_value <- Rcpp:::sourceCppFunction(function() {}, FALSE, `.sourceCpp_0_DLLInfo`, 'sourceCpp_0_get_value')
rm(`.sourceCpp_0_DLLInfo`)
Building shared library
--------------------------------------------------------
DIR: /tmp/Rtmpeuaiu4/sourcecpp_6a7c7c8295fc
/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_2.so' --preclean 'file6a7c6d1fc2d6.cpp'
ccache g++ -I/usr/share/R/include -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/tmp/Rtmpeuaiu4" -fpic -g -O3 -Wall -pipe -Wno-unused -pedantic -c file6a7c6d1fc2d6.cpp -o file6a7c6d1fc2d6.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o sourceCpp_2.so file6a7c6d1fc2d6.o -L/usr/lib/R/lib -lR
[1] 4
R>
and on that you see system details on my side (Linux, also using ccache) that will be different for you.
After that, try (Rcpp)Armadillo one-liners and so on.

How to get Rcpp and BH (Boost headers) working with regex_replace for Windows build

I'm using the excellent Rcpp project to implement some faster text processing in R, but cannot get the package to build correctly under Windows. (It builds fine under OS X and Linux.) I am using Rcpp, and the BH header packages. I can get the example at http://gallery.rcpp.org/articles/boost-regular-expressions/ and I can get the code below to build on every platform except Windows.
To isolate my problem I removed it from my larger package, and put it into a simple package so that it can be installed using devtools::install_github("kbenoit/boostTest").
The files are:
Makevars.win:
PKG_LIBS = -lboost_regex
src/clean.cpp:
#include <Rcpp.h>
#include <string>
#include <boost/regex.hpp>
// [[Rcpp::depends(BH)]]
const boost::regex re_digits("[[:digit:]]");
const boost::regex re_punct("[[:punct:]]");
const std::string space0("");
std::string removeDigits(const std::string& s) {
return boost::regex_replace(s, re_digits, space0, boost::match_default | boost::format_sed);
}
std::string removePunct(const std::string& s) {
return boost::regex_replace(s, re_punct, space0, boost::match_default | boost::format_sed);
}
// [[Rcpp::export]]
Rcpp::DataFrame cleanCpp(std::vector<std::string> str) {
int n = str.size();
for (int i=0; i<n; i++) {
str[i] = removeDigits(str[i]);
str[i] = removePunct(str[i]);
}
return Rcpp::DataFrame::create (Rcpp::Named("text") = str);
}
and cleanC.R is exported as:
cleanC <- function(x) as.character(cleanCpp(x)[, 1])
(I did this because I am so new to Rcpp that I could not figure out how to return a CharacterVector, but could get the Rcpp::DataFrame return type working by following the boost-regular-expressions example linked above.
In my DESCRIPTION file I have:
LinkingTo: Rcpp,BH
The problem is that when I build the package using devtools::build_win(), it fails, even though it builds fine on Linux and OS X. The output can be seen here:
* installing *source* package 'boostTest' ...
** libs
*** arch - i386
g++ -I"D:/RCompile/recent/R-3.1.3/include" -I"d:/RCompile/CRANpkg/lib/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.1/RcppArmadillo/include" -I"d:/RCompile/CRANpkg/lib/3.1/BH/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -I"D:/RCompile/recent/R-3.1.3/include" -I"d:/RCompile/CRANpkg/lib/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/lib/3.1/RcppArmadillo/include" -I"d:/RCompile/CRANpkg/lib/3.1/BH/include" -I"d:/RCompile/r-compiling/local/local320/include" -O3 -Wall -mtune=core2 -c clean.cpp -o clean.o
g++ -shared -s -static-libgcc -o boostTest.dll tmp.def RcppExports.o clean.o -lboost_regex -Ld:/RCompile/r-compiling/local/local320/lib/i386 -Ld:/RCompile/r-compiling/local/local320/lib -LD:/RCompile/recent/R-3.1.3/bin/i386 -lR
d:/compiler/gcc-4.6.3/bin/../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lboost_regex
collect2: ld returned 1 exit status
no DLL was created
ERROR: compilation failed for package 'boostTest'
* removing 'd:/RCompile/CRANguest/R-release/lib/boostTest'
Any help appreciated!

igraph 0.7.1 failing to install on R 3.1.0

I am trying to install igraph 0.7.1 on a macbook pro with mavericks. I have installed R 3.1.0 using macports and keep getting the following warnings / errors when installing igraph using install.packages("igraph"):
/usr/bin/clang -I/opt/local/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/opt/local/include -DUSING_R -I. -Ics -Iglpk -Iglpk/amd -Iglpk/colamd -Iplfit -pipe -Os -flax-vector-conversions -arch x86_64 -I/opt/local/include/libxml2 -pipe -Os -flax-vector-conversions -arch x86_64 -I/opt/local/include/libxml2 -DNDEBUG -DPACKAGE_VERSION=\"#VERSION#\" -DINTERNAL_ARPACK -DIGRAPH_THREAD_LOCAL=/**/ -fPIC -pipe -Os -flax-vector-conversions -arch x86_64 -c colamd.c -o colamd.o
colamd.c:1979:27: warning: implicitly declaring library function 'sqrt' with type 'double (double)'
dense_row_count = DENSE_DEGREE (knobs [COLAMD_DENSE_ROW], n_col) ;
^
colamd.c:792:33: note: expanded from macro 'DENSE_DEGREE'
((Int) MAX (16.0, (alpha) * sqrt ((double) (n))))
^
colamd.c:794:27: note: expanded from macro 'MAX'
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
^
colamd.c:1979:27: note: please include the header <math.h> or explicitly provide a declaration for 'sqrt'
colamd.c:792:33: note: expanded from macro 'DENSE_DEGREE'
((Int) MAX (16.0, (alpha) * sqrt ((double) (n))))
^
colamd.c:794:27: note: expanded from macro 'MAX'
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
^
1 warning generated.
The install then completes with the following error:
Error : .onLoad failed in loadNamespace() for 'igraph', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/opt/local/Library/Frameworks/R.framework/Versions/3.1/Resources/library/igraph/libs/igraph.so':
dlopen(/opt/local/Library/Frameworks/R.framework/Versions/3.1/Resources/library/igraph/libs/igraph.so, 10): Symbol not found: _colamd_printf
Referenced from: /opt/local/Library/Frameworks/R.framework/Versions/3.1/Resources/library/igraph/libs/igraph.so
Expected in: flat namespace
in /opt/local/Library/Frameworks/R.framework/Versions/3.1/Resources/library/igraph/libs/igraph.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/opt/local/Library/Frameworks/R.framework/Versions/3.1/Resources/library/igraph’
I have Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) installed on my machine. I would be very grateful for some help.
Thanks in advance.

Resources