Re: [Rd] Conditional dependency between packages
I agree that require makes it clearer what happens, so I will probably change. The disadvantage is the (for some users confusing) messages/warnings if the package is not installed. Wrapping it in with suppressWarnings and suppressMessages before reprinting the load message solves that though: if (suppressMessages(suppressWarnings(require(pkg1 { print("Loading required package: pkg1") ... } Thanks, Jon Henrik Bengtsson wrote: if ("pkg1" %in% rownames(utils:::installed.packages()) ) { library(pkg1) ... } can be replaced by: if (require("pkg1")) { ... } /Henrik On Thu, Jul 2, 2009 at 5:29 AM, Jon Olav Skoien wrote: Hi Seth, And thanks for your suggestion! I was not able to do exactly what you described (I have no earlier experience with using environments), but you mentioning .onLoad was a good starting point. I have now removed all references to pkg1 from the NAMESPACE, and wrote the following .onLoad function: .onLoad <- function(libname, pkgname) { if ("pkg1" %in% rownames(utils:::installed.packages()) ) { library(pkg1) info = matrix(c("fun1", "fun2", "fun3", rep("pkg2", 3), rep(NA,3)), ncol = 3) registerS3methods(info, package = "pkg1", env = environment(funInPkg2)) } } New methods for functions fun1, fun2 and fun3 seem to be available if pkg1 is installed, while they are ignored if pkg1 is not installed. The function above loads pkg1 automatically if installed (I would prefer this to be optional), but at least it will not be necessary to download pkg1 (with all its dependencies) for users without interest in it. I have not found any description of registerS3methods (except from an old .Rd file stating that it is not intended to be called directly), so there might be better ways of doing this... And I am sure there is a better way of assigning the right environment... Thanks again, Jon Seth Falcon wrote: Hi Jon, * On 2009-06-30 at 15:27 +0200 Jon Olav Skoien wrote: I work on two packages, pkg1 and pkg2 (in two different projects). pkg1 is quite generic, pkg2 tries to solve a particular problem within same field (geostatistics). Therefore, there might be users who want to use pkg2 as an add-on package to increase the functionality of pkg1. In other words, functions in pkg1 are based on the S3 class system, and I want pkg2 to offer methods for pkg2-objects to functions defined in pkg1, for users having both packages installed. Merging the packages or making pkg2 always depend pkg1 would be the easiest solution, but it is not preferred as most users will only be interested in one of the packages. I'm not sure I understand the above, I think you may have a pkg2 where you meant pkg1, but I'm not sure it matters. I think the short version is, pkg2 can be used on its own but will do more if pkg1 is available. I don't think R's packaging system currently supports conditional dependencies as you might like. However, I think you can get the behavior you want by following a recipe like: * In pkg2 DESCRIPTION, list Suggests: pkg1. * In pkg2 code, you might define a package-level environment and in .onLoad check to see if pkg1 is available. PKG_INFO <- new.env(parent=emptyenv()) .onLoad <- function(libname, pkgname) { if (check if pkg1 is available) { PKG_INFO[["pkg1"]] <- TRUE } } * Then your methods can check PKG_INFO[["pkg1"]]. + seth __ 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] package post install instructions
A somewhat related question. Can one install a hook to be called when __any__ package is loaded. Something like this : setHook(packageEvent(".*", "onLoad"), function(...) grDevices::ps.options(horizontal=FALSE)) Romain On 07/02/2009 09:38 AM, Romain Francois wrote: Hello, I've looked in tools:::.install_packages for some sort of hook that would let packages developers point to further instructions after a package is installed. For example, some packages need to setup environment variables, ... Is there something I have missed ? Romain -- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/qJ8V : RGG#153: stars network |- http://tr.im/qzSl : using ImageJ from R: the RImageJ package `- http://tr.im/qzSJ : with semantics for java objects in rJava __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] factor() calls sort.list unnecessarily?
R-devel, factor(x) can take a long time on large character vectors (more than a minute in the example below). This is because of a call to sort.list. > str(x) chr [1:3436831] "chr5" "chr10" "chr16" "chr3" "chr4" "chr15" ... > Rprof("/tmp/factor.Rprof") > invisible(factor(x)) > Rprof() > summaryRprof("/tmp/factor.Rprof") $by.self self.time self.pct total.time total.pct "sort.list" 66.14 98.9 66.14 98.9 "unique.default" 0.26 0.4 0.26 0.4 "unique" 0.24 0.4 0.50 0.7 "match" 0.24 0.4 0.24 0.4 "factor" 0.02 0.0 66.90 100.0 $by.total total.time total.pct self.time self.pct "factor" 66.90 100.0 0.02 0.0 "sort.list" 66.14 98.9 66.14 98.9 "unique" 0.50 0.7 0.24 0.4 "unique.default" 0.26 0.4 0.26 0.4 "match"0.24 0.4 0.24 0.4 $sampling.time [1] 66.9 sort.list is always called but used only to determine the order of levels, so unnecessary when levels are provided. In addition, order of levels is for unique values of x only. Perhaps these issues are addressed in the patch below? It does require unique() on the original argument x, rather than only on as.character(x) At the least, perhaps sort.list can be called only when levels are not provided? Martin Index: src/library/base/R/factor.R === --- src/library/base/R/factor.R (revision 48892) +++ src/library/base/R/factor.R (working copy) @@ -18,12 +18,13 @@ exclude = NA, ordered = is.ordered(x)) { exclude <- as.vector(exclude, typeof(x)) -ind <- sort.list(x) # or ? order(x) which more (too ?) tolerant +if (missing(levels)) +ind <- sort.list(unique(x)) nx <- names(x) force(ordered) x <- as.character(x) if(missing(levels)) # get unique levels ordered by the original values - levels <- unique(x[ind]) + levels <- unique(x)[ind] levels <- levels[is.na(match(levels, exclude))] f <- match(x, levels) if(!is.null(nx)) __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Bug report (PR#13798)
Full_Name: Emilien Henry Version: 2.9.1 OS: Submission from: (NULL) (80.13.174.5) R often stops working after execution of some packages (R commander or ade4TkGUI for example) Signature du problème : Nom dévénement de problème: APPCRASH Nom de lapplication: Rgui.exe Version de lapplication: 2.91.48839.0 Horodatage de l'application: 4a44ca0a Nom du module par défaut: tk85.dll Version du module par défaut: 8.5.2.6 Horodateur du module par défaut: 497e22aa Code de lexception: c005 Décalage de lexception: 0005d66d Version du système: 6.0.6001.2.1.0.768.3 Identificateur de paramètres régionaux: 1036 Information supplémentaire n° 1: 34a8 Information supplémentaire n° 2: 4fc241adc2ff96677a38ba647d58d35b Information supplémentaire n° 3: b1d4 Information supplémentaire n° 4: 93d43904703cf937a4f652a9b119d54 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Matrix with random number
Thanks very much! Fábio Mathias Corrêa UFLA --- Em qua, 1/7/09, Kjell Konis escreveu: De: Kjell Konis Assunto: Re: [Rd] Matrix with random number Para: "Fabio Mathias" Cc: r-devel@r-project.org Data: Quarta-feira, 1 de Julho de 2009, 8:30 Hi Fabio, Your function myrbeta returns void so assigning the output isn't going to work. Instead you need to call it like a FORTRAN subroutine. Also, I added arguments for the parameters of the beta and moved the fseedi and fseedo calls outside of the loop. This is a pretty basic FORTRAN programming question and there are lots of books that can help you learn FORTRAN. subroutine mat(x,l,c,pa,pb) integer l,c double precision x(l,c), a integer i,j call fseedi() do j = 1, c do i = 1, l call myrbeta(x(i,j),pa,pb) enddo enddo call fseedo() end In R call it like this: storage.mode(x) <- "double" ..Fortran("mat", x = x, as.integer(l), as.integer(c), as.double(1), as.double(2)) Cheers, Kjell On 30 juin 09, at 20:02, Fabio Mathias wrote: > Thanks Mr. Barry Rowlingson > > However, the matrix appears to zeros! > > Notice the code below! Please! > > Code in fortran > > subroutine mat(x,l,c,a) > integer l,c > double precision x(l,c), a > integer i,j > do j = 1, c > do i = 1, l > call fseedi() > x(i,j) = myrbeta(a,1,2) > call fseedo() > enddo > enddo > end > > In R: > > dyn.load("func.so") > x <- matrix(0,5,6) > l <- nrow(x) > c <- > ncol(x) > a <- 0 > > ..Fortran("mat", x = x, l, c, as.double(a)) > > Results: > > $x > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 0 0 0 0 0 0 > [2,] 0 0 0 0 0 0 > [3,] 0 0 0 0 0 0 > [4,] 0 0 0 0 0 0 > [5,] 0 0 0 0 0 0 > > [[2]] > [1] 5 > > [[3]] > [1] 6 > > [[4]] > [1] 1 > > > Thanks!!! > > > Fábio Mathias Corrêa UFLA > > > > > > > [[elided Yahoo spam]] > > [[alternative HTML version deleted]] > > [[elided Yahoo spam]] [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Dynamic libraries
I am trying to write a dynamic linked library for R, in Pascal. (This is to speed up the execution of a simulation that I am running in R.) I know Pascal might not be the perfect language for this (C or Fortran being more natural), but from what I have read I think it should work. Though I should point out that I am a neophyte when it comes to DLLs. From R I want to hand a function in the library a two-dimensional matrix, operate on the matrix, and hand back a 2-d matrix. I have no problem creating the library and loading it using dyn.load() I have written an R wrapper for the function. When I do something simple using scalars (integers) everything works fine, or so it seems at least. However, when I try to use a vector as an argument to the function, two things happen: R becomes very unstable; the function only operates on the first 3 elements of the vector (say if the vector has 5 elements). Anyone have experience with this sort of thing? Thanks, Robin Cowan Sample below: This one works fine: library Test1Lib; type pA=^integer; procedure simple(x:pA); cdecl; var i1,i2:integer; begin x^:=x^*2; end; exports simple; begin end. This one does not: library Test2Lib; type array1=array[1..5] of integer; pA=^array1; procedure simpleArray(x:pA); cdecl; var i1:integer; begin for i1:=1 to 5 do x^[i1]:=x^[i1]*2; end; exports simpleArray; begin end. Here is the wrapper I use: MySimple <- function(x) { ans <- .C("simple",as.integer(x)) # or simpleArray in the second case ans[[1]] } Here is what I get: > x<-c(1,2,3,4,5) > MySimple(x) [1] 2 4 6 4 5 The University of Maastricht has changed its name and mail servers. My email address is now r.co...@maastrichtuniversity.nl. [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Dynamic libraries
On 03/07/2009 4:37 PM, robin cowan wrote: I am trying to write a dynamic linked library for R, in Pascal. You need to tell us what Pascal compiler you're using. You may need to ask your compiler provider how to compile something to interface with R, or tell us all sorts of details that you may not know. Such as: - what is "integer"? - what is "cdecl"? Those have obvious answers; but it really depends on your compiler writer to implement them in the obvious way or some other. And then there's: - what registers does your compiler preserve across calls? That's usually much harder to discover, but really crucial for stable performance of the caller. Duncan Murdoch (This is to speed up the execution of a simulation that I am running in R.) I know Pascal might not be the perfect language for this (C or Fortran being more natural), but from what I have read I think it should work. Though I should point out that I am a neophyte when it comes to DLLs. From R I want to hand a function in the library a two-dimensional matrix, operate on the matrix, and hand back a 2-d matrix. I have no problem creating the library and loading it using dyn.load() I have written an R wrapper for the function. When I do something simple using scalars (integers) everything works fine, or so it seems at least. However, when I try to use a vector as an argument to the function, two things happen: R becomes very unstable; the function only operates on the first 3 elements of the vector (say if the vector has 5 elements). Anyone have experience with this sort of thing? Thanks, Robin Cowan Sample below: This one works fine: library Test1Lib; type pA=^integer; procedure simple(x:pA); cdecl; var i1,i2:integer; begin x^:=x^*2; end; exports simple; begin end. This one does not: library Test2Lib; type array1=array[1..5] of integer; pA=^array1; procedure simpleArray(x:pA); cdecl; var i1:integer; begin for i1:=1 to 5 do x^[i1]:=x^[i1]*2; end; exports simpleArray; begin end. Here is the wrapper I use: MySimple <- function(x) { ans <- .C("simple",as.integer(x)) # or simpleArray in the second case ans[[1]] } Here is what I get: > x<-c(1,2,3,4,5) > MySimple(x) [1] 2 4 6 4 5 The University of Maastricht has changed its name and mail servers. My email address is now r.co...@maastrichtuniversity.nl. [[alternative HTML version deleted]] __ 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] beginner's guide to C++ programming with R packages?
On Fri, 2009-06-26 at 16:17 -0400, Whit Armstrong wrote: > > But this draws me back to the basic question. I don't want to run R > > CMD INSTALL 20 times per hour. How do developers "actually" test > > their code? > > check out RUnit for tests. > http://cran.r-project.org/web/packages/RUnit/index.html > > as for testing c++ code. I have taken an approach which is probably > different than most. I try to build my package as a c++ library that > can be used independent of R. Then you can test your library with > whatever c++ test suite that you prefer. Once you are happy, then I also have C++ tests that operate separately from R, though I have a very small library of stub R functions to get the thing to build. There have been some tricky issues with R (if one links to the regular R library) and the test framework fighting over who was "main." I think that's why I switched to the stub. Working only with R level tests alone does not permit the kind of lower level testing that you can get by running your own unit tests. I use the boost unit test framework. Of course, you want R level tests too. Some of my upper level C++ tests are mirror images of R tests; this can help identify if a problem lies at the interface. For C++ tests I build my code in conjunction with a main program. I think I also have or had a test building it as a library, but I don't use that much. For R, my modules get built into a library. It's usually cleaner to build the R library from a fresh version of the sources; otherwise scraps of my other builds tend to end up in the R package. Thanks, Whit, for the pointers to Rcpp and RAbstraction. Ross Boylan __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] beginner's guide to C++ programming with R packages?
Ross Boylan wrote: On Fri, 2009-06-26 at 16:17 -0400, Whit Armstrong wrote: But this draws me back to the basic question. I don't want to run R CMD INSTALL 20 times per hour. How do developers "actually" test their code? check out RUnit for tests. http://cran.r-project.org/web/packages/RUnit/index.html You could also look at the svUnit package http://cran.r-project.org/web/packages/svUnit/index.html . PhG as for testing c++ code. I have taken an approach which is probably different than most. I try to build my package as a c++ library that can be used independent of R. Then you can test your library with whatever c++ test suite that you prefer. Once you are happy, then I also have C++ tests that operate separately from R, though I have a very small library of stub R functions to get the thing to build. There have been some tricky issues with R (if one links to the regular R library) and the test framework fighting over who was "main." I think that's why I switched to the stub. Working only with R level tests alone does not permit the kind of lower level testing that you can get by running your own unit tests. I use the boost unit test framework. Of course, you want R level tests too. Some of my upper level C++ tests are mirror images of R tests; this can help identify if a problem lies at the interface. For C++ tests I build my code in conjunction with a main program. I think I also have or had a test building it as a library, but I don't use that much. For R, my modules get built into a library. It's usually cleaner to build the R library from a fresh version of the sources; otherwise scraps of my other builds tend to end up in the R package. Thanks, Whit, for the pointers to Rcpp and RAbstraction. Ross Boylan __ 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