[Rcpp-devel] Accessing values contained in an R file using C++

Dr R. Dybowski rd460 at cam.ac.uk
Sun Dec 14 16:23:55 CET 2014



Sorry for the newbie question but I wish to access values contained in
an R file (foo.r) using C++ (source code in bar.cpp). I am using R
(3.0.1) via RStudio (0.98.501) under Ubuntu 13.10. 

>>>> The content of foo.r is: 

someNumber <<- 42
stooges <<- c("moe", "larry", "curly") 

>>>> The content of bar.cpp is: 

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int checkEnv() {
 // access the global env.
 Environment env = Environment::global_env();
 CharacterVector v = env["stooges"];
 Rcout << "Stooge Nb 2 is: " << v[1] << std::endl;
 return env["someNumber"];

int main(){
 return 0;

I have run Rcpp::sourceCpp('foo.r') from within RStudio. 

Question: How do I compile and run the C++ file from within RStudio? If
I try to compile it outside RStudio using g++ bar.cpp -o bar, the header
file Rcpp.h is not recognised. No doubt this has been asked before but I
cannot find the answer and I would be most grateful for assistance. 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20141214/f6db84a2/attachment.html>

More information about the Rcpp-devel mailing list