Hi, I'm using the Rcpp package to do some operations in C++. For example reading large files and do some calculations. I call the C++ function several times, so I would like the C++ environment keep the objects and variables in the memory when I'm going back to R. So my question; is it possible to set variables, read in files, etc. in C++ and keep them in the memory until I call the function next time? Another question, is this the right mailing list for how-to-use-Rcpp-questions, or is it only for the developing of Rcpp?
[Rcpp-devel] Keep variables in C++ environment
4 messages · Mattias Nyström, Romain Francois, Dirk Eddelbuettel
On 01/27/2010 05:29 PM, Mattias Nystr?m wrote:
Hi, I'm using the Rcpp package to do some operations in C++. For example reading large files and do some calculations. I call the C++ function several times, so I would like the C++ environment keep the objects and variables in the memory when I'm going back to R. So my question; is it possible to set variables, read in files, etc. in C++ and keep them in the memory until I call the function next time?
With the new api, i.e. the classes in the Rcpp namespace it is definitely possible. This was one of the goals. So if you do something like this : Rcpp::CharacterVector x(2) ; x[0] = "foo" ; x[1] = "bar" ; You can then put x wherever you like and use it when you come back. With the classic api, it is not possible I think. Dirk might prove me wrong.
Another question, is this the right mailing list for how-to-use-Rcpp-questions, or is it only for the developing of Rcpp?
That's the right list.
Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/KfKn : Rcpp 0.7.2 |- http://tr.im/JOlc : External pointers with Rcpp `- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009
On 27 January 2010 at 17:35, Romain Francois wrote:
| On 01/27/2010 05:29 PM, Mattias Nystr?m wrote:
| > Hi,
| >
| > I'm using the Rcpp package to do some operations in C++. For example reading large files and do some calculations. I call the C++ function several times, so I would like the C++ environment keep the objects and variables in the memory when I'm going back to R. So my question; is it possible to set variables, read in files, etc. in C++ and keep them in the memory until I call the function next time?
|
| With the new api, i.e. the classes in the Rcpp namespace it is
| definitely possible. This was one of the goals.
|
| So if you do something like this :
|
| Rcpp::CharacterVector x(2) ;
| x[0] = "foo" ;
| x[1] = "bar" ;
|
| You can then put x wherever you like and use it when you come back.
|
| With the classic api, it is not possible I think. Dirk might prove me wrong.
Aren't we talking about standard static variables?
edd at ron:/tmp> cat staticVar.cpp
#include <iostream>
void foo(void) {
static int ctr;
std::cout << "Ctr now " << ++ctr << std::endl;
}
int main(void) {
foo();
foo();
foo();
}
edd at ron:/tmp> g++ -o staticVar staticVar.cpp
edd at ron:/tmp> ./staticVar
Ctr now 1
Ctr now 2
Ctr now 3
edd at ron:/tmp>
Seems to fit the bill, no?
Dirk
Three out of two people have difficulties with fractions.
Static variables was a simple and working solution for my problem! Thanks! -----Original Message----- From: Dirk Eddelbuettel [mailto:edd at debian.org] Sent: den 27 januari 2010 18:10 To: Romain Francois Cc: Mattias Nystr?m; rcpp-devel at lists.r-forge.r-project.org Subject: Re: [Rcpp-devel] Keep variables in C++ environment
On 27 January 2010 at 17:35, Romain Francois wrote:
| On 01/27/2010 05:29 PM, Mattias Nystr?m wrote:
| > Hi,
| >
| > I'm using the Rcpp package to do some operations in C++. For example reading large files and do some calculations. I call the C++ function several times, so I would like the C++ environment keep the objects and variables in the memory when I'm going back to R. So my question; is it possible to set variables, read in files, etc. in C++ and keep them in the memory until I call the function next time?
|
| With the new api, i.e. the classes in the Rcpp namespace it is
| definitely possible. This was one of the goals.
|
| So if you do something like this :
|
| Rcpp::CharacterVector x(2) ;
| x[0] = "foo" ;
| x[1] = "bar" ;
|
| You can then put x wherever you like and use it when you come back.
|
| With the classic api, it is not possible I think. Dirk might prove me wrong.
Aren't we talking about standard static variables?
edd at ron:/tmp> cat staticVar.cpp
#include <iostream>
void foo(void) {
static int ctr;
std::cout << "Ctr now " << ++ctr << std::endl;
}
int main(void) {
foo();
foo();
foo();
}
edd at ron:/tmp> g++ -o staticVar staticVar.cpp
edd at ron:/tmp> ./staticVar
Ctr now 1
Ctr now 2
Ctr now 3
edd at ron:/tmp>
Seems to fit the bill, no?
Dirk
Three out of two people have difficulties with fractions.