s4

class in R: S3 vs S4

喜欢而已 提交于 2019-11-29 19:40:19
I want to create a class in R, should I use S3 or S4 class? I read a lot of different things about them, is there one superior to the other one? S3 can only dispatch on it's first argument, whereas S4 can dispatch on multiple arguments. If you want to be able to write methods for function foo that should do different things if given an object of class "bar" or given objects of class "bar" and "foobar" , or given objects of class "barfoo" and "foobar" , then S4 provides a far better way to handle such complexities. S3 is very simple and easy to implement, but isn't really a formal object

Rd file name conflict when extending a S4 method of some other package

久未见 提交于 2019-11-29 16:33:55
问题 Actual question How do I avoid Rd file name conflicts when a S4 generic and its method(s) are not necessarily all defined in the same package (package containing (some of) the custom method(s) depends on the package containing the generic) and using roxygenize() from package roxygen2 to generate the actual Rd files? I'm not sure if this is a roxygen2 problem or a common problem when the generic and its method(s) are scattered across packages (which IMHO in general definitely is a realistic

dputting an S4 object

不想你离开。 提交于 2019-11-29 10:13:58
How would a person dput() an S4 object? I tried this require(sp) require(splancs) plot(0, 0, xlim = c(-100, 100), ylim = c(-100, 100)) poly.d <- getpoly() #draw a pretty polygon - PRETTY! poly.d <- rbind(poly.d, poly.d[1,]) # close the polygon because of Polygons() and its kin poly.d <- SpatialPolygons(list(Polygons(list(Polygon(poly.d)), ID = 1))) poly.d dput(poly.d) Notice that if I dput() an S4 object, I cannot reconstruct it again. Your thoughts? As it currently stands, you cannot dput this object. The code of dput contains the following loop: if (isS4(x)) { cat("new(\"", class(x), "\"\n",

show source code for a function in a package in R [duplicate]

孤街浪徒 提交于 2019-11-29 07:51:02
Possible Duplicate: R: show source code of an S4 function in a package I downloaded a package ( GEOquery ) and was playing with some of the functions. One of them is called Table , which, to my understanding, is able to tabulate an S4 dataset. E.g. > summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI Length Class Mode 1 GDS S4 getAnywhere(Table) shows > getAnywhere(Table) A single object matching ‘Table’ was found It was found in the following places package:GEOquery namespace:GEOquery with value function (object) standardGeneric("Table") <environment: 0x06ad5268> attr(,"generic")

Is S4 method dispatch slow?

笑着哭i 提交于 2019-11-29 03:00:02
问题 My S4 class has a method that is called many times. I noticed that the execution time is much slower than it would be if a similar function was called independently. So I added a slot with type "function" to my class and used that function instead of the method. The example below shows two ways of doing this, and both of them run much faster than the corresponding method. Also, the example suggests that the lower speed of the method is not due to method having to retrieve data from the class,

How to access the slots of an S4 object in R

偶尔善良 提交于 2019-11-29 02:57:18
I'm working with wavelets on a program and I'm used the package wavelets to create the DWT of a time series using the function dwt . This function returns an object of class dwt , which is a S4 object with many slots: W , V , levels , filter , and so on. How can I access the W 's as a vector? Ari B. Friedman @ will let you access the slots of an S4 object. So if your object is called wave , then wave@W should get you your vector. Note that often the best way to do this is to not access the slot directly but rather through an accessor function (e.g. coefs() rather than digging out the

Adding S4 dispatch to base R S3 generic

丶灬走出姿态 提交于 2019-11-29 00:10:43
I am trying to add a spatial method to merge which needs to be S4 (since it dispatches on the types of two different objects). I have tried using an earlier solution as follows: #' Merge a SpatialPolygonsDataFrame with a data.frame #' @param SPDF A SpatialPolygonsDataFrame #' @param df A data.frame #' @param \dots Parameters to pass to merge.data.frame #' #' @export #' @docType methods #' @rdname merge-methods setGeneric("merge", function(SPDF, df, ...){ cat("generic dispatch\n") standardGeneric("merge") }) #' @rdname merge-methods #' @aliases merge,SpatialPolygonsDataFrame,data.frame-method

S4 Classes: Multiple types per slot

孤街浪徒 提交于 2019-11-28 23:18:19
Is it possible to create an S4 class, where one or more of the slots can be of multiple classes? For example. Let's say that you had a situation where data could be either a vector, or a data.frame. exampleClass <- setClass("exampleClass", representation(raw=c("data.frame","numeric","character"), anotherSlot=c("data.frame","numeric")) Or, is this the type of situation where defining a sub-class / super-class becomes necessary? PS: Searching for a useful tutorial on S4 classes produces limited results. Links to a good tutorial on S4 class creation/usage/documentation would be greatly

How to properly document S4 “[” and “[<-“ methods using roxygen?

空扰寡人 提交于 2019-11-28 19:29:35
Below I posted a mini example in which I want do write documentation for an “[“ method for a S4 class. Does someone know how to properly document a method for the generic "[" using roxygen and S4? I get a warning when checking the package after building (see below). #' An S4 class that stores a string. #' @slot a contains a string #' @export setClass("testClass", representation(a="character")) #' extract method for testClass #' #' @docType methods #' @rdname extract-methods setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"), function (x, i, j, ..., drop){ print("void function") } )

Converting spatial polygon to regular data frame without use of gpclib tools

只谈情不闲聊 提交于 2019-11-28 07:40:10
问题 I working with spatial data in R for a commercial application and would like to use ggplot2 for data visualization. If you run the Hadley's example at https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles you find that in order to run the fortify command you need to enable the use of gpclib tools using gpclibPermit() . I'm looking for an efficient way (that doesn't involve manually hacking into the S4 object) to perform the same operation that fortify does here, i.e. take a