[R] [Rd] setReplaceMethod creates 'object' in the userworkspace

Martin Maechler maechler at stat.math.ethz.ch
Tue Jun 27 10:27:52 CEST 2017


>>>>> 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"
>



More information about the R-help mailing list