Jonathan Fritzemeier
2017-Jun-23 14:15 UTC
[Rd] setReplaceMethod creates 'object' in the userworkspace
Hi,
I recognized that the function 'setReplaceMethod' is creating a
character vector in the user workspace having the name (e.g.
"newClass")
of the class used as value. If you can sort out a mistake by myself, I
would like you to file a bug report.
BBFN,
Jonathan
setClass("newClass", representation(value="numeric"))
setMethod(f = "initialize", signature = "newClass",
definition = function(.Object){
.Object at value <- 1
return(.Object)
})
setGeneric(name = "myValue",
def = function(object) { standardGeneric("myValue") }
)
setGeneric(name = "myValue<-",
def = function(object, value) {
standardGeneric("myValue<-") }
)
setMethod("myValue", signature(object = "newClass"),
function(object) {
return(object at value)
}
)
setReplaceMethod("myValue", signature = (object =
"newClass"),
function(object, value) {
object at value <- value
return(object)
}
)
myNewObject <- new("newClass")
print(object)
> print(object)
[1] "newClass"
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.0 tools_3.4.0
Martin Maechler
2017-Jun-27 08:27 UTC
[R] [Rd] setReplaceMethod creates 'object' in the userworkspace
>>>>> Jonathan Fritzemeier <clausjonathan.fritzemeier at uni-duesseldorf.de> >>>>> on Fri, 23 Jun 2017 16:15:30 +0200 writes:> Hi, > I recognized that the function 'setReplaceMethod' is creating a > character vector in the user workspace having the name (e.g. "newClass") > of the class used as value. If you can sort out a mistake by myself, I > would like you to file a bug report. Yes, a mistake by yourself (and really not fit for R-devel, but rather for R-help to which I follow up now) > BBFN, > Jonathan> setClass("newClass", representation(value="numeric")) > > setMethod(f = "initialize", signature = "newClass", > definition = function(.Object){ > .Object at value <- 1 > return(.Object) > }) > > setGeneric(name = "myValue", > def = function(object) { standardGeneric("myValue") } > ) > setGeneric(name = "myValue<-", > def = function(object, value) { standardGeneric("myValue<-") } > ) > > setMethod("myValue", signature(object = "newClass"), > function(object) { > return(object at value) > } > )> setReplaceMethod("myValue", signature = (object = "newClass"), > function(object, value) { > object at value <- value > return(object) > } > )Q: what do you think happens with the above [last setReplaceMethod() call] ? A: it creates an R object named 'object' in the globalenv (or the package if this would go into a package) If just replace '(object = "newClass")' by '"newClass"' things should be fine. {{ Removing all the completely redundant return(.), i.e. return implicitly rather than via an extra function call would also make the code "cleaner" and more R-like }} Best, Martin> myNewObject <- new("newClass") > print(object) > > > > print(object) > [1] "newClass" >