2
votes

I'm having some trouble with Rcpp functions which use boost objects when running them on RStudio 0.99. This issue does not occur in previous releases of RStudio (0.98) neither in the R console.

This is a sample of the cpp file that I'm using. There are two simple functions, f1 not using any boost date, and f2 with some elemental operation involving dates:

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>

using namespace Rcpp;
using namespace boost::posix_time;
using namespace boost::gregorian;
using namespace boost::local_time;

// [[Rcpp::export]]
int f1(int x, int y)
{
return x+y;
}

// [[Rcpp::export]]
int f2(const int hour, const int day, const int month, const int year) {

ptime pt(date(year,month,day), hours(hour));
time_zone_ptr zone(new posix_time_zone("UTC"));
local_date_time fecha(pt, zone);

double HoraUTC = fecha.utc_time().time_of_day().total_seconds() / 3600.0;

return HoraUTC;
}

Compilation through Rcpp::sourceCpp works ok, returning this warning about one of BH header files:

C:/R/RCurrent/library/BH/include/boost/datetime/posixtime/posixtimeconfig.hpp:73:79: warning: 'result' may be used uninitialized in this function [-Wuninitialized]

Function f1 runs normally, but when calling f2, a typical R session crash occurs. As I have told before, this never happens in the R console nor using previous versions of RStudio.

Session Info:

sessionInfo()
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LCCOLLATE=SpanishSpain.1252 LCCTYPE=SpanishSpain.1252 LCMONETARY=SpanishSpain.1252
[4] LCNUMERIC=C LCTIME=Spanish_Spain.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] Rcpp_0.12.0

loaded via a namespace (and not attached):
[1] tools3.2.0 BH1.58.0-1

Any idea?

Thanks!

1

1 Answers

2
votes

After fixing two simple errors (you were lacking an underscore each in posix_time and local_time) your code works fine here -- all current version of R, Rcpp and BH on Ubuntu 15.04:

R> sourceCpp("soquestion.cpp")

R> f1(2,3)
[1] 5

R> f2(12, 12, 12, 2015)
[1] 12
R> 

Maybe something happened which made your (binary ?) package installations to get out of whack. You can always install from source ...