How do I pass in a map/dictionary/list from R as a parameter to a c++ function?
For example, I want to do something like the following:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int test(List map) {
int val = map["test"];
return(val);
}
/*** R
map <- list(test = 200, hello = "a")
test(map)
*/
where the output should be 200.
I have a similar problem on Mac OS X. Running your snippet seems to always return 1
. However, if I modify the code in the following way it works:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int test(List map) {
int val = as<int>( map["test"] );
return(val);
}
/*** R
map <- list(test = 200, hello = "a")
test(map)
*/
It seems like something is going wrong with the type inference -- the compiler should "know" that, since we're assigning map["test"]
to an int
-declared variable that it should be converted as int
, but this does not seem to be the case. So, to be safe -- be sure to as
anything that's coming out of an R list.
Also, it's worth stating: in R 200
is a double
; if you want to explicitly pass an int
you should write 200L
.
FWIW, I'm compiling with clang++
:
> clang++ -v
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
and
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_0.10.4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With