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

Dirk Eddelbuettel edd at debian.org
Sun Dec 14 16:47:30 CET 2014


On 14 December 2014 at 15:23, Dr R. Dybowski wrote:
| Hi,
| 
| 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(){
|   checkEnv();
|   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

You just did.

The vignettes Rcpp-FAQ and Rcpp-attributes will be of interest to you.

| 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.

The question is, and pardon my bluntness, so ill posed that you may have
found few predecessors.

You are coming from the wrong direction. Do no think standalone C++ development.

Think "writing an extension for R" using a (in our view, at least) simpler
interface.  And for "writing an extension for R" you will also find a manual,
right there in your R installation.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list