[Rd] C/C++ namespaces
Dear community, this is just a suggestion, but might be useful for the following R releases. I was programming some C code for R and my compiler constantly showed me some crazy messages that a stdc++ macro length() was supplied with 4 arguments whereas only one was required. The problem could be partially resolved by changing the order of includes. However as soon as the file was used from another units, the problem reappeared. Finally, it turned out that length() with one argument was a macro in Rinternals.h along with LENGTH. So I have to use namespace specifications on the rest of my code, because R - length() is not protected under a namespace. The problem is however that length is a pretty common name and as any other very common name should be protected by a C namespace to avoid confusion. Therefore, the suggestion/question - wouldn't it be reasonable just to put the whole R includes into a unique namespace? It wouldn't require any modifications in the existing libraries because if the code is not conflicting, namespace can be omitted. Does it have sense? Regards Oleg Sklyar -- Dr Oleg Sklyar European Bioinformatics Institute Wellcome Trust Genome Campus Hinxton, Cambridge CB10 1SD United Kingdom tel +44 1223 492537 fax +44 1223 494468 email [EMAIL PROTECTED] [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] "[" operator and indexing ambiguity
Hi everybody. I myself am not subscribed for Rd therefore replying late. As the author of EBImage, I'd like to comment on the issue. As Tony correctly writes one can generally use nargs() to get the number of arguments, and this works for "["(x,i,j,...,drop) method as well. The problem I had with the EBImage where I could not distinguish between x[index] and x[index,,] is that somehow particularly for the "[" method and particularly for 3 dimensions nargs in both cases gave me the same number of arguments! This behavior was not present when the number of dimension was 2 or 4, i.e. for x[index,] or x[index,,,] -- these could be easily distinguished from x[index]. Anyway, maybe R changed since and I just did not notice that :) Otherwise, coming back to Bradley's original question, it is generally a difficult task to implement correct indexing if your underlying type does not initially support it simply because there are so many combinations possible! Originally, EBImage's Image class was not based on 'array' and then "[" method definitions were simply overwhelming -- too many and too long, and still did not work completely right. Using array simply allowed me to pass the arguments as they are to the underlying array and only build a couple of features on top. It becomes even more difficult for the set method "[<-" as you really need to parse all indexes and make them rotating if too short etc etc. Here the power of R in freestyle indexing of arrays becomes a pain. -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Speed of for loops
Tom, *apply's generally speed up calculations dramatically. However, if and only if you do a repetitive operation on a vector, list matrix which does NOT require accessing other elements of that variable than the one currently in the *apply index. This means in your case any of *apply will not speed up your calculation (until you significantly rethink the code). At the same time, you can speed up your code by orders of magnitude using c-functions for "complex" vector indexing operations. If you need instructions, I can send you a very nice "Step-by-step guide for using C/C++ in R" which goes beyond "Writing R Extensions" document. Otherwise, such questions should be posted to R-help, not Rd, please post correspondingly. Best regards, Oleg Tom McCallum wrote: > Hi Everyone, > > I have a question about for loops. If you have something like: > > f <- function(x) { > y <- rep(NA,10); > for( i in 1:10 ) { > if ( i > 3 ) { > if ( is.na(y[i-3]) == FALSE ) { > # some calculation F which depends on one or > more of the previously > generated values in the series > y[i] = y[i-1]+x[i]; > } else { > y[i] <- x[i]; > } > } > } > y > } > > e.g. > >> f(c(1,2,3,4,5,6,7,8,9,10,11,12)); > [1] NA NA NA 4 5 6 13 21 30 40 > > is there a faster way to process this than with a 'for' loop? I have > looked at lapply as well but I have read that lapply is no faster than a > for loop and for my particular application it is easier to use a for loop. > Also I have seen 'rle' which I think may help me but am not sure as I have > only just come across it, any ideas? > > Many thanks > > Tom > > > -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Step-by-step guide for using C/C++ in R; WAS: Speed of for loops
I know this should not go to [Rd], but the original post was there and the replies as well. Thank you all who expressed interest in the "Step-by-step guide for using C/C++ in R"! Answering some of you, yes it is by me and was written to assist other group members to start adding c/c++ code to their R coding. You can now download it from: http://www.ebi.ac.uk/~osklyar/kb/CtoRinterfacingPrimer.pdf I would also appreciate your comments if you find it useful or not, or maybe what can be added or modified. But not on the list, directly to my email please. Best wishes, Oleg Tamas K Papp wrote: > On Tue, Jan 30, 2007 at 12:15:29PM +, Oleg Sklyar wrote: > >> magnitude using c-functions for "complex" vector indexing operations. If >> you need instructions, I can send you a very nice "Step-by-step guide >> for using C/C++ in R" which goes beyond "Writing R Extensions" document. > > Hi Oleg, > > Can you please post this guide online? I think that many people would > be interested in reading it, incl. me. > > Tamas -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Speed of for loops
It is surely an elegant way of doing things (although far from being easy to parse visually) but is it really faster than a loop? After all, the indexing problem is the same and sapply simply does the same job as for in this case, plus "<<-" will _search_ through the environment on every single step. Where is the gain? Oleg -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 Byron Ellis wrote: > Actually, why not use a closure to store previous value(s)? > > In the simple case, which depends on x_i and y_{i-1} > > gen.iter = function(x) { > y = NA > function(i) { >y <<- if(is.na(y)) x[i] else y+x[i] > } > } > > y = sapply(1:10,gen.iter(x)) > > Obviously you can modify the function for the bookkeeping required to > manage whatever lag you need. I use this sometimes when I'm > implementing MCMC samplers of various kinds. > > > On 1/30/07, Herve Pages <[EMAIL PROTECTED]> wrote: >> Tom McCallum wrote: >>> Hi Everyone, >>> >>> I have a question about for loops. If you have something like: >>> >>> f <- function(x) { >>> y <- rep(NA,10); >>> for( i in 1:10 ) { >>> if ( i > 3 ) { >>> if ( is.na(y[i-3]) == FALSE ) { >>> # some calculation F which depends on one or >>> more of the previously >>> generated values in the series >>> y[i] = y[i-1]+x[i]; >>> } else { >>> y[i] <- x[i]; >>> } >>> } >>> } >>> y >>> } >>> >>> e.g. >>> >>>> f(c(1,2,3,4,5,6,7,8,9,10,11,12)); >>> [1] NA NA NA 4 5 6 13 21 30 40 >>> >>> is there a faster way to process this than with a 'for' loop? I have >>> looked at lapply as well but I have read that lapply is no faster than a >>> for loop and for my particular application it is easier to use a for loop. >>> Also I have seen 'rle' which I think may help me but am not sure as I have >>> only just come across it, any ideas? >> Hi Tom, >> >> In the general case, you need a loop in order to propagate calculations >> and their results across a vector. >> >> In _your_ particular case however, it seems that all you are doing is a >> cumulative sum on x (at least this is what's happening for i >= 6). >> So you could do: >> >> f2 <- function(x) >> { >> offset <- 3 >> start_propagate_at <- 6 >> y_length <- 10 >> init_range <- (offset+1):start_propagate_at >> y <- rep(NA, offset) >> y[init_range] <- x[init_range] >> y[start_propagate_at:y_length] <- cumsum(x[start_propagate_at:y_length]) >> y >> } >> >> and it will return the same thing as your function 'f' (at least when 'x' >> doesn't >> contain NAs) but it's not faster :-/ >> >> IMO, using sapply for propagating calculations across a vector is not >> appropriate >> because: >> >> (1) It requires special care. For example, this: >> >> > x <- 1:10 >> > sapply(2:length(x), function(i) {x[i] <- x[i-1]+x[i]}) >> >> doesn't work because the 'x' symbol on the left side of the <- in the >> anonymous function doesn't refer to the 'x' symbol defined in the >> global >> environment. So you need to use tricks like this: >> >> > sapply(2:length(x), >> function(i) {x[i] <- x[i-1]+x[i]; assign("x", x, >> envir=.GlobalEnv); x[i]}) >> >> (2) Because of this kind of tricks, then it is _very_ slow (about 10 times >> slower or more than a 'for' loop). >> >> Cheers, >> H. >> >> >>> Many thanks >>> >>> Tom >>> >>> >>> >> __ >> 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] Wrong vector size reported by error message
my R-SVN revision is 40458 compared to 40386 yours, could it be corrected already? * ~: R > 2**30 [1] 1073741824 > a<-2:1073741824 Error: cannot allocate vector of size 4194304 Kb > sessionInfo() R version 2.5.0 Under development (unstable) (2007-01-22 r40548) x86_64-unknown-linux-gnu locale: LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base" > -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 [EMAIL PROTECTED] wrote: > Hi, > > On my system, I get the following error message: > > > big <- 2:(2**30) > Error: cannot allocate vector of size 0 Kb > > Note the wrong "size 0 Kb" in the message! > > Cheers, > H. > > >> sessionInfo() > R version 2.5.0 Under development (unstable) (2007-01-05 r40386) > i686-pc-linux-gnu > > locale: > LC_CTYPE=en_CA.UTF-8;LC_NUMERIC=C;LC_TIME=en_CA.UTF-8;LC_COLLATE=en_CA.UTF-8;LC_MONETARY=en_CA.UTF-8;LC_MESSAGES=en_CA.UTF-8;LC_PAPER=en_CA.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_CA.UTF-8;LC_IDENTIFICATION=C > > attached base packages: > [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" > [7] "base" > > __ > 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] Wrong vector size reported by error message
Herve, it looks like 64-bit. I just tried another 32 bit version: Windows build R-2.4.1 under VMWare on the same 64 bit Ubuntu as in my post above and Windows version shows the same bug as you report: R2.4.1 on Windows 2000 as guest system in VMWare Ubuntu 6.10 64bit --- > big <- 2:(2**30) Fehler: kann Vektor der Größe 0 Kb nicht allozieren > sessionInfo() R version 2.4.1 (2006-12-18) i386-pc-mingw32 locale: LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252 attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base" -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 Herve Pages wrote: > Hi Oleg, > > Oleg Sklyar wrote: >> my R-SVN revision is 40458 compared to 40386 yours, could it be >> corrected already? > > No I don't think so. Maybe an architecture specific problem? > You are on a 64-bit system, I'm on a 32-bit system. > > I was able to reproduce on 3 systems so far (with any version of R): > - on my Pentium M laptop with Ubuntu 6.06 > - on a Linux SUSE 9.2 32-bit system > - on a Core 1 Duo (32-bit) Mac Mini > > All those systems are 32-bits. > > I've tried on a couple of 64-bit systems and I don't get this problem. > > Cheers, > H. > >> * ~: R >>> 2**30 >> [1] 1073741824 >>> a<-2:1073741824 >> Error: cannot allocate vector of size 4194304 Kb >>> sessionInfo() >> R version 2.5.0 Under development (unstable) (2007-01-22 r40548) >> x86_64-unknown-linux-gnu >> >> locale: >> LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C >> >> >> attached base packages: >> [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" >> [7] "base" >> -- >> Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 >> >> >> [EMAIL PROTECTED] wrote: >>> Hi, >>> >>> On my system, I get the following error message: >>> >>> > big <- 2:(2**30) >>> Error: cannot allocate vector of size 0 Kb >>> >>> Note the wrong "size 0 Kb" in the message! >>> >>> Cheers, >>> H. >>> >>> >>>> sessionInfo() >>> R version 2.5.0 Under development (unstable) (2007-01-05 r40386) >>> i686-pc-linux-gnu >>> >>> locale: >>> LC_CTYPE=en_CA.UTF-8;LC_NUMERIC=C;LC_TIME=en_CA.UTF-8;LC_COLLATE=en_CA.UTF-8;LC_MONETARY=en_CA.UTF-8;LC_MESSAGES=en_CA.UTF-8;LC_PAPER=en_CA.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_CA.UTF-8;LC_IDENTIFICATION=C >>> >>> >>> attached base packages: >>> [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" >>> [7] "base" >>> >>> __ >>> 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] R unstable and crashes after executing .C
There are many packages around that use .C and .Call interfaces with really huge objects and still are pretty stable. The error is most likely in your own C code and it most likely to be connected to memory allocation/deallocation or array indexing, but without having the code here no one will be able to help further. Oleg Sole Acha, Xavi wrote: > Dear R listers, > > > > I have developed a C function to be executed from R through the ".C" > interface. After doing dyn.load, the function executes properly and I get the > results. However, after executing my function, R seems to get unstable and > crashes (giving a segmentation fault and exiting) whenever I try to do > ANYTHING with a relatively large object (creating a new one or even just > writing the name of an existing one). > > > > I use R 2.4.0 under a Linux machine with 1 GB RAM. Below there is an example > of execution, so you can get an idea of what is happening: > > > > > > dyn.load("my_C_module.so"); > > res <- .C("my_C_function",.); #The function executes fine and res is ok > > dyn.unload("my_C_module.so") #I know this isn't strictly necessary > > > > #Here R is still running, but when I execute: > > > > m <- matrix(0,1000,100); #I try to create a new object and R crashes > > > > *** caught segfault *** > > address 0x10, cause 'memory not mapped' > > > > Traceback: > > 1: matrix(0, 1000, 100) > > > > Possible actions: > > 1: abort (with core dump) > > 2: normal R exit > > 3: exit R without saving workspace > > 4: exit R saving workspace > > > > > > Although I tell R to abort and give me the core dump, it doesn't succeed in > doing so. > > > > I would be grateful if anyone could tell me what could be the problem with my > C function that makes R behave this way? > > > > Thank you very much in advance, and apologies for this long email. > > > > Xavier Solé. > > > > > > > [[alternative HTML version deleted]] > > > > > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Problem with types on 64-bit
Besides the comments from Prof. Ripley, I am not sure your if statement is fully correct, because suppose v_dta_start is R_NilValue -- why then STRING_ELT( R_NilValue, 0 ) should be a valid statement in first place? In my code I often use R_NilValue to check for R-NULL, but it always works fine on both 32 and 64 bits, moreover on Linux, Mac and Windows as well... Oleg [EMAIL PROTECTED] wrote: > Hi Everyone, > > I have a problem using some working 32-bit R code with 64-bit machine ( I am > using version R-2.4.1 ). The problem occurs when I am trying to detect a NULL > STRSXP type. ( Perhaps I am doing this detection in the wrong way? ) > > On 32-bit the following works, and correctly identifies if I am passing NULL > or a valid string object: > > if ( v_dta_start != R_NilValue && STRING_ELT( v_dta_start, 0 ) != R_NilValue ) > { > dta.start = CHAR( STRING_ELT( v_dta_start, 0 ) ); > } > > Yet on a 64-bit machine I get the following errors: > > (1) when I pass NULL it goes into the if clause and then when it tries to set > dta.start it displays: > CHAR() can only be applied to a 'CHARSXP', not a 'NULL' > (2) if I pass a valid string such as "hello", I get the following: > CHAR() can only be applied to a 'CHARSXP', not a 'character' > > I have tried converting using AS_CHARACTER but that just brings up the same > messages. I have also seen S4 and PROMSXP types come up in these error > messages. > > What I don't understand is why this happens - any ideas? > > If I can supply any more info let me know, below is the CPU information for > the 64-bit machine. > > Many thanks for your help > > Tom > > 64-BIT CPU INFO > === > > processor : 0 > vendor_id : AuthenticAMD > cpu family: 15 > model : 5 > model name: AMD Opteron(tm) Processor 144 > stepping : 10 > cpu MHz : 1794.932 > cache size: 1024 KB > fpu : yes > fpu_exception : yes > cpuid level : 1 > wp: yes > flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov > pat > pse36 clflush mmx fxsr sse sse2 syscall nx mmxext lm 3dnowext 3dnow > bogomips : 3597.08 > TLB size : 1024 4K pages > clflush size : 64 > cache_alignment : 64 > address sizes : 40 bits physical, 48 bits virtual > power management: ts fid vid ttp > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Writing a package in which files must be sourced in a specific order
Put all loadings into functions and call the functions in .onLoad or .FirstLib, whatever you have there. I would simply advise not to put any code outside of functions or class methods. In this way the order of loading will not matter, it will not depend on system or alphabet and you will also be able to save the loaded image of the package for faster loading. Best Oleg hadley wickham wrote: > Dear all, > > I have been using the proto package to create objects with mutable > state for my ggplot package. This has been very successful so far, > but I have run into a problem when building/installing the package, > because the source files need to be loaded in a specific order so that > dependencies are initialised correctly. > > I have named the files so that dependencies are loaded before they are > needed, so that > > lapply(dir("~/documents/ggplot/ggplot/R", full.name=T), source) > > runs without error, but when installing the package I get an error > than indicates that the files aren't being loaded in alphabetical > order: > > Error in proto(Geom, { : object "Geom" not found > Error: unable to load R code in package 'ggplot' > Error: package/namespace load failed for 'ggplot' > > Can anyone suggest how I could get around this? > > Regards, > > Hadley > > ______ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Inherited S4 .Data prototype not assigned
Martin, thanks for commenting on the issue. I did not know if this was a "feature" and therefore did not post before, but: in EBImage, BioC, the class Image is derived from array and since recently (R2.5) I had a problem with the following constructor: new("Image", .Data=data). Finally I had to do res <- new("Image"); [EMAIL PROTECTED] <- dat, which worked. However, what I have problem with, is that R does not have a concept of public, private or protected members. Therefore I do not really see why .Data should be hidden and why then now and not initially. If this goes on this way and there will be no direct access to it, it will simply break my whole package. Best, Oleg Martin Morgan wrote: > An S4 class inheriting from another class with a prototype does not > inherit (at least sometimes) the prototype for .Data: > >> setClass("A", "numeric", 1) >> setClass("B", "A") >> new("A") > An object of class "A" > [1] 1 >> new("B") > An object of class "B" > numeric(0) > > My expectation is that prototype inheritance of .Data behaves as for > regular slots: > >> setClass("A", > + representation=representation(x="numeric"), > + prototype=prototype(x=1)) > [1] "A" >> setClass("B", "A") > [1] "B" >> new("B") > An object of class "B" > Slot "x": > [1] 1 > > I think this traces first to an errant ! that claims the data part is > filled, when it is not yet, and second to a call to getDataPart with a > primitive that occupies, but does not have, a .Data slot. > > Martin > > --- src/library/methods/R/RClassUtils.R (revision 41426) > +++ src/library/methods/R/RClassUtils.R (working copy) > @@ -45,7 +45,7 @@ > ## try for a single superclass that is not virtual > supers <- names(extends) > virtual <- NA > -dataPartDone <- length(slots)==0 || !is.na(match(".Data", snames)) > +dataPartDone <- length(slots)==0 || is.na(match(".Data", snames)) > dataPartClass <- if(dataPartDone) "ANY" else elNamed(slots, ".Data") > prototype <- [EMAIL PROTECTED] > ## check for a formal prototype object (TODO: sometime ensure that this > happens > @@ -79,8 +79,15 @@ > else if(length(slots) > 0) { > for(slotName in slotsi) { > if(identical(slotName, ".Data")) { > -if(!dataPartDone) { > -prototype <- setDataPart(prototype, > getDataPart(pri)) > +if (!dataPartDone) { > +temp <- getClass(class(pri))@slots > +if (length(temp)==0 || > +is.na(match(".Data", names(temp { > +prototype <- pri > +attributes(prototype) <- NULL > +} else > +prototype <- > +setDataPart(prototype, getDataPart(pri)) > dataPartDone <- TRUE > } > } >> sessionInfo() > R version 2.6.0 Under development (unstable) (2007-05-03 r41426) > x86_64-unknown-linux-gnu > > locale: > LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C > > attached base packages: > [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" > [7] "base" > -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] inline C/C++ in R: question and suggestion
This is a question and maybe an announcement. We've been discussing in the group that it would be nice to have a mechanism for something like "inline" C/C++ function calls in R. I do not want to reinvent the wheel, therefore, if something like that already exists, please give me a hint -- I could not find anything. If not, here is a working solution, please criticise so I could improve it. Example: I work on images (Bioconductor:EBImage) and just came to a point where I need to apply certain functions to image data, which are grey scale intensities in the range [0,1]. Assume I want to transform my image data from I(x,y,i) to exp(-(d/s)^2)*I(x,y,i), where I is the original intensity in dependence on coordinates x,y and frame i; s is a given value and d^2=(x-centre.x)^2+(y-centre.y)^2 for a given centre. Trying an R loop will run forever already on moderate image sizes as I do not see how to vectorize it. Now, below is the solution using the "inline" C code, completely in R and runs instantly. I created a small package "inline" that simply encapsulates a quite simple function "cfunction". The package source is available from http://www.ebi.ac.uk/~osklyar/inline -- please give it a try and I would be happy to hear your comments, both on already existing implementations for "inline" calls and on the current one. It is a draft and I will be happy to improve it (especially comments on how to ensure that the shared object is unloaded when the function is removed). In the example below EBImage is not required, I use randomly generated values instead of images, but the output it quite obvious. After installing "inline" the example should just work by copy-pasting. Best and thanks in advance, Oleg code <- character(17) code[1] <- " SEXP res;" code[2] <- " int nprotect = 0, nx, ny, nz, x, y;" code[3] <- " PROTECT(res = Rf_duplicate(a)); nprotect++;" code[4] <- " nx = INTEGER(GET_DIM(a))[0];" code[5] <- " ny = INTEGER(GET_DIM(a))[1];" code[6] <- " nz = INTEGER(GET_DIM(a))[2];" code[7] <- " double sigma2 = REAL(s)[0] * REAL(s)[0], d2 ;" code[8] <- " double cx = REAL(centre)[0], cy = REAL(centre)[1], *data, *rdata;" code[9] <- " for (int im = 0; im < nz; im++) {" code[10] <- "data = &(REAL(a)[im*nx*ny]); rdata = &(REAL(res)[im*nx*ny]);" code[11] <- "for (x = 0; x < nx; x++)" code[12] <- " for (y = 0; y < ny; y++) {" code[13] <- " d2 = (x-cx)*(x-cx) + (y-cy)*(y-cy);" code[14] <- " rdata[x + y*nx] = data[x + y*nx] * exp(-d2/sigma2);" code[15] <- " }}" code[16] <- " UNPROTECT(nprotect);" code[17] <- " return res;" library(inline) funx <- cfunction(signature(a="array", s="numeric", centre="numeric"), code) x <- array(runif(50*50), c(50,50,1)) res <- funx(a=x, s=15, centre=c(30,35)) image(res[,,1]) res <- funx(x, 10, c(15,15)) x11(); image(res[,,1]) -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inline C/C++ in R: question and suggestion
> That is actually a (rare) case that can be completely vectorized: Thanks for the vectorization solution. > I really like the idea! Except for the fact that it's forcing the use of > C++ which adds unnecessary overhead :P I can make it optional. I was just thinking that C++ would be more general than C. I was also asked to create a .C interface, which to me has the only difference that one needs to preallocate the result. But the same is easy to implement by pre-allocating an R variable and passing it as an argument with NULL return. > I'd like a configurable extension > [including .m] and the ability to prepend functions as code. What would > be very useful now is a set of tools that would allow you to construct > the source with R commands, so that you could compute on it, edit it :) It would be nice, but I doubt it is feasible at all and if yes, the overhead will be enormous, especially weighted by the number of people using it. However, what I would consider reasonable is to have such an inline interface to more than just C and C++. There is something for Ruby I think, but one could think of generalizing it somehow. Best, Oleg __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] inline C/C++ in R: question and suggestion
> I think the only way this would affect your package is that you need to > be able to handle embedded newlines in strings, but I can't see why you > wouldn't be able to do that automatically. That would be no problem if there were such a thing. As for now, I think the Deepayan's solution for multi line string input is quite elegant. > You might want to allow a list of signatures and code chunks to be > passed to the compiler, producing a list of functions to evaluate them, > all compiled into one DLL. Reasonable and doable. Will do using "setMethod" implementation. Thanks, Oleg -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] package: 'inline' to inline C or C++ code in R functions
Dear all, following the earlier discussion, I've made a couple of changes to 'inline' (http://www.ebi.ac.uk/~osklyar/inline). As announced before the idea of the package is to enable inlining C or C++ code in R functions using only R sources. The changes include: - it is possible to select between C and C++ - it is possible to declare a standard R function calling a C/C++ routine or an S4 method with a fixed signature - setCMethod accepts single of multiple method signatures in a list, all put in one shared object - readline syntax is used in the example for a multiline C++ source :) Thanks Duncan, Simon and Deepayan for useful comments! Not tested on Windows, I would be thankful if somebody could do it. Duncan Murdoch wrote: > Another suggestion: > > You might want to allow a list of signatures and code chunks to be > passed to the compiler, producing a list of functions to evaluate them, > all compiled into one DLL. Simon Urbanek wrote: > I really like the idea! Except for the fact that it's forcing the use > of C++ which adds unnecessary overhead :P I'd like a configurable > extension [including .m] and the ability to prepend functions as code. -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R scripts slowing down after repeated called to compiled code
I work with images with a lot of processing done in C code. Quite often I allocate memory there up to several gigs in chunks of 10-15 Mb each plus hundreds of protected dims, names etc. I had a similar problem only once when due to some erroneous use of an external library, internally created objects were not freed correctly. Otherwise, after correcting this, I never have seen any slow down on large number of objects created and manipulated. And then, it was so difficult to track the memory leak that I would really suggest to double and triple check all the memory allocations. Your code does not use any temp files? This could be a real pain. Oleg Dirk Eddelbuettel wrote: > On 25 May 2007 at 19:12, Michael Braun wrote: > | So I'm stuck. Can anyone help? > > It sounds like a memory issue. Your memory may just get fragmented. One tool > that may help you find leaks is valgrind -- see the 'R Extensions' manual. I > can also recommend the visualisers like kcachegrind (part of KDE). > > But it may not be a leak. I found that R just doesn't cope well with many > large memory allocations and releases -- I often loop over data request that > I subset and process. This drives my 'peak' memory use to 1.5 or 1.7gb on > 32bit/multicore machine with 4gb, 6gb or 8gb (but 32bit leading to the hard > 3gb per process limit) . And I just can't loop over many such task. So I > now use the littler frontend to script this, dump the processed chunks as > Rdata files and later re-read the pieces. That works reliably. > > So one think you could try is to dump your data in 'gsl ready' format from R, > quit R, leave it out of the equation and then see if what happens if you do > the iterations in only GSL and your code. > > Hth, Dirk > -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Use of Rf_duplicate
> PROTECT(y=x); and This has no sense as y is just the same pointer as x. By doing this you did not create any new data, if you modify y, x will be modified. y does not need protection as x is probably protected. > PROTECT (y = duplicate(x)); ? This will allocate new memory for data in x and copy data over there. This is correct, modifying y will not modify x. And y needs protection as it is newly allocated. > PROTECT(y = duplicate(x)); > PROTECT(z = duplicate(VECTOR_ELT(y,2))); It is not clear what you want to achieve by this. z will not be part of y any more > PROTECT(y=duplicate(x)); > PROTECT(z = VECTOR_ELT(y,2)); The correct way would be PROTECT(y=duplicate(x)); z = VECTOR_ELT(y,2); as you do not need to protect z, it is done by protecting y > And if I did create a duplicate, is there a way to destroy it manually > before the end of the function, rather than rely on on the R garbage > collector? Not that I know about. Best, Oleg -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] bug in R environments? Was: [BioC] 'recursive default argument' error...
Dear developers, has anyone experienced the problem described below? Is it a bug in handling interrupts in R? Best, Oleg Original Message Subject: Re: [BioC] 'recursive default argument' error in GOENTREZID2GO From: Diego Diez <[EMAIL PROTECTED]> ...steps that use to reach me to that point. It happens with any environment, or at least annotation packages stored as environments. I can't think of any reason why this happens but let me show you the code: > library(rae230a) > xx <- as.list(rae230aENTREZID) ^C > xx <- as.list(rae230aENTREZID) Error in as.list(rae230aENTREZID) : recursive default argument reference now, while the as.list() is working, cancel the process with control- C, and after that, the mentioned error happens whenever you try to repeat the process of transforming the environment to a list. The cancel must be done soon after hitting return, I found one case when delaying a little made me unable to reproduce the error. Other environments found in the package are not affected (i.e. rae230aSYMBOL, rae230aGENENAME, etc). Going deeper to the problem, I found that canceling the environment "loading" with any function reproduces the problem: > is(rae230aSYMBOL) ^C > is(rae230aSYMBOL) Error in .class1(object) : recursive default argument reference > rae230aSYMBOL Error: recursive default argument reference By "loading" a mean that the first time I execute as.list (rae230aSYMBOL) or is(rae230aSYMBOL) there is a delay which I assume is the environment being loaded into memory. Any further execution of the same commands will occur almost immediately. So, why should I cancel an environment loading soon after trying to load it? It happens that I was loading an environment inside a function, ran the function and wanted to cancel the execution, thus, the error appeared and the environemnt was unusable. Anyway, this seems to me more an R related issue. HTH, Diego. > sessionInfo() R version 2.5.0 (2007-04-23) powerpc-apple-darwin8.9.1 locale: C attached base packages: [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" [7] "base" other attached packages: rae230a "1.16.0" > On Jun 11, 2007, at 11:27 PM, Seth Falcon wrote: > "James W. MacDonald" <[EMAIL PROTECTED]> writes: > >> Hi Oleg, >> >> Oleg Sklyar wrote: >>> Hi, >>> >>> anybody has an idea as for the following error, the example is >>> actually >>> from the GO package examples. Thanks, Oleg >>> >>>> library(GO) >>>> egId <- "10043" >>>> egIdGoData <- GOENTREZID2GO[[egId]] >>> Error: recursive default argument reference >> >> I sometimes see that error as well. I have no idea what causes it, >> as it >> doesn't seem repeatable. Anyway, quitting R and starting over has >> always >> fixed the problem for me. > > Oleg, is this reproducible for you? I wasn't able to get the error. > Have you tried updating to R patched? > > + seth > > -- > Seth Falcon | Computational Biology | Fred Hutchinson Cancer > Research Center > http://bioconductor.org > > ___ > Bioconductor mailing list > [EMAIL PROTECTED] > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/ > gmane.science.biology.informatics.conductor -- Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] 'inline' package update
Dear all, the 'inline' package was updated to version 0.2.2 with the following changes: - functions declared using 'cfunction' can now be saved, the code is recompiled when the object is loaded (not yet implemented for setCMethod) - full path to the R binary is used for compilation allowing for the use of the correct R version when several are installed The update has been submitted to CRAN and should appear shortly. Meanwhile, the package is available from http://www.ebi.ac.uk/~osklyar/inline Best, Oleg -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] 'inline' package supports Fortran, .C calls etc
Dear developers, Duncan and me have just finished the update of the 'inline' package, which latest version (0.3.2) is now on CRAN. In addition to the previous update that featured saving and loading cfunction objects with recompiling the code, the current release supports recompiling the code in setCMethod on load, different calling conventions (.C, .Call, .Fortan) and allows to inline not only C, but also Fortran and ObjectiveC (needs testing) code, it performs clean up of all temp files created during compilation etc. Best, Oleg -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] In creating a Windows binary package (zip), how do I make the installation configurable
A configure.win file if found in the root of the package is executed during install before files from /inst are copied. What you can do is not to put your dlls in /libs but elsewhere in /inst and write a simple DOS script that will copy the required dll into /libs and rename it correspondingly. I have something similar for my package as I include a windows dll in the source package because compiling my package requires a lot of dependencies on Windows. So I have this dll in /inst/winlibs and the configure.win file deletes the /src directory (thus preventing it from compiling if inst/winlibs is not empty) and copies the dll to /libs Have a look at the source of http://www.ebi.ac.uk/~osklyar/EBImage/src/EBImage_2.1.10.tar.gz Best, Oleg [EMAIL PROTECTED] wrote: > I would like to create a binary package for Windows which when > installed will give the user an option of whether to install > one version or another. > > Here is the problem, I have a package 'FAME' that can run > in two different modes on Windows. An old mode that uses > R to connect to a remote linux machine and a new way that > uses a locally installed database. > > I would allow this decision to be made at run time, but > the DLL in the later case depends upon the database's DLL > being on the Windows machine. A runtime solution would > require at least one extra level of dynamic loading. > > So I would like something like this to happen: > the zip file contains fame-local.dll and fame-remote.dll > when the user selects 'install packages from a zip file' > the user should be given the option of which DLL > should be renamed to 'fame.dll'. > > Is this possible?? > > If so, how would I do it? > > If not, then are there an recommendations for a solution? > An alternative I can try is to have 2 separate packages, > 'fame' and 'fame-remote'. > > Thanks, > Rick Jones > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] R2.6 bug in arithmetics of S4 classes inherited from 'array', or intended behavior?
Hi, I have an S4 class directly derived from 'array' as shown in the code below (EBImage package of Bioconductor 2.1, devel), it simply contains array adding a couple of slots: setClass ("Image", representation (colormode="integer", filename="character", compression="character", resolution="numeric", features="list" ), prototype (colormode= Grayscale, filename="no-name", compression="JPEG", resolution=c(2.5e+6, 2.5e+6), features=list() ), contains = "array" ) No mathematical/arithmetical routines have been redefined for the class and this is what I was getting since R2.0, sum of two objects of class Image is Image again: version.string R version 2.5.1 (2007-06-27): > library(EBImage) > a <- Image(0, c(2,2)) > class(a+a) [1] "Image" attr(,"package") [1] "EBImage" The same stands for version.string R version 2.6.0 Under development (unstable) (2007-07-11 r42199) > library(EBImage) > a <- Image(0, c(2,2)) > class(a+a) [1] "Image" attr(,"package") [1] "EBImage" Now, in the yesterday's revision of R2.6 I get the following: version.string R version 2.6.0 Under development (unstable) (2007-07-21 r42284) > library(EBImage) > a <- Image(0, c(2,2)) > class(a+a) [1] "array" Is this the intended behavior to fall back to the base class (same for *,/,-)? If yes, could someone point me to the following two things: what are the reasons behind and WHAT operators and functions have been affected -- I will need to redefine all those then. Moreover, it is not consistent: > class(a*2) [1] "Image" attr(,"package") [1] "EBImage" > class(2*a) [1] "Image" attr(,"package") [1] "EBImage" Unfortunately, I do not have R versions installed between revisions 42199 and 42284, so I cannot narrow down to the particular revision. Thanks in advance, Oleg -- Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223493366 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R2.6 bug in arithmetics of S4 classes inherited from 'array', or intended behavior?
Narrowed down to Rev. 42246: * ~/R/Rsvn: svn up -r42245 * ~/R/Rsvn: make * ~/R/Rsvn: Rsvn CMD INSTALL ~/tmp/EBImage_2.1.13.tar.gz * ~/R/Rsvn: Rsvn > version$version.string [1] "R version 2.6.0 Under development (unstable) (2007-07-16 r42245)" > library(EBImage) > a <- Image(0,c(2,2)) > class(a+a) [1] "Image" attr(,"package") [1] "EBImage" * ~/R/Rsvn: svn up -r42246 * ~/R/Rsvn: make * ~/R/Rsvn: Rsvn CMD INSTALL ~/tmp/EBImage_2.1.13.tar.gz * ~/R/Rsvn: Rsvn > version$version.string [1] "R version 2.6.0 Under development (unstable) (2007-07-16 r42246)" > library(EBImage) > a <- Image(0,c(2,2)) > class(a+a) [1] "array" * ~/R/Rsvn: svn log -r42246 r42246 | jmc | 2007-07-16 14:32:16 +0100 (Mon, 16 Jul 2007) | 1 line implicitGeneric() and structure class ---- -- Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not, why?
Any idea why CHAR(asChar(STRING_ELT( produces NA whereas CHAR(STRING_ELT( gets a pointer to a string? It's generally expected that STRING_ELT should already be a character, but why the coercion does not work? Here is a simple example (consistent over R2.5.1-R2.6 rev 42284, I didn't check earlier versions, but it used to be different in 2.4): install.packages("inline") library(inline) sig <- signature(x="character") code1 <- " for (int i = 0; i < LENGTH(x); i++ ) Rprintf(\"%s\\n\", CHAR(STRING_ELT(x, i))); return R_NilValue; " code2 <- " for (int i = 0; i < LENGTH(x); i++ ) Rprintf(\"%s\\n\", CHAR(asChar(STRING_ELT(x, i; return R_NilValue; " setCMethod(c("p1","p2"), list(sig,sig), list(code1,code2)) #------ p1(c("str1", "str2")) # str1 # str2 # NULL p2(c("str1", "str2")) # NA # NA # NULL -- Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not, why?
Thank you Prof. Ripley. Funny enough after digging in the R sources I was just writing a reply to my own question (in case someone else would find it interesting) and it was approximately the same as what you write. It just slipped from my attention initially. O. On Mon, 2007-07-23 at 13:22 +0100, Prof Brian Ripley wrote: > I think you are asking why calling asChar on a CHARSXP gives NA_STRING. > In particular, the calls you mention *are* perfectly OK and work as > intended. > > As barely documented in R-exts, asChar is designed for vector arguments: a > CHARSXP is not a vector. It gives NA_STRING for invalid inputs. > The as family of functions are designed to coerce as necessary user > inputs, and CHARSXPs are not visible at R level. > > In general, with internal functions you are expected to read the code to > find out what they do before using them. > > On Mon, 23 Jul 2007, Oleg Sklyar wrote: > > > Any idea why CHAR(asChar(STRING_ELT( produces NA whereas > > CHAR(STRING_ELT( gets a pointer to a string? It's generally expected > > that STRING_ELT should already be a character, > > It is required to be a CHARSXP, but 'character' usually refers to STRSXP. > > > but why the coercion does not work? Here is a simple example (consistent > > over R2.5.1-R2.6 rev 42284, I didn't check earlier versions, but it used > > to be different in 2.4): > > There is no R '2.4', but the behaviour of asChar was the same in R 2.4.0 > except for the adding of SYMSXP at > > - > r40358 | maechler | 2007-01-04 11:07:04 + (Thu, 04 Jan 2007) | 1 line > > eliminate CHAR_STAR in methods/src/ as per old "todo" > - > > which message does not help me at all. Perhaps Martin can explain? > > > > install.packages("inline") > > > > library(inline) > > > > sig <- signature(x="character") > > code1 <- " > > for (int i = 0; i < LENGTH(x); i++ ) > >Rprintf(\"%s\\n\", CHAR(STRING_ELT(x, i))); > > return R_NilValue; > > " > > > > code2 <- " > > for (int i = 0; i < LENGTH(x); i++ ) > >Rprintf(\"%s\\n\", CHAR(asChar(STRING_ELT(x, i)))); > > return R_NilValue; > > " > > > > setCMethod(c("p1","p2"), list(sig,sig), list(code1,code2)) > > > > > > #-- > > p1(c("str1", "str2")) > > # str1 > > # str2 > > # NULL > > > > p2(c("str1", "str2")) > > # NA > > # NA > > # NULL > > > > > > > -- Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R2.6 bug in arithmetics of S4 classes inherited from 'array', or intended behavior?
John, thank you for clarification. I just wonder that some packages could also see that their behaviour has changed. Anyway, I redefined Arith as the rest seems to work fine and also it is pretty straight forward in my case what the result of Arith should be. Thanks again. Oleg On Sat, 2007-07-28 at 09:38 -0400, John Chambers wrote: > Yes, this is the intended behavior. Nothing to do with implicit > generics, but with some ongoing changes to make class "structure" and > related classes such as "array" give trustworthy results consistent > with the S language idea of vector structures. There's a short note > in NEWS about the changes, added for the revision where the change was > introduced. > > As for programming your class, using showMethods("+") as below will > tell you that the method is inherited from the group generic function > "Ops", so a method for that function is needed, if you _really_ know > what the results should be. > > The behavior of the base package code for operations is unacceptable > for trustworthy computations with classes. Roughly, it takes the > attributes of the left-hand operand and inserts those into the result > of operating on the vector or array part. So in the example below we > have two unrelated classes that both extend "array". A mixed > operation gives a result from class "A1" if done in one order and from > "A2" if done in the other order. Both are likely wrong and quite > possibly seriously misleading. > > Even if the two objects came from the same class, copying one set of > attributes makes sense only if the two sets are identical (an > expensive test) and not necessarily even then, if any of the > attributes would be invalidated by the operation. > > Two acceptable results are either to reduce to the common inheritance > (array, here) or to call the result undefined and throw an error. The > current implementation in 2.6.0 is the first, as the example shows: > > > setClass("A1", contains="array", representation(flag = "character")) > [1] "A1" > > setClass("A2", contains="array", representation(positive = > "logical")) > [1] "A2" > > a1 = new("A1", array(1:8, rep(2,3)), flag = "test") > > a2 = new("A2", array(8:1, rep(2,3)), positive = rep(TRUE,8)) > > a1+a2 > , , 1 > > [,1] [,2] > [1,]99 > [2,]99 > > , , 2 > > [,1] [,2] > [1,]99 > [2,]99 > > > showMethods("+") > Function: + (package base) > e1="A1", e2="A2" > (inherited from: e1="array", e2="array") > (definition from function "Ops") > > > Oleg Sklyar wrote: > > Narrowed down to Rev. 42246: > > > > * ~/R/Rsvn: svn up -r42245 > > * ~/R/Rsvn: make > > * ~/R/Rsvn: Rsvn CMD INSTALL ~/tmp/EBImage_2.1.13.tar.gz > > * ~/R/Rsvn: Rsvn > > > > > version$version.string > > > > > [1] "R version 2.6.0 Under development (unstable) (2007-07-16 r42245)" > > > > > library(EBImage) > > > a <- Image(0,c(2,2)) > > > class(a+a) > > > > > [1] "Image" > > attr(,"package") > > [1] "EBImage" > > > > * ~/R/Rsvn: svn up -r42246 > > * ~/R/Rsvn: make > > * ~/R/Rsvn: Rsvn CMD INSTALL ~/tmp/EBImage_2.1.13.tar.gz > > * ~/R/Rsvn: Rsvn > > > > > version$version.string > > > > > [1] "R version 2.6.0 Under development (unstable) (2007-07-16 r42246)" > > > > > library(EBImage) > > > a <- Image(0,c(2,2)) > > > class(a+a) > > > > > [1] "array" > > > > * ~/R/Rsvn: svn log -r42246 > > > > r42246 | jmc | 2007-07-16 14:32:16 +0100 (Mon, 16 Jul 2007) | 1 line > > > > implicitGeneric() and structure class > > > > > > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] suggesting \alias* for Rd files (in particular for S4 method documentation)
Hi, I do not know if everybody finds index pages of the html-formatted R help useful, but I know I am at least not the only one who uses them extensively to get the overview of functions and methods in a package (even for my own package). Problems arise, however, if a lot of S4 methods need to be documented blowing out the index with (generally irrelevant) entries like: write.image,Image,missing-method write.image,Image,character-method instead of a simple "write.image". I also do not believe anyone really does something like "help(write.image,Image,missing-method)" on the command line, thus these structures are more for internal linking than for users. Therefore, I would suggest to introduce a modification of the \alias keyword, that would do all the same as the standard \alias keyword, yet it would *hide* that particular entry from the index. Reasonable construction could be something like \alias*{} yielding \alias{write.image} \alias*{write.image,Image,missing-method} \alias*{write.image,Image,character-method} Alternatively: \alias{write.image} \alias[hide]{write.image,Image,missing-method} \alias[hide]{write.image,Image,character-method} Any comments? For me, the current way around is to avoid usage sections with \S4method all together, substituting them with pairs of \section{Usage}{\preformatted{ }} \section{Arguments}{ } and putting all aliases marked above with * into internals, which is definitely not the best way of going around documentation and code/documentation mismatches. Best regards, Oleg -- Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-464466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] suggesting \alias* for Rd files (in particular for S4 method documentation)
Maybe I need to add a comment. The idea is not to avoid providing documentation for specific methods, rather to be able to selectively hide things in the index keeping the index sane. On the developer discretion, as generally I think the developer should decide what the user can see and how. It would still bring up the help page on typing the help command e.g. help(write.image,Image,missing-method) and it would still allow the corresponding referencing. Moreover it would allow to specifically show those method definitions, which *are* important rather than all that exist. There are not so many cases where the generic is often reused with different classes across different packages and in those cases it is truly reasonable to have a comprehensive help listing the signatures in the index as well. But take the above "write.image" -- I doubt there is another implementation of this generic anywhere in the R world and my current documentation would have *one* page for all -- the generic and all the methods, so why to have 3 visible links in the index that do not really give more information than the page itself (where all use cases are listed). I however agree that the indented structure as Martin suggested would be a big step forward already. James: moving things to internals does not work as if you provide \S4method clause you *must* provide a corresponding alias, otherwise you get a check warning and for example on Bioconductor you will have problems with BioC maintainers, and it is good so ;-) Best, Oleg On Thu, 2007-08-30 at 10:06 -0400, James W. MacDonald wrote: > I think Oleg makes a good point here, and I don't see how his suggestion > would hide any documentation. > > As an example, start R and then open the HTML help page, and go to the > Category package. If you click on any one of > > annotation,GOHyperGParams-method > categoryName,GOHyperGParams-method > conditional,GOHyperGParams-method > conditional<-,GOHyperGParams-method > GOHyperGParams-class > ontology,GOHyperGParams-method > ontology<-,GOHyperGParams-method > show,GOHyperGParams-method > > You will be sent to the same help page, which contains the documentation > for all those specific methods. The question here is do we really this > many-to-one relationship in the HTML pages? > > In general (Oleg notwithstanding), I think the HTML pages are used > primarily by new users to R, and having such an overload on the index > page for this package is IMO a disservice to these people. Having just > one link, GOHyperGParams-class, or possibly an additional > GOHyperGParams-methods would be much cleaner. > > There already exists a mechanism for keeping internal methods from > showing up in the HTML indices: adding \keyword{internal} to the end of > the .Rd file. However, this hides all the \alias{} (and \name{}) > entries, so won't do what Oleg wants unless you have two separate .Rd > files, one containing the \alias{} names you want to show, and the other > with the 'internal' keyword. > > Best, > > Jim > > > > Martin Morgan wrote: > > Hi Oleg -- > > > > On the usefulness of write.image,Image,character-method, in the end I > > really want documentation on specific methods. Maybe the issue is one > > of presentation? > > > > write.image > > Image,character-method > > Image,missing-method > > > > or, in a little more dynamic world, a '+' in front of write.image to > > expand the methods list. > > > > alias* is a little strange, because it implies you're writing > > documentation, but then hiding easy access to it! This is not a strong > > argument against introducing alias*, since no one is forced to use it. > > > > It also suggests that your documentation is organized by generic, > > which might also be a bit unusual -- I typically have an object (e.g., > > an Image) and wonder what can be done to it (e.g., write it to > > disk). This suggests associating method documentation with object > > documentation. Multiple dispatch might sometimes make this difficult > > (though rarely in practice?). Separately documenting the generic is > > also important. > > > > Martin > > > > Oleg Sklyar <[EMAIL PROTECTED]> writes: > > > >> Hi, > >> > >> I do not know if everybody finds index pages of the html-formatted R > >> help useful, but I know I am at least not the only one who uses them > >> extensively to get the overview of functions and methods in a package > >> (even for my own package). Problems arise, however, if a lot of S4 > >> methods need to be documented blowing out the index with (generally
Re: [Rd] Font problem (PR#9906)
OS: Ubuntu 7.10 Gutsy Gibbon GNOME: 2.19.92 (build: 04/09/07) Linux 2.6.22-10-generic #1 SMP x86_64 GNU/Linux It works perfectly fine here with 2 recent versions of R (errors because dialogues closed without any data): * ~: Rd R version 2.7.0 Under development (unstable) (2007-09-06 r42789) > edit(data.frame()) Error in max(lengths) : invalid 'type' (list) of argument > * ~: R R version 2.5.1 (2007-06-27) > edit(data.frame()) Error in max(..., na.rm = na.rm) : invalid 'type' (list) of argument > On Wed, 2007-09-12 at 16:52 +0200, [EMAIL PROTECTED] wrote: > Full_Name: M. Muñoz Márquez > Version: 2.3.1 > OS: Ubuntu > Submission from: (NULL) (150.214.231.66) > > > Here is the reply to the edit command using gnome > > edit(data.frame()) > Erro en dataentry(datalist, modes) : invalid device > Además: Warning message: > unable to create fontset -*-fixed-medium-r-normal--13-*-*-*-*-*-*-* > > Sys.getlocale() > [1] > "LC_CTYPE=es_ES.UTF-8;LC_NUMERIC=C;LC_TIME=es_ES.UTF-8;LC_COLLATE=es_ES.UTF-8;LC_MONETARY=es_ES.UTF-8;LC_MESSAGES=es_ES.UTF-8;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C" > > Under kde it works fine. > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Call C code from R
1) You include libraries just like you would always do. Here is the example from my package that uses external libraries of ImageMagick and GTK, standard libraries (stdio, pthread) and R libraries: #include #include #include #include #include #include #include #ifndef WIN32 # include #endif /* These are to use GTK */ #ifdef USE_GTK # include # ifdef WIN32 typedef unsigned long ulong; # include # else # include # endif #endif 2) The above is C code and no namespaces are used, however the piece below is C++ code using namespaces and STL. With C++ you only need to make sure that you export your function as C: extern "C" {}: #include #include #include #include #include #include /* list of STL, C++ */ #include #define BG 0.0 struct TheSeed { int index, seed; }; typedef std::list IntList; typedef std::list SeedList; Oleg On Mon, 2007-09-17 at 09:14 +0700, Ольга К. Камнева wrote: > Hello, All! > > I'm new for R-devel list. And I'd like to ask some questions, > maybe they will be stuped for the most part of members of the > list. > I need to call function which is written in C++ from R. > My questions are: > 1. How should I include libraries (for example, iomanip, > sstream, iostream)? > 2. Can I use namespace? > > Thanks All :) > Olga > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] How to test if R is running on a Mac
I had the same problem. The following line using either 'apple' or 'darwin' does the job for me (tested on many different Macs): length(grep("apple", tolower(Sys.getenv("R_PLATFORM" != 0 On Wed, 2007-09-19 at 15:41 +0200, Gorjanc Gregor wrote: > Hi! > > Is there any way to test if R is running on a Mac? I usually use > value of .Platform$OS.type for windows or unix, but Mac falls in the > latter group. > > Thanks, Gregor > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] append/concatenate an element to a list in C-language
Hi. I believe it is virtually impossible, maybe even not virtually: if you manage to do so somehow, please report it as a bug because such things must be impossible as they break the integrity of R and data. Forget about changing size in place: it is C and 'realloc' would be about the only way to do it, which would not be smart even if you can find a pointer to what you want to change. However, consider you want to copy (after all it is a list, which is an array of pointers and you might think you only need to copy pointers, which is cheap even for a huge list). Consider a simple example: you allocVector(VECSXP,n+1); then either memcpy n elements (if you manage to get a pointer say with DATAPTR, and R-API is smart not to allow you to get it that simple) or you simply SET_VECTOR_ELT(new, i, VECTOR_ELT(old, i)) and then add the last one. You know what would be wrong about this idea? You cannot be sure that pointers to elements of the old list are not modified/deleted elsewhere, thus affecting your new list! Even if you overwrite the list in place, providing such software to users is error prone as users do not know about your ideas of keeping everything tidy. The only right way to replicate (if you want to do it in C) is copy and copy duplicating each element of the list, i.e.: SEXP f(SEXP old, SEXP v) { SEXP new; int i,nprotect=0; PROTECT(new=allocVector(VECSXP,n+1)); nprotect++; for(i=0;i dear people, > > i need to code a function in C working in R and receives two R SEXP > objects as parameters, where one is a list and another is a vector of > integers: > > void f(SEXP list, SEXP vector) { > > ... > > return list; > } > > > and it should return the given list with the integer vector concatenated > at the end (as the last element of the list). the list can be really big > so i would not like to create a new list from scratch and copy all the > elements, including the additional one. i'm also interested in knowing > how should i properly handle protections of the SEXP objects when doing > this. > > i've been looking at the R source code for everything that has to do > with CAR, CDR, CONS, and even found functions with promising names like > listAppend or GrowList but i have not been able to figure this out nor i > haven't been able to find any reference on how to do this by googling > all around, so any help will be very much appreciated. > > > thanks a lot!!! > > robert. > > __ > 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] How to overload the assignment operator?
I think the question is not about setReplaceMethod, here is the idea: ## setReplaceMethod-like situation: > names(x) <- c("a","b") ## the question of interest > class(x) [1] myClass > y <- x ## this to run a user-defined clone method for class 'myClass' Why is this important - if x contains data that actually needs to be duplicated this would come handy. I'd like to know the solution as well. Maybe it's my ignorance that I don't know the answer and the response will be RTFM - fine, just point out where :) Best, Oleg On Tue, 2007-11-13 at 14:52 +0100, Matthias Kohl wrote: > are you looking for "setReplaceMethod"? > hth > Matthias > > Jens Oehlschlägel wrote: > > Dear all, > > > > what is the proper way to make the assignment operator generic and define > > methods depending on the class of the assigned value? > > > > Best regards > > > > > > Jens Oehlschlägel > > > > P.S. I vaguely remember that this was possible in S+. In R I tried to no > > avail: > > > > # using this like h<-1:3 gives Error: in `<-.default`(h, 1:3) : invalid > > (do_set) left-hand side to assignment > > "<-.default" <- get("<-") > > > > # using this does fail on subassignments like: h <- 1:3 ; h[1] <- 7 (h > > still is 1:3) > > "<-.default" <- function(x, value){ > > assign(deparse(substitute(x)), value, parent.frame()) > > invisible(x) > > } > > > > # this seems to work > > "<-" <- function(x, value){ > > UseMethod("<-", value) > > } > > > > # whenever the assigned value has class 'ff' I want to do execute > > something like > > "<-.ff" <- function(x, value){ > > y <- clone(value) > > assign(deparse(substitute(x)), y, parent.frame()) > > y > > } > > > > > > > >> version > >> > >_ > > platform i386-pc-mingw32 > > arch i386 > > os mingw32 > > system i386, mingw32 > > status > > major 2 > > minor 6.0 > > year 2007 > > month 10 > > day03 > > svn rev43063 > > language R > > version.string R version 2.6.0 (2007-10-03) > > > > -- > > > > __ > > 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 -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Nested SEXP functions
You assume that when you call any of those functions its arguments are protected (as it is the case when the function is called from within R). However, I do not see why they would be if you do not do it explicitly. Therefore, it is very likely that a result of one function, used in turn in a call to the other one, is garbage collected on the way and what you get can be totally wrong. The more R API calls you have within each of those functions, the more likely it is that the unprotected argument is garbage collected as the events are triggered within some of those calls. One should be very careful about protecting things in .Call interface. Now here is what I mean in a coding example: ## R CODE # z1 = .Call("interfaceCFun1", as.numeric(x)) # OK z2 = .Call("interfaceCFun12", as.numeric(x)) # Can be !OK SEXP f1(SEXP); // C code SEXP interfaceCFun(SEXP x) { / /x is protected, so it prints smth and returns new var to R return f1(x); } SEXP interfaceCFun2(SEXP x) { // for first call to f1 it prints smth of x and returns new var // to the second call, however in the second call the argument is no // more x, it is no more protected and can be released by GC, so it is // no clear what it is going to print there! return f1(f1(x)); // this would be correct: SEXP y; PROTECT(y=f1(x)); // now when we print in the following function we have memory protected y = f1(y) UNPROTECT(1); return y; } SEXP f1(SEXP x) { Rprintf("\n"); // can possibly trigger garbage collector Rprintf(REAL(x)[0]); return allocVector(REALSXP,2); } Best, Oleg On Thu, 2007-11-15 at 17:10 -0500, [EMAIL PROTECTED] wrote: > Hey All, > > I was wondering if I could solicit a little advice. I have been > experiencing some quirkiness in my C code through .Call. > Unfortunately, my program is rather large, so I'm trying to create a > brief example. Yet before I do, I thought maybe a conceptual question > would be sufficient. > > Basically, I'm writing up a multidimensional Gaussian likelihood (with > spatial covariances and other enhancements). What I'm noticing is > that when I have nested SEXP functions I get inconsistent results. > > Here is what I am essentially trying to accomplish when looking at the > Gaussian kernel: > > l(beta) = (y-X*beta)^T V^{-1} (y-X*beta) > > Now in order to accomplish this, I wrote various linear algebra > subroutines to handle the R objects, we'll call this: > > SEXP XtimesY(SEXP X,SEXP Y); // X*Y > SEXP XminusY(SEXP X,SEXP Y); // X-Y > SEXP tX(SEXP X); // X^T > SEXP mycholinv(SEXP V); // Use cholesky decomposition for inverse > > Now, what I'm noticing is that if I call each routine individually > such as: > > pt1=XtimesY(X,beta); // X*beta > pt2=Xminus(Y,pt1); // Y-X*beta > pt3=tX(pt2); // (Y-X*beta)^T > pt4=mycholinv(V); //V^{-1} > pt5=XtimesY(pt2,pt4); // (Y-X*beta)^T V^{-1} > result=XtimesY(pt5,pt2); //(y-X*beta)^T V^{-1} (y-X*beta) > > Then the result is correct. But if instead I want to save some lines > of code, and I use: > > result=XtimesY(XtimesY(tX(XminusY(Y,XtimesY(X,beta))),mycholinv(V)),XminusY(Y,XtimesY(X,beta))) > > I don't always get the same result. Now my question is, should I > expect weird and ugly things to happen when nesting SEXP functions > such as this? Or is it even highly discouraged in general in C to do > something like this? > > If this should work, then I'll need to go through each one of the > functions and try to diagnose where the problem lies. Yet if it > shouldn't work, I'll stick with the first approach I have going now. > > Thanks in advance for your input! > > __ > 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] Returning vectors of two different data types back to R environment (from C).
SEXP x,y,res; PROTECT(res = allocVector(VECSXP, 2)); SET_VECTOR_ELT(res, 0, x = allocVector(REALSXP, 20)); SET_VECTOR_ELT(res, 1, y = allocVector(INTSXP, 10)); // code with x,y goes here UNPROTECT(1); return res; On Fri, 2007-11-16 at 15:49 -0500, Charles Danko wrote: > Hello, > > Quick question. > > I have written a C function - I would like to make it return two vectors to > the R environment - one STRSXP vector, and one INTSXP vector. Is this > possible/ easy to do using the API? > > I looked, but could not find the answer to this question in the "Writing R > Extensions" guide. > > Thanks very much for your help! > Charles > > [[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] Building packages
These files in the SVN tree does not harm the things that are checked in. However it is indeed reasonable to keep the rubbish out, so: > I've started a new package and I'm trying to work out the best way to do > it. I'm managing my package source directory with SVN, but "R CMD build" > likes to dump things in the inst/doc directory when making vignette PDF > files. I don't want to keep these in SVN (they aren't strictly > 'source'), so it set me thinking. Solution 1: copy the package SVN dir elsewhere and build/install from there Solution 2: a better one, make a 2-liner shell script that runs solution 1 (what I do) This will also prevent gcc from populating your svn src directory with .o, .so, .d, .dll files. > One of the other projects I work with has an out-of-source build system. > You make a 'build' directory, run a config system (cmake-based) and then > 'make' does everything in the build directory without touching the > source tree. Very nice and neat. How much work would it take to have > something similar for building R packages? At present I've just got some > svn:ignore settings to stop SVN bothering me. R does understand 'configure' which is more reasonable then to require cmake to be installed. Think of multiplatform builds etc. > I also hit the problem of vignettes needing the package to be > installed before being able to build them, but not being able to install > the package because the vignettes wouldn't build without the package > already being installed. The fix is to build with --no-vignettes, then > install the package, then build with the vignettes enabled. Seems > kludgy, plus it means that vignettes are always built with the currently > installed package and not the currently-being-installed package. So I > install and do a second pass to get it all right again. If I am not mistaken R CMD build builds the package temporarily and uses that build to build the vignette, so where is the problem? All my vignettes build fine on both Linux and Windows and on Windows you actually see that running R CMD build --binary builds the source code two times - exactly for the above purposes. > > Or am I doing it wrong? > > Once I get smooth running of R package development and SVN I might > write it up for R-newsletter - there's a couple of other tricks I've had > to employ... What exactly? > > Barry > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4 class extending data.frame?
I had the same problem. Generally data.frame's behave like lists, but while you can extend list, there are problems extending a data.frame class. This comes down to the internal representation of the object I guess. Vectors, including list, contain their information in a (hidden) slot .Data (see the example below). data.frame's do not seem to follow this convention. Any idea how to go around? The following example is exactly the same as Ben's for a data.frame, but using a list. It works fine and one can see that the list structure is stored in .Data * ~: R R version 2.6.1 (2007-11-26) > setClass("c3",representation(comment="character"),contains="list") [1] "c3" > l = list(1:3,2:4) > z3 = new("c3",l,comment="hello") > z3 An object of class “c3” [[1]] [1] 1 2 3 [[2]] [1] 2 3 4 Slot "comment": [1] "hello" > [EMAIL PROTECTED] [[1]] [1] 1 2 3 [[2]] [1] 2 3 4 Regards, Oleg On Thu, 2007-12-13 at 00:04 -0500, Ben Bolker wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > I would like to build an S4 class that extends > a data frame, but includes several more slots. > > Here's an example using integer as the base > class instead: > > setClass("c1",representation(comment="character"),contains="integer") > z1 = new("c1",55,comment="hello") > z1 > z1+10 > z1[1] > [EMAIL PROTECTED] > > -- in other words, it behaves exactly as an integer > for access and operations but happens to have another slot. > > If I do this with a data frame instead, it doesn't seem to work > at all. > > setClass("c2",representation(comment="character"),contains="data.frame") > d = data.frame(1:3,2:4) > z2 = new("c2",d,comment="goodbye") > z2 ## data all gone!! > z2[,1] ## Error ... object is not subsettable > [EMAIL PROTECTED] ## still there > > I can achieve approximately the same effect by > adding attributes, but I was hoping for the structure > of S4 classes ... > > Programming with Data and the R Language Definition > contain 2 references each to data frames, and neither of > them has allowed me to figure out this behavior. > > (While I'm at it: it would be wonderful to have > a "rich data frame" that could include as a column > any object that had an appropriate length and > [ method ... has anyone done anything in this direction? > ?data.frame says the allowable types are > "(numeric, logical, factor and character and so on)", > but I'm having trouble sorting out what the limitations > are ...) > > hoping for enlightenment (it would be lovely to be > shown how to make this work, but a definitive statement > that it is impossible would be useful too). > > cheers > Ben Bolker > > -----BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.6 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org > > iD8DBQFHYL1pc5UpGjwzenMRAqErAJ9jj1KgVVSGIf+DtK7Km/+JBaDu2QCaAkl/ > eMi+WCEWK6FPpVMpUbo+RBQ= > =huvz > -END PGP SIGNATURE- > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4 class extending data.frame?
Thanks for your comments. I cannot recall now when I had the situation that I wanted to inherit from a data.frame, but the fact was that I could not set the data. So now it just popped up and I thought it was indeed unfortunate that data.frame structure did not follow the same principles as other "standard" classes do. Regarding named lists, modifying .Data directly may play a bad joke until one clearly thinks about all aspects of the object. I had a similar situation as well and after that am very careful about such things (well, I had it in C when creating an object with names attribute). The thing is: names is and independent attribute, so there is a potential possibility to set .Data at different length from names etc when working directly. Thanks for pointing this out anyway. Regards, Oleg On Thu, 2007-12-13 at 07:01 -0800, Martin Morgan wrote: > Ben, Oleg -- > > Some solutions, which you've probably already thought of, are (a) move > the data.frame into its own slot, instead of extending it, (b) manage > the data.frame attributes yourself, or (c) reinvent the data.frame > from scratch as a proper S4 class (e.g., extending 'list' with > validity constraints on element length and homogeneity of element > content). > > (b) places a lot of dependence on understanding the data.frame > implementation, and is probably too tricky (for me) to get right,(c) > is probably also tricky, and probably caries significant performance > overhead (e.g., object duplication during validity checking). > > (a) means that you don't get automatic method inheritance. On the plus > side, you still get the structure. It is trivial to implement methods > like [, [[, etc to dispatch on your object and act on the appropriate > slot. And in some sense you now know what methods i.e., those you've > implemented, are supported on your object. > > Oleg, here's my cautionary tale for extending list, where manually > subsetting the .Data slot mixes up the names (callNextMethod would > have done the right thing, but was not appropriate). This was quite a > subtle bug for me, because I hadn't been expecting named lists in my > object; the problem surfaced when sapply used the (incorrectly subset) > names attribute of the list. My solution in this case was to make sure > 'names' were removed from lists used to construct objects. As a > consequence I lose a nice little bit of sapply magic. > > > setClass('A', 'list') > [1] "A" > > setMethod('[', 'A', function(x, i, j, ..., drop=TRUE) { > + [EMAIL PROTECTED] <- [EMAIL PROTECTED] > + x > + }) > [1] "[" > > names(new('A', list(x=1, y=2))[2]) > [1] "x" > > Martin > > Oleg Sklyar <[EMAIL PROTECTED]> writes: > > > I had the same problem. Generally data.frame's behave like lists, but > > while you can extend list, there are problems extending a data.frame > > class. This comes down to the internal representation of the object I > > guess. Vectors, including list, contain their information in a (hidden) > > slot .Data (see the example below). data.frame's do not seem to follow > > this convention. > > > > Any idea how to go around? > > > > The following example is exactly the same as Ben's for a data.frame, but > > using a list. It works fine and one can see that the list structure is > > stored in .Data > > > > * ~: R > > R version 2.6.1 (2007-11-26) > >> setClass("c3",representation(comment="character"),contains="list") > > [1] "c3" > >> l = list(1:3,2:4) > >> z3 = new("c3",l,comment="hello") > >> z3 > > An object of class “c3” > > [[1]] > > [1] 1 2 3 > > > > [[2]] > > [1] 2 3 4 > > > > Slot "comment": > > [1] "hello" > > > >> [EMAIL PROTECTED] > > [[1]] > > [1] 1 2 3 > > > > [[2]] > > [1] 2 3 4 > > > > Regards, > > Oleg > > > > On Thu, 2007-12-13 at 00:04 -0500, Ben Bolker wrote: > >> -BEGIN PGP SIGNED MESSAGE- > >> Hash: SHA1 > >> > >> I would like to build an S4 class that extends > >> a data frame, but includes several more slots. > >> > >> Here's an example using integer as the base > >> class instead: > >> > >> setClass("c1",representation(comment="character"),contains="integer") > >> z1 = new("c1",55,comment="hello") > >> z1 > >> z1+10 > >> z1[1] > &
Re: [Rd] help files for load and related functions
Dear Patrick, Firstly, and most importantly, I do not think that your post qualified for Rd! Please use the correct mail list for such things: R-help. I do not think anybody on Rd wants mailboxes clogged with irrelevant messages. Back to your question: it is not clear if you are confused, or your 'user' is confused, but all three help pages look pretty clear and straight forward to me. Moreover, I do not see any connection between attach and library, which you find logical: - load - the general use of this one is to load external data sets, e.g. load serialised R object(s) (as the example shows). Until you load, you cannot use the object as it has no relation to the R session and can be e.g. a file sitting somewhere on a network - attach - the general use of this one would be to access elements of a data set directly, without the data set name specifier and the accessor operator, such as $, thus as the help page states - it is used to add the data set to the search path (as the example shows). If you look at the example, you do not have to call attach to be able to use data, data could have existed there before and what you effectively get with attach is a more convenient way of dealing with the data - library - is used to load *and* attach an R package, which is not exactly the same as a serialised R object(s), but a full set of other functionality. Attaching packages is just a part of the loading process, which occurs basically when the package becomes visible to the user. Same as with load, you cannot use the package until you load it. There is not a hint of similarity between loading a package and attaching a data set as I see it. Regards, Oleg On Mon, 2007-12-17 at 11:00 +, Patrick Burns wrote: > I recently had a discussion with a user about loading > and attaching in R. I was surprised that the help files > don't provide a very clear picture. > > From my point of view 'load' and 'attach' are very > similar operations, the difference being that 'attach' > creates a new database on the search list while 'load' > puts all the objects into the global environment. > > The help file for 'load' is inexplicit that this is what > happens. The 'load' and 'attach' help files neither refer > to the other in their See Also. > > Furthermore, the 'library' help file talks about "loading" > packages. I would suggest that it should use "attaching" > as that is the analogous operation. > > None of these three help files (nor that of 'save') has a > Side Effects section. Personally I think that all help files > should have a Side Effects section (to make it clear to > new users what side effects are and that they are not a > good thing for most functions to have). I can understand > there could be another point of view on that. However, I > definitely think that there should be a Side Effects section > in the help files of functions whose whole point is a side > effect. > > Patrick Burns > [EMAIL PROTECTED] > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of S Poetry and "A Guide for the Unwilling S User") > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Installation of RGtk2 (PR#10519)
RGtk2 is a packages that is a wrapper for GTK+ GUI toolkit. GTK+ itself is a large separate software package. I do not know if GTK+ is delivered within the RGtk2, but I doubt it as the former is quite big (at least with my own package that uses GTK, users need to install GTK separately). Now on Windows you can install GTK runtime environment as any other application by downloading it from here: http://gladewin32.sf.net and running a Windows-standard installation. The default installation should be fine. After you have GTK installed, reinstall the RGtk2 package if it does not work automatically. Actually I just checked http://www.ggobi.org/rgtk2/ and it says that indeed you need to install GTK separately. Best, Oleg On Mon, 2007-12-17 at 15:00 +0100, [EMAIL PROTECTED] wrote: > Full_Name: Caroline Keef > Version: 2.6.1 > OS: Windows XP > Submission from: (NULL) (195.171.203.131) > > > I have tried to install the package rggobi which if I'm right requires the > package RGtk2 > If I install RGtk2 using the install.packages (I used the UK (Bristol) > mirror, I > haven't tried any other mirror) within R and then use library(RGtk2) I get the > following message box > > "This application has failed to start because libtak-1.0-0.dll was not found. > Re-installing the application may fix this problem." > > I click ok and the following is printed to the R console. > > Error in dyn.load(file, ...) : > unable to load shared library > 'C:/PROGRA~1/R/R-26~1.1/library/RGtk2/libs/RGtk2.dll': > LoadLibrary failure: The specified module could not be found. > > > [1] "PLEASE RESTART R BEFORE TRYING TO LOAD THE PACKAGE AGAIN" > Error in .C("R_gtkInit", length(args), x = args, PACKAGE = "RGtk2") : > C symbol name "R_gtkInit" not in DLL for package "RGtk2" > In addition: Warning message: > In fun(...) : > Failed to load RGtk2 dynamic library:Error in dyn.load(file, ...) : > unable to load shared library > 'C:/PROGRA~1/R/R-26~1.1/library/RGtk2/libs/RGtk2.dll': > LoadLibrary failure: The specified module could not be found. > > > Error : .onLoad failed in 'loadNamespace' for 'RGtk2' > Error: package/namespace load failed for 'RGtk2' > > I've re-started R again and the same happened. I've also tried downloading > the > RGtk2 zip files from the CRAN website and installing from a local zip file > option which gave the same results. I haven't tried re-installing R to get > around this problem. > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Finding windows DLLs
Should adding PREFIX/library/XML/libs to PATH before system32 solve the issue as Windows relies on PATH when searching for libs as well? Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 Martin Morgan wrote: > The XML package relies on libxml2.dll (e.g., bundled with the CRAN > binary) installed in library/XML/libs. Unfortunately, > c:/WINDOWS/system32/libxml2.dll will be found and loaded before > this. > > Is there any programatic solution? > > Thanks, > > Martin __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S3 vs S4 for a simple package
In EBImage I have a very similar situation (well, with more methods). It would be impossible to use S3 in my case as my data structures are images and I need at least two dimensions (but in fact use 3), thus 2 variables to dispatch on in [, which are defined for multiple configurations like integer,missing; integer,integer or logical,missing. To make things faster in S4, I however did not put data into a separate slot, rather derived the class with contains="array", so effectively giving the class all the functionality of array, and only changing the default behaviour for a couple of methods. Obviously one can access the data low level through the @.Data slot, both in C and R. This was a real performance boost compared to putting data into a separate slot, otherwise even working with many images and large sets I have not really noticed any performance issues that would be connected with the speed of method dispatch (although I would not argue that there are none). Regards, Oleg Dr Oleg Sklyar | EBI-EMBL, Cambridge CB10 1SD, UK | +44-1223-494466 John Chambers wrote: > Prof Brian Ripley wrote: >> On Mon, 7 Jan 2008, Robin Hankin wrote: >> >> >>> I am writing a package and need to decide whether to use S3 or S4. >>> >>> I have a single class, "multipol"; this needs methods for "[" and "[<-" >>> and I also need a print (or show) method and methods for arithmetic +- >>> */^. >>> >>> In S4, an object of class "multipol" has one slot that holds an array. >>> >>> Objects of class "multipol" require specific arithmetic operations; >>> a,b being >>> multipols means that a+b and a*b are defined in peculiar ways >>> that make sense in the context of the package. I can also add and >>> multiply >>> by scalars (vectors of length one). >>> > One thing you cannot do in S3 is to have methods that depend on anything > but the first argument. Do you want something sensible for 1 + a when > a is a "multipol"? The default call to the primitive version may or may > not give you what you want. >>> My impression is that S3 is perfectly adequate for this task, although >>> I've not yet finalized the coding. >>> >>> S4 seems to be "overkill" for such a simple system. >>> >>> Can anyone give me some motivation for persisting with S4? >>> >>> Or indeed reassure me that S3 is a good design decision? >>> >> Does performance matter?: S4 dispatch is many times slower than S3 >> dispatch for such functions. (It is several times slower in general, but >> the difference is particularly marked for primitives.) >> > Well, the question is whether performance of _method dispatch_ matters, > which it tends not to in many cases. And it would be good to have some > data to clarify "many times slower". Generally, looking up inherited > methods is likely to take a while, but only the first time does R code > need to work out the inheritance. On repeated calls with the same > signature, dispatch should be basically a lookup in an environment. > > [[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] S3 vs S4 for a simple package
> Would you like existing functions such as mean, range, sum, > colSums, dim, apply, length, and many more to operate on the array of > numbers? If so use an S3 class. The above is misleading as 'setClass("NewArray", [additional structures go in here], contains="array")' will give the NewArray all the functionality of "array" with mean, range, sum, colSums, dim, apply, length etc *and* [<- [[<- supported out of the box! You basically need only to write [, [[ to ensure that returns are of the NewClass! > If you would like to effectively disable such functions, to prevent > them from working on the object unless you write a method that specifies > exactly how the function should operate on the class, then either > use an S4 class, or an S3 class where the array is one component of > a list. This is very slow when used with [, [[ operators on that element > An S3 class also allows for flexibility - you can add attributes, > or list components, without breaking things. That's true, but then what is the purpose of a class but the rigid structure? > As for reassurance - I use S3 classes for almost everything, happily. > The one time I chose to use an S4 class I later regretted it. This > was for objects containing multiple imputations, where I wanted to > prevent functions like mean() from working on the original data, > without filling in imputations. The regret was because we later > realized that in some cases we wanted to add a "call" attribute or > component/slot so that update() would work. If it had been an S3 > object we could have done so, but as an S4 object we would have broken > existing objects of the class. > > Tim Hesterberg > Disclaimer - this is my personal opinion, not my employer's. > >> I am writing a package and need to decide whether to use S3 or S4. >> >> I have a single class, "multipol"; this needs methods for "[" and "[<-" >> and I also need a print (or show) method and methods for arithmetic +- >> */^. >> >> In S4, an object of class "multipol" has one slot that holds an array. >> >> Objects of class "multipol" require specific arithmetic operations; >> a,b being >> multipols means that a+b and a*b are defined in peculiar ways >> that make sense in the context of the package. I can also add and >> multiply >> by scalars (vectors of length one). >> >> My impression is that S3 is perfectly adequate for this task, although >> I've not yet finalized the coding. >> >> S4 seems to be "overkill" for such a simple system. >> >> Can anyone give me some motivation for persisting with S4? >> >> Or indeed reassure me that S3 is a good design decision? > > __ > 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] %s in filename when opening device causes crash (PR#10571)
Same on 2.7.0 Under development (unstable) (2007-12-21 r43753) using Ubuntu i686 2.6.22-14-generic: * ~: R :: R version 2.7.0 Under development (unstable) (2007-12-21 r43753) pdf> pdf("foo%s.pdf") *** caught segfault *** address 0x1, cause 'memory not mapped' Traceback: 1: .External(PDF, file, old$paper, old$family, old$encoding, old$bg, old$fg, old$width, old$height, old$pointsize, onefile, old$pagecentre, old$title, old$fonts, version[1], version[2]) 2: pdf("foo%s.pdf") Possible actions: 1: abort (with core dump, if enabled) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace Selection: 2 * ~: [EMAIL PROTECTED] wrote: > Full_Name: Richard Cotton > Version: 2.6.1 > OS: Windows XP (32bit) > Submission from: (NULL) (193.119.236.82) > > > Using %s in a filename when opening a device causes R to crash, e.g., > > pdf("foo%s.pdf") > win.metafile("foo%s.wmf") > postscript("foo%s.ps") > > ______ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] assigning NULLs to elements of a list
Dear developers: I have just came across an (unexpected to me) behaviour of lists when assigning NULLs to list elements. I understand that a NULL is a valid R object, thus assigning a NULL to a list element should yield exactly the same result as assigning any other object. So I was surprised when assigning a NULL in fact removed the element from the list. Is this an intended behaviour? If so, does anybody know where is it documented and what is a good way around? Thanks for help, Oleg Sklyar Here goes an example, the inline C-code does exactly what I would prefer R were doing, but both R examples do remove the element 'b': x = list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3]) library(inline) code=" SEXP res; PROTECT(res = Rf_duplicate(x)); SET_VECTOR_ELT(res,1,R_NilValue); UNPROTECT(1); return res; " setnull = cfunction(signature(x="list"),code) setnull(x) # $a # [1] 1 2 # $b # NULL # $c # [1] "A" "B" "C" y = x x[[2]] = NULL x # $a # [1] 1 2 # $c # [1] "A" "B" "C" x = y x$b = NULL x # $a # [1] 1 2 # $c # [1] "A" "B" "C" > sessionInfo() R version 2.6.1 (2007-11-26) i686-pc-linux-gnu locale: LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] inline_0.3.3 loaded via a namespace (and not attached): [1] rcompgen_0.1-17 -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] assigning NULLs to elements of a list
:) Well, yes, but what do you do with a named vector if you want to remove an element by name? It is not general: you cannot do that on vectors, matrices, arrays and all inherited objects anyway. Using a negative index is a standard and throughout practice of deleting elements in R. Surely one can have exceptions or extended behaviour for different classes, like list and data.frame here, but I cannot say it is really necessary to have it in order to produce a clean, easily readable and reliable code. Yes, I know now, this NULL assignment has existed there for long time and I am not about to propose its removal, but I really do not see a good reason for having it either. I would never use it in my code either. After all you do not assign NULLs to elements of a non-list vector or matrix or array. Best, Oleg Gabor Grothendieck wrote: > But what about by name? > > a <- list(a = 1, b = 2, c = 3) > > a$b <- NULL > > > On Feb 13, 2008 9:39 AM, Oleg Sklyar <[EMAIL PROTECTED]> wrote: >> Hmm, I think the pretty traditional R style does the job... >> >> a = list(1,2,3) >> a[-2] >> >> So I really do not see a good reason for doing a[2] = NULL instead of a >> = a[-2] >> >> >> Jeffrey J. Hallman wrote: >>> >From your tone, I gather you don't much like this behavior, and I can see >>> >your >>> point, as it not very intuitive that setting a list element to NULL deletes >>> any existing element at that index. But is there a better way to delete an >>> element from a list? Maybe there should be. >>> >>> Jeff >>> >>> Prof Brian Ripley <[EMAIL PROTECTED]> writes: >>>>> I have just came across an (unexpected to me) behaviour of lists when >>>>> assigning NULLs to list elements. I understand that a NULL is a valid R >>>>> object, thus assigning a NULL to a list element should yield exactly the >>>>> same result as assigning any other object. So I was surprised when >>>>> assigning a NULL in fact removed the element from the list. Is this an >>>>> intended behaviour? If so, does anybody know where is it documented and >>>>> what is a good way around? >>>> Yes, it was apparently intended: R has long done this. >>>> >>>> x <- list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3]) >>>> x[2] <- list(NULL) >>>> >>>> is what I think you are intending. >>>> >>>> See e.g. the comment in subassign.c >>>> >>>> /* If "val" is NULL, this is an element deletion */ >>>> /* if there is a match to "nlist" otherwise "x" */ >>>> /* is unchanged. The attributes need adjustment. */ >> -- >> Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 >> >> __ >> >> R-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] assigning NULLs to elements of a list
Hmm, I think the pretty traditional R style does the job... a = list(1,2,3) a[-2] So I really do not see a good reason for doing a[2] = NULL instead of a = a[-2] Jeffrey J. Hallman wrote: >>From your tone, I gather you don't much like this behavior, and I can see your > point, as it not very intuitive that setting a list element to NULL deletes > any existing element at that index. But is there a better way to delete an > element from a list? Maybe there should be. > > Jeff > > Prof Brian Ripley <[EMAIL PROTECTED]> writes: >>> I have just came across an (unexpected to me) behaviour of lists when >>> assigning NULLs to list elements. I understand that a NULL is a valid R >>> object, thus assigning a NULL to a list element should yield exactly the >>> same result as assigning any other object. So I was surprised when >>> assigning a NULL in fact removed the element from the list. Is this an >>> intended behaviour? If so, does anybody know where is it documented and >>> what is a good way around? >> Yes, it was apparently intended: R has long done this. >> >> x <- list(a=c(1L,2L), b=matrix(runif(4),2,2), c=LETTERS[1:3]) >> x[2] <- list(NULL) >> >> is what I think you are intending. >> >> See e.g. the comment in subassign.c >> >> /* If "val" is NULL, this is an element deletion */ >> /* if there is a match to "nlist" otherwise "x" */ >> /* is unchanged. The attributes need adjustment. */ > -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4 : declaration of inheritance
setClass("myClass", representation(x="numeric"), contains="fartherClass") seems more logical to me in sense of syntax (pay attention to setClass instead of setMethod and contains instead of containt), but both work apparently. Oleg [EMAIL PROTECTED] wrote: > Hi the list, > > There is two way to declare a new object with inheritence : one can do > > setMethod("myClass", > representation("fatherClass",x="numeric") > > > or > > setMethod("myClass", > representation(x="numeric"), > containt="fatherClass" > > > Is there any difference ? What is the usal way ? > > Christophe > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Standard method for S4 object
Hi Tim Tim Hesterberg wrote: > It depends on what the object is to be used for. > > If you want users to be able to operate with the object as if it > were a normal vector, to do things like mean(x), cos(x), etc. > then the list would be very long indeed; for example, there are > 225 methods for the S4 'bdVector' class (in S-PLUS), plus additional > methods defined for inheriting classes. This somehow undermines the whole idea of inheritance. If you do not inherit, then you are just implementing a class that mimics another one from scratch. However, the question then is not about standard methods any more, it's about the methods of the class that you mimic. As an example, in my EBImage (image analysis, Bioconductor) the class "Image" is derived from "array" using contains="array", thus providing all the functionality of arrays on top of that of images without extra code. What I needed in this case was to redefine the [ operators and "Arith" ones as I wanted them to return objects of class "Image" instead of "array"; the [<- are actually fine. Print or plot work exactly the same as in arrays printing additional slots where appropriate if not redefined, as well as most of the ones you listed above like mean, cos, hist etc, just as the following example shows (the Math group NOT redefined) > library(EBImage) > x = Image(0.5,c(10,10)) > cos(x) 'Image' colorMode() : Grayscale storage class : numeric 3D array, writable images in range [0..1] dim() : 10x10 ... Surprisingly, there are things that didn't work out of the box, like for example median: I had to redefine it to work on the .Data slot directly, otherwise it took ages, but that was about the only problem. > In cases like this you might prefer using an S3 class, using > attributes rather than slots for auxiliary information, so that > you don't need to write so many methods. The reasoning here is not really clear. Could you please explain why is this better? Best, Oleg > > Tim Hesterberg > >> I am defining a new class. Shortly, I will submit a package with it. >> Before, I would like to know if there is a kind of "non official list" >> of what method a new S4 object should have. >> More precisely, personally, I use 'print', 'summary' and 'plot' a lot. >> So for my new class, I define these 3 methods and of course, a get and a >> set for each slot. What else? Is there some other methods that a R user >> can reasonably expect? Some "minimum basic tools"... >> >> Thanks >> >> Christophe > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Standard method for S4 object
Hi Tim, well, I am not arguing that there are situation when one needs to rewrite everything from scratch. However it is always worth at least considering inheritance if there is a candidate to inherit from. It saves a lot of work. Anyway, your examples of S3 class usage are obviously valid in sense that they are indeed S3 methods providing desired functionality. However, I still do not see WHY using attributes with S3 is better than slots and S4 for structures like those inherited from 'array' or similar. S3 gives more freedom in assigning new attributes, but this freedom also means that one has little control over the structure of an object making it, for example, more difficult to use with C/C++ code. Are there any specific benefits in not using S4 and slots (apart from some known performance issues)? Best, Oleg Tim Hesterberg wrote: >> Tim Hesterberg wrote: >>> It depends on what the object is to be used for. >>> >>> If you want users to be able to operate with the object as if it >>> were a normal vector, to do things like mean(x), cos(x), etc. >>> then the list would be very long indeed; for example, there are >>> 225 methods for the S4 'bdVector' class (in S-PLUS), plus additional >>> methods defined for inheriting classes. >> This somehow undermines the whole idea of inheritance. If you do not >> inherit, then you are just implementing a class that mimics another one >>from scratch. However, the question then is not about standard methods >> any more, it's about the methods of the class that you mimic. > > My experience with S4 classes is primarily with classes that had > to be implemented from scratch, there was nothing one could inherit > from - bdFrame and bdVector in library(bigdata), miVariable in > library(missing) (sorry, these are S-PLUS only). > > Actually, for miVariable we considered S3 class + attributes, but > in this case we decided that we did NOT want operations like mean(x) > to work without going through a method specifically for the class. > >> ... example of "Image" class omitted here >> >>> In cases like this you might prefer using an S3 class, using >>> attributes rather than slots for auxiliary information, so that >>> you don't need to write so many methods. >> The reasoning here is not really clear. Could you please explain why is >> this better? > > Three examples. First is "bs": >> library(splines) >> bsx <- bs(1:99, knots = 10 * 2:6) >> showStructure(bsx) > numeric[99,8] S3 class: bs basis > attributes: dimnames > °ree scalar class: integer > &knotsnumeric[ length 5] class: numeric > &Boundary.knots numeric[ length 2] class: integer > &interceptlogical[ length 1] class: logical > > (I plan to add showStructure to library(splus2R) shortly.) > > This is an S3 class, a matrix plus some additional attributes. > Everything that works for a matrix works for this object, > without needing additional classes. > > A second example is "label": >> library(Hmisc) >> age <- c(21,65,43) >> label(age) <- "Age in Years" >> showStructure(age) > numeric[ length 3] S3 class: labelled > &label character[ length 1] class: character >> cos(age) > Age in Years > [1] -0.5477293 -0.5624539 0.5551133 > > Another S3 class, basically any object plus a label attribute. > There are a few methods for this class, otherwise it works > out of the box. > > The third is "lm" - a list with an S3 class. Functions that > operate on lists work fine without extra methods. And you can > add extra components without needing to define a new class > (I've done this in library(resample)). -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] error loading library
Could it be connected with the fact that each R session creates an Rtmp directory in tmp (at least on UNIX-alike) and that if started simultaneously on machines with the same time stamp they might want to use a directory with the same ? It still does not explain the behaviour much as, first, loading a package (with a shared object and external dependencies) does not seem to cause creation of any files there, and, second, I have never experienced such problems of our Linux farm although there were situations where I launched about a hundred of R session on a farm at the same time. Maybe the external library itself uses this temp space and locks it? Oleg Vadim Organovich wrote: > Hi, > > I am debugging intermittent crashes of R that seem to happen when multiple R > sessions nearly summaltaneously load same dll-based library. > > I have R and my libraries installed on a network drive (everything is > Windows). The drive is visible from a farm of servers. I have an R script, > foo.R, that just loads a dll-based library (to be precise it loads a library > that requires a dll-based library). When I start R (via Rscript --vanilla > foo.R) from all of the farm computers summaltaneously some of the sessions > often crash. It doesn't seem to be specific to the library, I was able to > reproduce this with the gbm library as well as with my own library. It feels > that the longer it takes to load the library the higher the probability of > the crash. > > foo.R : > library(vmisc) > > The most informative error message I've got so far looks like this, here > vmisc dll is the required library that dynamically loads a dll. > > Loading required package: vmiscdll > Error in file(file, "r") : unable to open connection > In addition: Warning message: > In file(file, "r") : > cannot open file 'q:/R/vogranovich/library/vmisc/R/vmisc', reason > 'Permission denied' > Error : unable to load R code in package 'vmisc' > Error: package/namespace load failed for 'vmisc' > Execution halted > Exit Code 1 - Process forcefully killed by the TestMaster > In stand alone sessions, interactive or not, everything loads fine. > > Does loading a library place some sort of a lock? > > I would appreciate any help with this matter. > > Regards, > Vadim > > [[alternative HTML version deleted]] > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] SEXP size management.
Rblah in your example is a SEXP structure. Realloc is an interface to C realloc and is not intended to resize SEXP structures -- it is used to resize user-controlled memory (which is generally not created by allocVector/INTEGER). You would expect a call like: int * val; ... Realloc(val, 20, int); Best, Oleg Charles Danko wrote: > Hi, > > Trying to decrease the size of a SEXP variable without reassigning > values individually in a loop. > > So far, I've tried using Realloc, as the follow source demonstrates: > SEXP dothis() { > SEXP Rblah; > PROTECT(Rblah = NEW_INTEGER(6)); > > int* blah = INTEGER(Rblah); > blah[0] = 1; > blah[1] = 2; > blah[2] = 3; > > Realloc(Rblah, 3, INTEGER); > > UNPROTECT(1); > return(Rblah); > } > > According to the documentation, I think that this should work, however > it returns an error on compile: "test.c:17: error: expected expression > before ')' token" (line 17 refers to the Realloc line). > > Another solution that will suit my needs is managing the variable in C > and assigning the pointer to an R type in the end. The following code > gets at this, but int* and SEXP, INTEGER are incompatible: > SEXP dothat() { > int* blah = malloc(3);// = INTEGER(Rblah); > blah[0] = 1; > blah[1] = 2; > blah[2] = 3; > > SEXP Rblah; > PROTECT(Rblah = blah); > > UNPROTECT(1); > return(Rblah); > } > > Any suggestions for someone still new to SEXP memory management? > > Thanks, > Charles > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] SEXP size management.
Charles Danko wrote: > Hi, Oleg, > > Thanks very much for your answer! I was indeed confused; thinking > that Realloc was intended to be an analog to realloc (except for SEXP > variables). > > Is there a way to do either of the following: > -- resize the SEXP directly (for my purpose, I ONLY need to decrease > the size), or Not that I know. > -- assign it to to a C pointer that was allocated and used previously > in the function (and can thus be resized with realloc). Well you can get a pointer to the underlying data, basically with INTEGER, REAL etc macros, but resizing that vector is not a good idea as you do not know about underlying processes in R connected with memory management etc. I think that whether you want to increase or decrease the size, you will end up with recreating a SEXP var. Well, you can keep your data outside of SEXP's, like in STL vectors etc and then move it to SEXP at the end. > > Thanks again, > Charles > > On Wed, Mar 5, 2008 at 11:14 AM, Oleg Sklyar <[EMAIL PROTECTED]> wrote: >> Rblah in your example is a SEXP structure. Realloc is an interface to C >> realloc and is not intended to resize SEXP structures -- it is used to >> resize user-controlled memory (which is generally not created by >> allocVector/INTEGER). >> >> You would expect a call like: >> >> int * val; >> ... >> Realloc(val, 20, int); >> >> Best, >> Oleg >> >> >> >> Charles Danko wrote: >> > Hi, >> > >> > Trying to decrease the size of a SEXP variable without reassigning >> > values individually in a loop. >> > >> > So far, I've tried using Realloc, as the follow source demonstrates: >> > SEXP dothis() { >> > SEXP Rblah; >> > PROTECT(Rblah = NEW_INTEGER(6)); >> > >> > int* blah = INTEGER(Rblah); >> > blah[0] = 1; >> > blah[1] = 2; >> > blah[2] = 3; >> > >> > Realloc(Rblah, 3, INTEGER); >> > >> > UNPROTECT(1); >> > return(Rblah); >> > } >> > >> > According to the documentation, I think that this should work, however >> > it returns an error on compile: "test.c:17: error: expected expression >> > before ')' token" (line 17 refers to the Realloc line). >> > >> > Another solution that will suit my needs is managing the variable in C >> > and assigning the pointer to an R type in the end. The following code >> > gets at this, but int* and SEXP, INTEGER are incompatible: >> > SEXP dothat() { >> > int* blah = malloc(3);// = INTEGER(Rblah); >> > blah[0] = 1; >> > blah[1] = 2; >> > blah[2] = 3; >> > >> > SEXP Rblah; >> > PROTECT(Rblah = blah); >> > >> > UNPROTECT(1); >> > return(Rblah); >> > } >> > >> > Any suggestions for someone still new to SEXP memory management? >> > >> > Thanks, >> > Charles >> > >> > __ >> > R-devel@r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-devel >> >> -- >> Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 >> -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4: what to put in initialize, validity and constructor?
[EMAIL PROTECTED] wrote: Do not change initialize! As I sat, this is a toy example. In my real example, initialize does a lot of things like calculation of quality indice (b is not the scare of a, but B1, B2 and B3 are the the within matrix of A after imputation with 3 differents methods), giving names to some matrix column and so on. So I seams costfull to not use an initialize. This all can be done in one of the constructors, which is then called by the other, as in my example below. I do not see a contradiction. Define constructors: setGeneric("A", function(a,b,...) standardGeneric("A")) setMethod("A", signature(a="missing",b="missing"), function(a,b,...) A(as.numeric(1:10),...) ## calls the one below ) setMethod("A", signature(a="A",b="missing"), function(a,b,...) a ) setMethod("A", signature(a="ANY",b="ANY"), function(a,b,...) new("A",a=as.numeric(a),b=as.numeric(b),...) ) setMethod("A", signature(a="ANY",b="missing"), function(a,b,...) A(a,a,...) ## Calls the one above ) etc. In words: 1) validity should return a character in case of errors 2) default initializer usually does the job 3) define constructors as methods to allow different signatures and conversions from other classes 4) If you derive your class from numeric, rather than add slots, the performance will be much better and you will get default behaviour of numeric, i.e. setClass("A", representatiom("numeric", b="numeric") etc Dr Oleg Sklyar Technology Group Man Investments Ltd +44 (0)20 7144 3803 [EMAIL PROTECTED] -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 02 May 2008 15:41 To: r-devel@r-project.org Subject: [Rd] S4: what to put in initialize, validity and constructor? Hi the list, I have some trouble using validity, intialize and the constructor. More precisely, what should go where? Here is a toy exemple (seams long, but the code is very simple): I want to define an object with two slots a and b with the properties that b will be either empty or the scare of a. Example of valid object : a= b= a=2 b= a=3 b=9 So I define my object and the validity function : setClass( "A", representation(a="numeric",b="numeric"), validity=function(object){ cat("Validity\n") if(length([EMAIL PROTECTED])!=0){ if(length([EMAIL PROTECTED])==0){stop("Can not have empty a and non emty b")}else{} if([EMAIL PROTECTED]@b){stop("b is not the scare of a")}else{} }else{} return(TRUE) } ) It works: new("A") new("A",a=2,b=4) try(new("A",b=4)) new("A",a=2) try(new("A",a=2,b=3)) Then I define the initialize function. When b is givent but not a, the initialise function set a to sqrt(b). setMethod( "initialize", "A", function(.Object,a,b){ if(missing(a)&!missing(b)){ [EMAIL PROTECTED] <- b [EMAIL PROTECTED] <- sqrt(b) }else{} if(!missing(a)&missing(b)){ [EMAIL PROTECTED] <- a }else{} if(!missing(a)&!missing(b)){ [EMAIL PROTECTED] <- a [EMAIL PROTECTED] <- b }else{} validObject(.Object) return(.Object) } ) It is fine: new("A") new("A",a=2,b=4) new("A",b=9) new("A",a=2) try(new("A",a=2,b=3)) Then I want to set the constructor A <- function(a,b){ return(new("A",a,b)) } But this does not work: A() A(a=2,b=4) A(b=9) A(a=2) The following does not work either: A <- function(a=numeric(),b=numeric()){ return(new("A",a,b)) } A() A(a=2,b=4) A(b=9) A(a=2) So is there a way to define the constructor A without dealing again with all the missing&missing staff like in initialize? Christophe __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel ** The contents of this email are for the named addressee(s) only. It contains information which may be confidential and privileged. If you are not the intended recipient, please notify the sender immediately, destroy this email and any attachments and do not otherwise disclose or use them. Email transmission is not a secure method of communication and Man Investments cannot accept responsibility for the completeness or accuracy of this email or any attachments. Whilst Man Investments makes every effort to keep its network free from viruses, it does not accept responsibility for any computer virus which might be transferred by way of this e
[Rd] texi2dvi instead of pdflatex in R2.7.0? and related bioc 2.2 builds
Dear lists: I am somewhat puzzled by the use of texi2dvi/pdflatex in R2.7.0. When building EBImage (bioconductor) with 2.7.0 I get the same error both in Windows and Linux, which reads the following: ** building package indices ... * DONE (EBImage) * creating vignettes ... ERROR /usr/bin/texi2dvi: pdflatex exited with bad status, quitting. Error in texi2dvi(file = bft, pdf = TRUE, clean = FALSE, quiet = quiet) : running 'texi2dvi' on 'AnalysisWithEBImage.tex' failed Calls: buildVignettes -> texi2dvi Execution halted The tex file includes links to png graphics and this might be the reason. Running pdflatex on the generated tex file directly produces a nice pdf in both Linux and Windows. And this seems have been the default in previous version of R. Now the puzzles are: 1) the bioc build system also uses 2.7.0 and builds vignettes just fine on both Windows and Linux: * DONE (EBImage) * creating vignettes ... OK * removing junk files * checking for LF line-endings in source and make files * checking for empty or unneeded directories * building 'EBImage_2.4.0.tar.gz' 2) have the tex settings been changed from 2.6.0 into 2.7.0 as changes to vignette have not been significant and the compilation goes through fine with 2.6.0. Or I am missing something and doing something wrong? Best, Oleg -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] help with segmentation fault
Katie, 99% it's a bug in your C code caused by accessing memory (probably writing to) which was not allocated or freed already or out of boundary. The fact that it worked fine on Windows is likely to be a pure coincidence of different compiler/optimisation settings. However, the main problem is that segfault is such a generic error that without looking at the code one will not be able to tell you anything. Debug it, get ddd with gdb and go step-wise or introduce lots of pritouts to nail down the location of the problem. Oleg Kyeongmi Cheon wrote: Hello all, I'm trying to have C called by R. I wrote C codes that worked perfectly fine with R-2.6.0 in windows system ( using R tools). I had to change to SuSE Linux (this system has 3.2 GHz Intel Xeon processors and 4 GB of RAM), the C codes were compiled okay but when it was called to R-2.6.2, I got error mesage : ***caught segfault*** address 0x1df5000, cause 'memory not mapped' segmentation fault According to C references, this kind of error occurs when you get something wrong with dereferencing pointers or go out of edge of arrays. But the very same codes were fine in windows and I did not find any related errors in my codes. How could this happen and how can I fix it? Does this have to do with different systems (linux vs windows) or C/R program? Thank you in advance. -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4 generic for lowess
One can redefine range and similar ones (c, cbind, rbind etc) as S4 methods: :: R version 2.8.0 Under development (unstable) (2008-04-22 r45454) > setGeneric("range") [1] "range" > setClass("MyClass", representation("numeric",comment="character")) [1] "MyClass" > setMethod("range", "MyClass", function(x, ..., na.rm = FALSE) { + print([EMAIL PROTECTED]) + range([EMAIL PROTECTED]) + }) [1] "range" > a = new("MyClass", runif(5,1,25), comment="Hello world") > range(a) [1] "Hello world" [1] 4.953273 21.792185 This is a trivial example, but handling of ... in the function body is also possible thus making it less trivial in general. Best, Oleg Iago Mosqueira wrote: Hi, Henrik Bengtsson wrote: A quick comment on design: On Tue, May 20, 2008 at 1:41 AM, Iago Mosqueira <[EMAIL PROTECTED]> wrote: Hi, A lowess method is defined in our package for one of our S4 classes. To explicitely define the generic this is being used if (!isGeneric("lowess")) setGeneric("lowess", useAsDefault = lowess) This works fine for many other methods inherited from various R packages. In this case a warning is issued in R CMD check: * checking R code for possible problems ... NOTE lowess: no visible binding for global variable 'xy' lowess: no visible binding for global variable 'o' which I assume originates from the formals of lowess lowess(x, y = NULL, f = 2/3, iter = 3, delta = 0.01 * diff(range(xy$x[o]))) where 'xy' and 'o' are present in the argument list but are really defined inside the function itself. To my opinion, this could have been implemented as (or with some other default value on 'delta'): lowess <- function(x, y=NULL, f=2/3, iter=3, delta=NULL) { [...] if (is.null(delta)) delta <- 0.01 * diff(range(xy$x[o]))); [...] } and then document in the help pages what 'delta' defaults to. My $.02 /Henrik That would be extremely useful for our code. I have some other examples of functions or S3 methods that could benefit from changes in default arguments to make them more S4 friendly. range, for example, cannot be overload as its generic needs to be based on function (..., na.rm = FALSE) .Primitive("range" Is there interest in the R Core Team in identifying these? Regards, Iago I suppose I can safely ignore this warning, and the new methods seems to work fine, but I was curious to know if there is a better way to deal with this. Many thanks, Iago -- Iago Mosqueira Cefas Systems Modelling Pakefield Rd. Lowestoft NR33 0HT U.K. +44 (0)1502 558003 ______ 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 -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Rmpi segfault after install on Ubuntu Hardy Heron
simply cd to $R_HOME/library/Rmpi/libs and do on the command line ldd -r Rmpi.so this will display you a list of dependencies of Rmpi.so and you can see if you find there the libraries that you expected to link against, or there will be some libraries from unexpected paths or missing ones Mark Kimpel wrote: Dirk, Configure concluded without errors. I am not running this on the HPC cluster with icc, rather a separate local box used for testing and learning. Its just an 64-bit Ubuntu 8.04 install on a 4-core machine. LAM is not installed (I checked to make sure). So, that leaves me with you last two options. Before I make my first foray into using the R-debugger, I'd like to try you suggestion to "Check ldd on Rmpi.so" but don't have a clue how to do so. Could you instruct me? Thanks to all for the helpful comments. Mark On Wed, Jun 11, 2008 at 8:22 AM, Dirk Eddelbuettel <[EMAIL PROTECTED]> wrote: On 11 June 2008 at 00:46, Mark Kimpel wrote: | I just installed Rmpi on my 64-bit Ubuntu Hardy Heron OS and using the | following without errors: | "R CMD INSTALL Rmpi_0.5-5.tar.gz --configure-args=--with-mpi=/usr/lib64/openmpi" And it concluded without warnings or errors? Configure found all the right files? As Paul suggested, make sure you're not getting it mixed with exisiting LAM headers or MPICH2 headers or, say, that you're getting your gcc and icc experiemenst mixed up or ... ? I control this by locally re-building the Debian packages for Open MPI. That way I only keep one -dev package for MPI and can ensure that I do not get mixups with other MPI installation. But then I do buy into the Package Management mantra, but not everybody does... | Immediately at library(Rmpi) I get the segfault displayed in my | complete output below. My first thought is that perhaps I used the | wrong library for openmpi, but with my 64 bit install it seemed like a | logical choice and the install went without a hitch. Check ldd on Rmpi.so to ensure that you linked against what you thought you should link against. And if everything else fails, debug the compiled code and run R under the debuggger. See the 'R Extensions' manual. | Two other general comments: | 1. Am I addressing this to the correct list or should I use R-help? As | I read the posting guide, I'm not sure. | 2. Has anyone considered an R-SIG-HPC list? Anyone besides me interested? I'd join. Dirk -- Three out of two people have difficulties with fractions. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Dates in C api
Dates are number of days since 1970-01-01 that is it. to get data of Date in C: library(inline) code = 'Rprintf("%f", REAL(x)[0]); return R_NilValue;' f = cfunction(signature(x="double"), body=code) na = f(Sys.date()) to create an object of class Date: --- in C --- SEXP res = allocVector(REALSXP, n); return res; // one can assign class here, but one can also do it on return to R --- in R --- res = .Call("myDateFun", n) class(res) = "Date" Best, Oleg Lee, Philip (IT) wrote: r-devel, I've been trying to write a C plugin for R, and I'm struggling to understand how dates are represented. I can create a date in R: mydate<-as.Date(1, origin="1900-01-01") mydate [1] "1900-01-02" When I pass my date to a plugin, though, it's type is that of a real. There doesn't seem to be a date type in Rinternals.h, is there a way to recognize that a value is a date rather than a real? Equally, does anyone know if it's possible to create date values in the C api? Thanks, Phil Lee Philip Lee Morgan Stanley | Technology 750 Seventh Avenue, 12th Floor | New York, NY 10019 [EMAIL PROTECTED] NOTICE: If received in error, please destroy and notify sender. Sender does not intend to waive confidentiality or privilege. Use of this email is prohibited when received in error. ______ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Dr Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +44-1223-494466 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] particulars of importing/loading libraries
I will give it a try with .onLoad, but I am not sure why this should be required. I do not think this is the solution for the problem as at the times when there were no namespaces, adding a library in Depends of the Description file was sufficient. As for the second point I am aware of that, but this just highlights the problem: pack1 was not made available to the global namespace and therefore that construction was required. Best, Oleg Paul Gilbert wrote: Maybe I'm missing something (it wouldn't be the first time), but I think your problem is that pack2 needs a function .onLoad <- function(library, section) {require("pack1")} since you actually want the functions from pack1 available, and not just its namespace. ( And you will need the "pack1::" in pack1::testPosixVal only if testPosixVal is not exported from pack1. ) Paul Sklyar, Oleg (London) wrote: I was thinking of this, but this is going to be a pain if a package imports 5 packs, is being imported by another one, which itself is imported by yet another one and the only one one would like to load explicitly is the last down the line. If I do not find a better solution this is what I probably will have to do, reexport everything. Dr Oleg Sklyar Research Technologist AHL / Man Investments Ltd +44 (0)20 7144 3107 oskl...@maninvestments.com -Original Message- From: Martin Morgan [mailto:mtmor...@fhcrc.org] Sent: 13 January 2009 16:31 To: Sklyar, Oleg (London) Cc: r-devel@r-project.org Subject: Re: [Rd] particulars of importing/loading libraries Hi Oleg -- "Sklyar, Oleg (London)" writes: Dear List: Sorry for posting maybe a trivial question, but I have a basic understanding problem. If I have say pack1 and pack2, two R packages, and pack2 depends on and imports pack1 fully (as in the code below), is there a way to make all the functionality of pack1 available for the global and other environments (not only for the functions called from withing pack2) by loading pack2 only? I thought if pack2 depends on and imports pack1 and essentially reexports everything, one should get the full functionality simply by loading pack2. This does not seem to be the case or I am missing something trivial in my NAMESPACE/DESCRIPTION files? I think that exportPattern does a simple ls() on the environment of the package name space. The imported symbols are not defined in that environment, but (I think) in a variable .__NAMESPACE__. and so are not discovered. Arguably, exportPattern (and friends) should be smarter. Pragmatically, you need to re-export imported symbols explicitly. I haven't worked this through entirely, and could be wrong... Martin If this is documented in Writing R Extensions, I would be thankful for a page number and maybe a quick fix in my example below as so far I have not been able to find a clear explanation. The problem can be illustrated by the following simple example (this is a simple code for 2 packages, pack1 and pack2; plus an example). Thank you for your replies. Dr Oleg Sklyar Research Technologist AHL / Man Investments Ltd +44 (0)20 7144 3107 oskl...@maninvestments.com --- pack1: DESCRIPTION -- Package: pack1 Version: 0.0.1 Date: 12 Jan 2009 Title: pack1 to test S3/S4 methods compatibility Author: Oleg Sklyar Depends: R (>= 2.7.1), methods Maintainer: Oleg Sklyar Description: pack1 LazyLoad: yes License: Proprietary URL: http://www.maninvestments.com LazyLoad: no --- pack1: NAMESPACE -- import(methods) exportPattern("^[^\\.]") exportClasses(posixTime) exportMethods(as.POSIXct) --- pack1: posix.R -- setClass("posixTime", "numeric") setGeneric("as.POSIXct") setMethod("as.POSIXct", signature(x="posixTime"), function(x, tz) { z = x...@.data attr(z,"class") = c("POSIXt", "POSIXct") attr(z,"tzone") = "UTC" z } ) testPosixVal = new("posixTime", as.numeric(Sys.time())) --- pack2: DESCRIPTION Package: pack2 Version: 0.0.1 Date: 12 Jan 2009 Title: pack2 to test S3/S4 methods compatibility Author: Oleg Sklyar Depends: R (>= 2.7.1), methods Maintainer: Oleg Sklyar Description: pack2 LazyLoad: yes License: Proprietary URL: http://www.maninvestments.com LazyLoad: no --- pack2: NAMESPACE -- import(pack1) exportPattern("^[^\\.]") --- pack2: posix.R -- testPosix = function() { z = as.POSIXct(testPosixVal) print(z) print(class(z)) z } -- test code to run from global env, showing problems --- require(pack2) ## use as.POSIXct imported into pack2 from pack1 to do the conversion in the fun testPosix() #~ [1] "2009-01-13 15:29:50 UTC" #~ [1] "POSIXt" "POSIXct" #~ [1] "2009-01-13 15:29:50 UTC" ## now try using it directly from the global env (pack1 was not explicitly loade
Re: [Rd] Interactive Graphics in R [Was: Google Summer of Code 2009]
Simon, as promised I attach a simple package that utilises gtkdatabox. It is Linux only, sorry for that: as it was hacked together in the last two hours I did not have time for Windows stuff. Under my Ubuntu I only had to install libgtkdatabox-dev from standard repos (which would pull libgtk2-dev where necessary). The package relies on gtkdatabox being found under the standard pkg-config path (i.e. custom path installs would be difficult until compiler flags are manually changed). This is for simplicity of ./configure After installing, simply run example(databox) and use your mouse for zooming-in with quite a standard left mouse click for drawing a selection box (a click is required within a selection to zoom in); right mouse click zooms out. I think it is CTRL-right or SHIFT-right to zoom out to full scale. This is a kind of functionality I would like to see. I do not mean the gtkdatabox, but the idea. With this one it is quite easy to add more plots to the window and as the user has control over callbacks it is easy to do autorescale on multiple plots if required. The limitation is the ruler of the gtkdatabox itself (no time), no NA treatment, implementation via increases pix buffer on zoom (rather than off-screen) etc. I do not know if r-devel will allow a tar.gz source through, but if anybody else is interested, please let me know and I will send the source directly. Best, Oleg databox_0.0.1.tar.gz Description: GNU Zip compressed data __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel