r-s3

S3 method help (roxygen2)

喜你入骨 提交于 2019-12-10 02:52:25
问题 I am trying to use a S3 method in a package and thought I understood how to set it up after asking a question here: S3 method consistency warning when building R package with Roxygen But now I get results I don't expect. If I run the code below directly in R it gives me the expected results but if I compile it into a package I don't get the correct results (notice the word the gets counted twice when it should take only unique words from vector a ). I'm not sure what I'm setting up

How to use S3 methods from another package which uses export rather than S3method in its namespace without using Depends or library()

风流意气都作罢 提交于 2019-12-10 02:03:04
问题 I'm working on an R package at present and trying to follow the best practice guidelines provided by Hadley Wickham at http://r-pkgs.had.co.nz. As part of this, I'm aiming to have all of the package dependencies within the Imports section of the DESCRIPTION file rather than the Depends since I agree with the philosophy of not unnecessarily altering the global environment (something that many CRAN and Bioconductor packages don't seem to follow). I want to use functions within the Bioconductor

Modify S3 object without returning it?

别来无恙 提交于 2019-12-06 10:26:28
问题 I'm new to object-oriented programming in R and struggle with how to properly write a function that modifies an object. This example works: store1 <- list( apples=3, pears=4, fruits=7 ) class(store1) <- "fruitstore" print.fruitstore <- function(x) { paste(x$apples, "apples and", x$pears, "pears", sep=" ") } print(store1) addApples <- function(x, i) { x$apples <- x$apples + i x$fruits <- x$apples + x$pears return(x) } store1 <- addApples(store1, 5) print(store1) But I suppose there should be a

S3 method help (roxygen2)

北慕城南 提交于 2019-12-05 01:22:06
I am trying to use a S3 method in a package and thought I understood how to set it up after asking a question here: S3 method consistency warning when building R package with Roxygen But now I get results I don't expect. If I run the code below directly in R it gives me the expected results but if I compile it into a package I don't get the correct results (notice the word the gets counted twice when it should take only unique words from vector a ). I'm not sure what I'm setting up incorrectly. The .R file: #' Find Common Words Between Groups #' #' Find common words between grouping variables

Getting the object name for S3 print method failing

蹲街弑〆低调 提交于 2019-12-04 17:02:36
问题 Define an object of S3 class "bar" and a print method: foo=list(1) class(foo) <- c("bar") print.bar <- function(x,...){ cat("print.bar says this was ",deparse(substitute(x)),"\n") } Now print(foo) does this: > print(foo) print.bar says this was foo Great, but auto-printing fails: > foo print.bar says this was structure(list(1), class = "bar") I'm guessing this is something to do with the way the line is evaluated as a top-level expression. Had a quick search on R-devel to no avail. Anyone

Safely creating S3 Generics in R

蓝咒 提交于 2019-12-04 15:03:40
问题 Henrik Bengtsson has provided the internet with a nice way of creating S3 generics in R without having to bother whether they were already created before... in 2002. What his function setGenericsS3 does, is basically: check whether the name is fine check whether there is a function with that name if so, check whether it is a generic in case it's not, rename it as .default and create a generic if not, just create the generic. This code proved very useful to automatically create generics when

emulating multiple dispatch using S3 for “+” method - possible?

喜夏-厌秋 提交于 2019-12-04 11:50:10
问题 I have two classes ( a and b ) and I want to define the + method for them. I need different methods for the four possible combinations of the two classes, i.e.: a + a method 1 a + b method 2 b + a method 3 b + b method 4 I know I could use S4 for multiple dispatch, but I want to know if there is a way to emulate this behaviour using S3. My approach was the following: a <- "b" class(a) <- "a" b <- "e" class(b) <- "b" Ops.a <- function(e1, e2){ if (class(e1) == "a" & class(e2) == "a") print("a

Safely creating S3 Generics in R

别等时光非礼了梦想. 提交于 2019-12-03 10:24:55
Henrik Bengtsson has provided the internet with a nice way of creating S3 generics in R without having to bother whether they were already created before... in 2002. What his function setGenericsS3 does, is basically: check whether the name is fine check whether there is a function with that name if so, check whether it is a generic in case it's not, rename it as .default and create a generic if not, just create the generic. This code proved very useful to automatically create generics when there was none available in your own packages. As we moved quite past this R era, I was wondering what

emulating multiple dispatch using S3 for “+” method - possible?

ⅰ亾dé卋堺 提交于 2019-12-03 08:10:25
I have two classes ( a and b ) and I want to define the + method for them. I need different methods for the four possible combinations of the two classes, i.e.: a + a method 1 a + b method 2 b + a method 3 b + b method 4 I know I could use S4 for multiple dispatch, but I want to know if there is a way to emulate this behaviour using S3. My approach was the following: a <- "b" class(a) <- "a" b <- "e" class(b) <- "b" Ops.a <- function(e1, e2){ if (class(e1) == "a" & class(e2) == "a") print("a & a") if (class(e1) == "a" & class(e2) == "b") print("a & b") if (class(e1) == "b" & class(e2) == "a")

function to return all S3 methods applicable to an object

六月ゝ 毕业季﹏ 提交于 2019-12-01 05:15:06
Has anyone put together/found a good method for listing all the S3 methods available for a given object ? The built-in methods() function will give all available methods for a specified class, or for a specified generic function, but not for an object. The example I have in mind is a glm object, which is of (minor?) class "glm" but also inherits from "lm" g <- glm(y~x,data=data.frame(x=1:10,y=1:10)) class(g) ## [1] "glm" "lm" There are 35 methods for class "lm" and 22 for "glm". I'm interested in an answer that combines the results of lapply(class(g),function(x) methods(class=x)) in a sensible