[Rcpp-devel] Problems wit rlnorm

Silkworth,David J. SILKWODJ at airproducts.com
Mon May 30 00:02:15 CEST 2011


I've made a lot of progress working through some simple issues, but this
one effects the cornerstone of my project.

Below is some sample code that shows my efforts.

I can set the seed programmatically with any integer variable in the
place of the 20 in  "seed"=20 using the environment method.
The language method could only make a fixed call.  I still can't figure
out what the RNGScope could possibly do.

I reviewed the discussion at:
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-September/0
01078.html 

This made me realize that I should go ahead and code a simple putcol
function loop when I need to populate a column in a Matrix "array".  (I
had spent some time looking for such a defined method.)

An array or Matrix class would be nice, but for now I seem to be able to
find work-arounds.

Now I am calling the r-prefixed distributions with success, but this
same code will fail on compile if the rlnorm() function is called.
src <- '		
Rcpp::RNGScope Scope;	
Environment base("package:base");	
Function SetSeed = base["set.seed"];	
int NumRands = 5;	
int NumTrials = 3;	
Rcpp::NumericVector RandCol(NumRands);	
 Rcpp::NumericMatrix RandVals(NumRands, NumTrials*2);	
SetSeed(Named("seed",20) );	
RandCol=rnorm(NumRands,2,1);	
int j=3;		
for(int i=0; i<NumRands; i++) {	
RandVals(i,j) = RandCol(i);	
}		
return RandVals;	
'		
 fun <- cxxfunction(signature(),	
 src, plugin = "Rcpp")	
		
compile error if rlnorm is called instead of rnorm:	
"file3a7d3a6f.cpp:38:28: error: 'rlnorm' was not declared in this scope"


I looked at the source files and could not figure out why this does not
work.  
Fortunately, the environment facility enabled me to have "work around"
success.
Adding these lines to the front of the  code solves the problem for now.

Environment stats("package:stats");	
Function rlnorm = stats["rlnorm"];	

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


More information about the Rcpp-devel mailing list