[Rcpp-devel] package .onLoad multiple modules
Dear list, [Disclaimer: I am not very familiar with Rcpp/C++, and probably haven't followed all the relevant discussions on this list.] I'm trying to tidy up two packages on r-forge [*] before submitting them to CRAN. In cda/zzz.r, I have the following code, NAMESPACE - environment() cda - new( Module ) cdatests - new( Module ) cd - new( Module ) linear - new( Module ) array - new( Module ) .onLoad - function(libname, pkgname){ unlockBinding( cda , NAMESPACE ) unlockBinding( cdatests , NAMESPACE ) unlockBinding( cd , NAMESPACE ) unlockBinding( linear , NAMESPACE ) assign( cda, Module( cda ), NAMESPACE ) assign( cdatests, Module( cdatests ), NAMESPACE ) assign( cd, Module( cd ), NAMESPACE ) assign( linear, Module( linear ), NAMESPACE ) lockBinding( cda, NAMESPACE ) lockBinding( cdatests, NAMESPACE ) lockBinding( cd, NAMESPACE ) lockBinding( linear, NAMESPACE ) unlockBinding( array , NAMESPACE ) assign( array, Module( array ), NAMESPACE ) lockBinding( array, NAMESPACE ) } It seems to work, but is there something I can/should do to make this a wee cleaner? Thanks! baptiste [*]: https://r-forge.r-project.org/scm/viewvc.php/pkg/cda/?root=photonics https://r-forge.r-project.org/scm/viewvc.php/pkg/planar/?root=photonics ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] package .onLoad multiple modules
On 29 May 2011 at 18:56, baptiste auguie wrote: | Dear list, | | [Disclaimer: I am not very familiar with Rcpp/C++, and probably | haven't followed all the relevant discussions on this list.] | | I'm trying to tidy up two packages on r-forge [*] before submitting | them to CRAN. In cda/zzz.r, I have the following code, | | NAMESPACE - environment() | cda - new( Module ) | cdatests - new( Module ) | cd - new( Module ) | linear - new( Module ) | array - new( Module ) | | .onLoad - function(libname, pkgname){ | unlockBinding( cda , NAMESPACE ) | unlockBinding( cdatests , NAMESPACE ) | unlockBinding( cd , NAMESPACE ) | unlockBinding( linear , NAMESPACE ) | assign( cda, Module( cda ), NAMESPACE ) | assign( cdatests, Module( cdatests ), NAMESPACE ) | assign( cd, Module( cd ), NAMESPACE ) | assign( linear, Module( linear ), NAMESPACE ) | lockBinding( cda, NAMESPACE ) | lockBinding( cdatests, NAMESPACE ) | lockBinding( cd, NAMESPACE ) | lockBinding( linear, NAMESPACE ) | | unlockBinding( array , NAMESPACE ) | assign( array, Module( array ), NAMESPACE ) | lockBinding( array, NAMESPACE ) | | } | | It seems to work, but is there something I can/should do to make this | a wee cleaner? Have another look at the Rcpp-modules vignette, and/or the third set of slides (Advanced Rcpp) from our class in April -- you no longer need the unlockBinding / lockBinding business as Romain internalized that. The skeleton-generated packages now just do this in zzz.R: .onLoad - function(pkgname, libname){ loadRcppModules() } using a 'RcppModules: cda, cdatests, cd, linear, array' declaration in DESCRIPTION. The question Laurent raised the other day about spurious warnings from 'R CMD check' around this still stands though. Not yet sure what to do there... Cheers, Dirk -- Gauss once played himself in a zero-sum game and won $50. -- #11 at http://www.gaussfacts.com ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
[Rcpp-devel] ‘_’ was not declared in this scope
Hi all, Apologies for the newbie question, but I am trying to build my first R package with a Rcpp function, and I am running into some issues with Sugar expressions not compiling. I tried to model my package on one of the RcppExamples, and put the code pasted below in the file /src/ar1.cpp. There's also an R function in /R/ar1.R with a simple .Call to the c++ function. I build the package, but when I try to install it from source using 'R CMD INSTALL mypackage.tar.gz', I get the error pasted below. But the example works fine with the inline package. I am running R 2.12 on Ubuntu 11.04. Thanks for your time! Vincent ### Error: ar1.cpp: In function ‘SEXPREC* ar1(SEXPREC*, SEXPREC*)’: ar1.cpp:8:11: error: ‘_’ was not declared in this scope make: *** [ar1.o] Error 1 ERROR: compilation failed for package ‘mypackage’ ### Code: #include Rcpp.h RcppExport SEXP ar1(SEXP xbe, SEXP g){ Rcpp::NumericMatrix xbem(xbe); int nrows = xbem.nrow(); Rcpp::NumericVector gv(g); for (int i = 1; i nrows; i++) { xbem(i,_) = xbem(i-1,_) * gv[0] + xbem(i,_); } return xbem; } ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
[Rcpp-devel] Problems wit rlnorm
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; iNumRands; 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]; ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
[Rcpp-devel] trans() changed in latest RcppArmadillo
Dear list, The latest RcppArmadillo broke some code that worked for me before, and produces erroneous results. I have just tracked down the origin of the problem to a change in the armadillo library: applying trans() to complex matrices now takes the complex conjugate as well as transposing. Using strans() restores the original behavior. I though I'd warn other RcppArmadillo users to check their code if it uses complex linear algebra. Rather curiously, strans() cannot be applied on real matrices, so trans() should be kept there. Best regards, baptiste ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] package .onLoad multiple modules
Hi, On 30 May 2011 04:28, Dirk Eddelbuettel e...@debian.org wrote: On 29 May 2011 at 18:56, baptiste auguie wrote: | Dear list, | | [Disclaimer: I am not very familiar with Rcpp/C++, and probably | haven't followed all the relevant discussions on this list.] | | I'm trying to tidy up two packages on r-forge [*] before submitting | them to CRAN. In cda/zzz.r, I have the following code, | | NAMESPACE - environment() | cda - new( Module ) | cdatests - new( Module ) | cd - new( Module ) | linear - new( Module ) | array - new( Module ) | | .onLoad - function(libname, pkgname){ | unlockBinding( cda , NAMESPACE ) | unlockBinding( cdatests , NAMESPACE ) | unlockBinding( cd , NAMESPACE ) | unlockBinding( linear , NAMESPACE ) | assign( cda, Module( cda ), NAMESPACE ) | assign( cdatests, Module( cdatests ), NAMESPACE ) | assign( cd, Module( cd ), NAMESPACE ) | assign( linear, Module( linear ), NAMESPACE ) | lockBinding( cda, NAMESPACE ) | lockBinding( cdatests, NAMESPACE ) | lockBinding( cd, NAMESPACE ) | lockBinding( linear, NAMESPACE ) | | unlockBinding( array , NAMESPACE ) | assign( array, Module( array ), NAMESPACE ) | lockBinding( array, NAMESPACE ) | | } | | It seems to work, but is there something I can/should do to make this | a wee cleaner? Have another look at the Rcpp-modules vignette, and/or the third set of slides (Advanced Rcpp) from our class in April -- you no longer need the unlockBinding / lockBinding business as Romain internalized that. The skeleton-generated packages now just do this in zzz.R: .onLoad - function(pkgname, libname){ loadRcppModules() } using a 'RcppModules: cda, cdatests, cd, linear, array' declaration in DESCRIPTION. Strange, this does not seem to work for me. If I use the following zzz.r, (with RcppModules listed in DESCRIPTION) .onLoad - function(libname, pkgname){ loadRcppModules() } I get a segfault whenever I use a c++ function, *** caught segfault *** address 0x0, cause 'memory not mapped' Traceback: 1: .External(list(name = InternalFunction_invoke, address = pointer: 0x101407f30, dll = list(name = Rcpp, path = /Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/libs/x86_64/Rcpp.so, dynamicLookup = TRUE, handle = pointer: 0x101441030, info = pointer: 0x1002b3ed0), numParameters = -1L), pointer: 0x103011050, ...) 2: circular_dichroism_spectrum(clust, gold, n = 1.33, N = 36, progress = FALSE) 3: onecluster() Is there another step I'm missing? Best regards, baptiste The question Laurent raised the other day about spurious warnings from 'R CMD check' around this still stands though. Not yet sure what to do there... Cheers, Dirk -- Gauss once played himself in a zero-sum game and won $50. -- #11 at http://www.gaussfacts.com ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Problems wit rlnorm
Hi David, On 29 May 2011 at 18:02, Silkworth,David J. wrote: | 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. You can do that more simply at the R level. So I would just do set.seed(20) fun() # calling the function you defined | The language method could only make a fixed call. I still can?t figure out | what the RNGScope could possibly do. Can you possibly go back and read the list archives? That was discussed at some length when we did most of the sugar work -- in essence it just brings the sane state of the R RNGs to our C++ projects, and then returns it. All automatically witin the scope. | I reviewed the discussion at: | | http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-September/ | 001078.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.) Yes, you should also be able to assign a column directly. There are other examples. | 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; iNumRands; i++) { | RandVals(i,j) = RandCol(i); | } | return RandVals; | ' | fun - cxxfunction(signature(), | src, plugin = Rcpp) I would write this as follows (forgetting for a moment that you do not need to loop to assign the NumRands values into the matrix): src - ' Rcpp::RNGScope Scope; int NumRands = 5; int NumTrials = 3; Rcpp::NumericVector RandCol(NumRands); Rcpp::NumericMatrix RandVals(NumRands, NumTrials*2); RandCol = rnorm(NumRands,2,1); int j=3; for (int i=0; iNumRands; i++) { RandVals(i,j) = RandCol(i); } return RandVals; ' fun - cxxfunction(signature(), body = src, plugin = Rcpp, include=#include Rcpp/stats/random/rlnorm.h) set.seed(20) fun() so no need for the Language or Enviornment objects. | 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. I am a little stumped too. I also failed to get it to work. I will take another look later --- this may in fact be a bug. | 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]; Yes, very good trick to get it from R -- but you should also be able to access the rlnorm functions defined via include/Rcpp/stats/random/rlnorm.h Dirk -- Gauss once played himself in a zero-sum game and won $50. -- #11 at http://www.gaussfacts.com ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] Problems wit rlnorm
David, On 29 May 2011 at 18:02, Dirk Eddelbuettel wrote: | I am a little stumped too. I also failed to get it to work. I will take | another look later --- this may in fact be a bug. It _was_ a bug and seemingly nobody but you ever tried rnlorm :) There was a silly typo bug inasmuch as the header guard used _norm_ not _lnorm_ and with the other term already defined, this header file was never included. We also need a few more fixed making exp() explicitly call the C math library one, and correcting a typo. If you are comfortable applying a diff, try this one from rev3038. Cheers, Dirk Index: inst/include/Rcpp/stats/random/rlnorm.h === --- inst/include/Rcpp/stats/random/rlnorm.h (revision 3029) +++ inst/include/Rcpp/stats/random/rlnorm.h (working copy) @@ -19,8 +19,8 @@ // You should have received a copy of the GNU General Public License // along with Rcpp. If not, see http://www.gnu.org/licenses/. -#ifndef Rcpp__stats__random_norm_h -#define Rcpp__stats__random_norm_h +#ifndef Rcpp__stats__random_lnorm_h +#define Rcpp__stats__random_lnorm_h namespace Rcpp { namespace stats { @@ -32,7 +32,7 @@ meanlog(meanlog_), sdlog(sdlog_) {} inline double operator()() const { - return exp( meanlog + sdlog * ::norm_rand() ) ; + return ::exp( meanlog + sdlog * ::norm_rand() ) ; } private: @@ -47,7 +47,7 @@ meanlog(meanlog_) {} inline double operator()() const { - return exp( meanlog + ::norm_rand() ) ; + return ::exp( meanlog + ::norm_rand() ) ; } private: @@ -57,10 +57,10 @@ class LNormGenerator_0 : public Generatorfalse,double { public: - LNormGenerator_1( ) {} + LNormGenerator_0( ) {} inline double operator()() const { - return exp(::norm_rand() ) ; + return ::exp(::norm_rand() ) ; } } ; @@ -75,7 +75,7 @@ // TODO: R also throws a warning in that case, should we ? return NumericVector( n, R_NaN ) ; } else if (sdlog == 0. || !R_FINITE(meanlog)){ - return NumericVector( n, exp( meanlog ) ) ; + return NumericVector( n, ::exp( meanlog ) ) ; } else { return NumericVector( n, stats::LNormGenerator( meanlog, sdlog ) ); } -- Gauss once played himself in a zero-sum game and won $50. -- #11 at http://www.gaussfacts.com ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] package .onLoad multiple modules
Thanks, that is helpful. I think I have nailed down the problem. I defined a wrapper at R level that used a C++ function from a module named cd, circular_dichroism_spectrum - function( ... ){ [...] # some argument processing res - cd$circular_dichroism_spectrum(...) # calling the C++ function return(res) } Now, with loadRcppModules(), no cd module is created, as far as I can tell, and invoking circular_dichroism_spectrum() is calling the C++ function directly; a segfault results because some of the arguments differ between the C++ function and its R wrapper. I see two options: - choose a different name for the R and C++ functions - manage some kind of namespace masking the C++ function from the top-level, like cd$ did before. Is there a way to do this in the loadRcppModules() approach? Best regards, baptiste On 30 May 2011 11:11, Dirk Eddelbuettel e...@debian.org wrote: On 30 May 2011 at 10:29, baptiste auguie wrote: | Hi, | | On 30 May 2011 04:28, Dirk Eddelbuettel e...@debian.org wrote: | | On 29 May 2011 at 18:56, baptiste auguie wrote: | | Dear list, | | | | [Disclaimer: I am not very familiar with Rcpp/C++, and probably | | haven't followed all the relevant discussions on this list.] | | | | I'm trying to tidy up two packages on r-forge [*] before submitting | | them to CRAN. In cda/zzz.r, I have the following code, | | | | NAMESPACE - environment() | | cda - new( Module ) | | cdatests - new( Module ) | | cd - new( Module ) | | linear - new( Module ) | | array - new( Module ) | | | | .onLoad - function(libname, pkgname){ | | unlockBinding( cda , NAMESPACE ) | | unlockBinding( cdatests , NAMESPACE ) | | unlockBinding( cd , NAMESPACE ) | | unlockBinding( linear , NAMESPACE ) | | assign( cda, Module( cda ), NAMESPACE ) | | assign( cdatests, Module( cdatests ), NAMESPACE ) | | assign( cd, Module( cd ), NAMESPACE ) | | assign( linear, Module( linear ), NAMESPACE ) | | lockBinding( cda, NAMESPACE ) | | lockBinding( cdatests, NAMESPACE ) | | lockBinding( cd, NAMESPACE ) | | lockBinding( linear, NAMESPACE ) | | | | unlockBinding( array , NAMESPACE ) | | assign( array, Module( array ), NAMESPACE ) | | lockBinding( array, NAMESPACE ) | | | | } | | | | It seems to work, but is there something I can/should do to make this | | a wee cleaner? | | Have another look at the Rcpp-modules vignette, and/or the third set of | slides (Advanced Rcpp) from our class in April -- you no longer need the | unlockBinding / lockBinding business as Romain internalized that. The | skeleton-generated packages now just do this in zzz.R: | | .onLoad - function(pkgname, libname){ | loadRcppModules() | } | | using a 'RcppModules: cda, cdatests, cd, linear, array' declaration in | DESCRIPTION. | | Strange, this does not seem to work for me. | | If I use the following zzz.r, (with RcppModules listed in DESCRIPTION) | | .onLoad - function(libname, pkgname){ | loadRcppModules() | } | | I get a segfault whenever I use a c++ function, | | *** caught segfault *** | address 0x0, cause 'memory not mapped' | | Traceback: | 1: .External(list(name = InternalFunction_invoke, address = | pointer: 0x101407f30, dll = list(name = Rcpp, path = | /Library/Frameworks/R.framework/Versions/2.13/Resources/library/Rcpp/libs/x86_64/Rcpp.so, | dynamicLookup = TRUE, handle = pointer: 0x101441030, | info = pointer: 0x1002b3ed0), numParameters = -1L), pointer: | 0x103011050, ...) | 2: circular_dichroism_spectrum(clust, gold, n = 1.33, N = 36, progress = FALSE) | 3: onecluster() | | Is there another step I'm missing? My first instinct is to check the unitTest, so look at what is different in test run from inst/unitTests/runit.Module.client.package.R using inst/unitTests/testRcppModule/ That should still work on your platform and you should be able to go from there. Dirk -- Gauss once played himself in a zero-sum game and won $50. -- #11 at http://www.gaussfacts.com ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
[Rcpp-devel] R.e.: Problems with rlnorm
On Sun, May 29, 2011 at 3:29 PM, rcpp-devel-requ...@r-forge.wu-wien.ac.at wrote: 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. Take a look at the quickref in the section Using matrices, particularly the part about _ -- Romain has done a lot of work since the conversation that you reference :) Also, see previous list message from Vincent and Dirk for namespace notes on using _ outside of inline. Now I am calling the r-prefixed distributions with success, but this same code will fail on compile if the rlnorm() function is called. compile error if rlnorm is called instead of rnorm: file3a7d3a6f.cpp:38:28: error: 'rlnorm' was not declared in this scope It's working now in the most recent SVN version (I'm guessing someone's already pointed this out). Below is an example that includes by-column assignment. Also, unless you need to reset the seed mid-function-call, RNGScope grabs the seed from R, so you can use a regular set.seed call. So, the return value of the following is identical to that of your example: require(inline) src - ' Rcpp::RNGScope Scope; // gets value from set.seed call in R int NumRands = 5; int NumTrials = 3; Rcpp::NumericMatrix RandVals(NumRands, NumTrials*2); RandVals(_,3)=rlnorm(NumRands,2.0,1.0); return RandVals; ' fun1 - cxxfunction(signature(), src, plugin = Rcpp) set.seed(20) fun1() -Christian -- A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal – Panama! ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Re: [Rcpp-devel] trans() changed in latest RcppArmadillo
Hello, After upgrading to the 0.2.21 release of RcppArmadillo, my previously working code (across many functions) ceased working (on a Windows XP installation). I re-installed the previous version (0.2.20) from CRAN (via a server location not yet updated to 0.2.21); didn't fix it. The timing may be a coincidence, though reading the post on trans() encourages me to make this post. While I use 'trans' across my functions, it is not applied on complex-valued matrices; only real-valued. So the prior post wouldn't explain my issue. Thanks, Terrance Savitsky __ This email message is for the sole use of the intended recipient(s) and may contain confidential information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. ___ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel