Re: [Rd] Linking to native routines in other packages
Thanks Romain for that elegant solution. Best, Gregor On Wed, 22 Jan 2014 20:23:10 +0100 Romain François wrote: > Hello, > > The problem is that you have logic in both your mother and child packages. > IMO, you should only have logic in the mother package. > > I’ve done this in a number of packages, it requires a bit of work > initially, but not that much. > > What I’d do is have something like this in mother/inst/include/mother.h : > > #if defined(COMPILING_MOTHER) > // just declare, will be defined in test.c > SEXP fun(SEXP test) ; > #else > inline SEXP fun(SEXP test){ > typedef SEXP(*Fun)(SEXP); > static Fun fun = (Fun)R_GetCCallable("mother", "fun") ; > return fun(test) ; > } > #endif > > In your test.c file, make sure you define COMPILING_MOTHER before you > include mother.h, something like this > > #include > #include > #include > #define COMPILING_MOTHER > #include > > SEXP fun(SEXP); > > void R_init_mother(DllInfo *dll) { > R_RegisterCCallable("mother", "fun", (DL_FUNC) &fun); > } > > SEXP fun(SEXP test) { > Rprintf("fun so much fun\n"); > return R_NilValue; > } > > So that in your child package you only have to use it, something like: > > #include > #include > #include > > SEXP afun(SEXP test) { >fun(test); >return R_NilValue; > } > > Note that if you only want the interface between the two packages to be at > low level (not visible from R), then you don’t need to conform to the > SEXP(SEXP...) interface. You can use anything you like. > > Romain > > Le 22 janv. 2014 à 19:56, Gregor Kastner a écrit : > > > Hi again, > > > > On Wed, 22 Jan 2014 06:39:17 -0600 > > Dirk Eddelbuettel wrote: > > > > | Working examples I know of: > > | > > |'xts' importing two functions from 'zoo' > > | > > |'RcppXts' importing approx. ten functions from 'xts' > > | > > | Maybe by comparing to these you can sort out the missing step at your > > end. > > > > Thanks Dirk for the hints; I finally got the code running. Important point > > is that R_init_PKGNAME() is declared as extern "C" (or RcppExport, of > > course) if using g++ in both the mother and the child package. > > (Interestingly, dyn.load() only complains when either the mother or the > > child package don't do so, but not if both don't do so => SEGFAULT.) > > Since it took me almost the entire afternoon to figure that out, I'll > > document a working example here. > > > > Scenario: We have a 'mother' package, which wants to make some C/C++ > > routines available to the 'child' package to be called directly from > > C/C++ level. Thus, mother's 'src' cointains: > > > > > > * BEGIN test.c * > > > > #include > > #include > > #include > > > > SEXP fun(SEXP); > > > > void R_init_mother(DllInfo *dll) { > > R_RegisterCCallable("mother", "fun", (DL_FUNC) &fun); > > } > > > > SEXP fun(SEXP test) { > > Rprintf("fun so much fun\n"); > > return R_NilValue; > > } > > > > ** END test.c ** > > > > > > (Note that no extern "C" is needed here because it will be compiled with > > gcc anyway). > > > > The child uses Rcpp and mother, thus has > > > > > > * BEGIN DESCRIPTION * > > > > LinkingTo: mother, Rcpp > > Depends: mother, Rcpp > > Imports: mother, Rcpp > > > > ** END DESCRIPTION ** > > > > > > in its DESCRIPTION file, and > > > > > > * BEGIN test.cpp * > > > > #include > > #include > > > > extern "C" { > > SEXP afun(SEXP); > > SEXP(*fun)(SEXP); > > > > void R_init_child(DllInfo *info) { > > fun = (SEXP(*)(SEXP)) R_GetCCallable("mother", "fun"); > > } > > > > SEXP afun(SEXP test) { > > fun(test); > > return R_NilValue; > > } > > } > > > > ** END test.cpp ** > > > > (Note that extern "C" is crucial here.) After installing mother and > > child, we have: > > > >> library(child) > > Loading required package: mother > > Loading required package: Rcpp > >> .Call("afun", 123, PACKAGE="child") > > fun so much fun > > NULL > >> > > > > Maybe it is of help to someone; please excuse me if I bored anyone with > > trivialities. > > > > Best, > > Gregor > > > > __ > > R-devel@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Behavior of --install-tests and testInstalledPackage
On Wed, Jan 22, 2014 at 10:55 AM, Brian Lee Yung Rowe wrote: > Hello, > > I'm writing a script that automates the testing of reverse dependencies of > a package. I found the function testInstalledPackage in the tools package, > which seems to do what I want. However, when I use it for a source package > that was installed with --install-tests, I've noticed that only the actual > test files (e.g. located in inst/tests) are available and run. In other > words the test harness script (e.g. in tests) is not copied, so any wiring > to run tests isn't available when running testInstalledPackage. > > I tried loading the required packages (i.e. the installed package to > retest plus the testing frameworks RUnit, testthat), but since > testInstalledPackage makes an external call to R CMD BATCH, any packages > loaded in my current environment have no effect on the tests. The only > workaround that I've come up with is to add require statements to the top > of each test file, but this is a bit onerous. > > My question is whether > 1) there is a technique to force the test harness script (e.g. > tests/run_tests.R, tests/doRUnit.R) to be copied into the installed source > package, or > 2) there is a way to have testInstalledPackage force the loading of > required packages prior to executing test scripts, or > 3) has someone already done this? > I can answer (3). devtools already has a function that does this: library(devtools) revdep_check('mypackage') It's basically a wrapper for the revdep and check_cran functions from devtools. In the development version of devtools (on Github) there are some changes to improve the output of these functions. -Winston [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Behavior of --install-tests and testInstalledPackage
Winston, Thanks for the pointer. Hadley made the same suggestion. One thing I was looking to do though was have something like this automated, so it would be nice to have a return value instead of just results written to files. That's one nice thing about the testInstalledPackage function, despite the issue with the test harness. Any additional pointers are greatly appreciated. Warm Regards, Brian Rowe â¢â¢â¢â¢â¢ Brian Lee Yung Rowe Founder, Zato Novo Professor On Jan 27, 2014, at 12:52 PM, Winston Chang wrote: > On Wed, Jan 22, 2014 at 10:55 AM, Brian Lee Yung Rowe > wrote: >> Hello, >> >> I'm writing a script that automates the testing of reverse dependencies of a >> package. I found the function testInstalledPackage in the tools package, >> which seems to do what I want. However, when I use it for a source package >> that was installed with --install-tests, I've noticed that only the actual >> test files (e.g. located in inst/tests) are available and run. In other >> words the test harness script (e.g. in tests) is not copied, so any wiring >> to run tests isn't available when running testInstalledPackage. >> >> I tried loading the required packages (i.e. the installed package to retest >> plus the testing frameworks RUnit, testthat), but since testInstalledPackage >> makes an external call to R CMD BATCH, any packages loaded in my current >> environment have no effect on the tests. The only workaround that I've come >> up with is to add require statements to the top of each test file, but this >> is a bit onerous. >> >> My question is whether >> 1) there is a technique to force the test harness script (e.g. >> tests/run_tests.R, tests/doRUnit.R) to be copied into the installed source >> package, or >> 2) there is a way to have testInstalledPackage force the loading of required >> packages prior to executing test scripts, or >> 3) has someone already done this? > > > I can answer (3). devtools already has a function that does this: > library(devtools) > revdep_check('mypackage') > > It's basically a wrapper for the revdep and check_cran functions from > devtools. In the development version of devtools (on Github) there are some > changes to improve the output of these functions. > > -Winston [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] package namespace from C
Hi all, what is the supported way to get the package namespace from C? I want to call a non-exported R function from C, and did not find anything in the docs. Thanks, Gabor [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] package namespace from C
Gabor, You can evaluate R code from C, so it should be pretty straightforward to construct a call to ::: ::: looks like it just uses get, so you could probably do that as well. ~G On Mon, Jan 27, 2014 at 7:30 PM, Gábor Csárdi wrote: > Hi all, > > what is the supported way to get the package namespace from C? I want to > call a non-exported R function from C, and did not find anything in the > docs. > > Thanks, > Gabor > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Gabriel Becker Graduate Student Statistics Department University of California, Davis [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] package namespace from C
Indeed, thanks. Btw. is it by design, that if I do EVAL(lang3(install(), ...)) from a package, it is not evaluated in the package's namespace? Anyway, this is how I did it in some old code of mine: PROTECT(rho = EVAL(lang2(install("getNamespace"), ScalarString(mkChar("igraph"); PROTECT(ec=eval(lang3(install(".igraph.progress"), ScalarReal(percent), ScalarString(mkChar(message))), rho)); I guess this still works. Best, Gabor On Mon, Jan 27, 2014 at 10:39 PM, Gabriel Becker wrote: > Gabor, > > You can evaluate R code from C, so it should be pretty straightforward to > construct a call to ::: > > ::: looks like it just uses get, so you could probably do that as well. > > ~G > > > On Mon, Jan 27, 2014 at 7:30 PM, Gábor Csárdi wrote: > >> Hi all, >> >> what is the supported way to get the package namespace from C? I want to >> call a non-exported R function from C, and did not find anything in the >> docs. >> >> Thanks, >> Gabor >> >> [[alternative HTML version deleted]] >> >> __ >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > > > -- > Gabriel Becker > Graduate Student > Statistics Department > University of California, Davis > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel