[R] 'deparse(substitute'))' then 'assign' in a S4 methods

William Dunlap wdunlap at tibco.com
Sat Feb 4 18:48:03 CET 2012


Using substitute() and assign(..., envir=parent.frame()) in
fooBis will lead you into a lot of trouble.  It is nicer to
use a 'replacement function', whose  name ends with '<-',
whose last argument is named 'value', and which returns
the modified first argument.  They are usually paired with
extractor functions.

E.g.,

 > twice <- function(x, value) { 2*x } # 'extractor function'
 > `twice<-` <- function(x, value) { x <- value/2 ; x } # 'replacement function'
 > x <- 1:5
 > twice(x)
 [1]  2  4  6  8 10
 > twice(x)[2] <- 100
 > x
 [1]  1 50  3  4  5

Your example, setting the input to a fixed value, could be
 `fooBis<-` <- function(x, value) { x <- 4 ; x }
You would have to call it as an assignment: whatever is on
the right side would be ignored
 x <- 20
 fooBis(x) <- NULL
 # x should be 4 now

There should not be any problem turning these into S4 generics
in the usual way.


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cgenolin
> Sent: Saturday, February 04, 2012 2:41 AM
> To: r-help at r-project.org
> Subject: [R] 'deparse(substitute'))' then 'assign' in a S4 methods
> 
> Hi the list,
> 
> I am writing a function in which I need to affect a variable on a higher
> level. My fnction could be:
> 
> ++++++++++++++++++
> fooBis <- function(x){
>       nameObject <- deparse(substitute(x))
>       print(nameObject)
>       assign(nameObject,4,envir=parent.frame())
> }
> > fooBis(e)
> [1] "e"
> > e
> [1] 4
> -----------------
> 
> (to simplify, this fnction can affect only the number 4. But it does it in
> the parent frame).
> 
> My problem is that I use S4 programming. So the code becomes:
> 
> +++++++++++++
> setGeneric("foo",function(x){standardGeneric("foo")})
> setMethod("foo","ANY",
>     function(x){
>       nameObject <- deparse(substitute(x))
>       print(nameObject)
>       assign(nameObject,4,envir=parent.frame())
>    }
> )
> -----------
> But it does not work since the definition of foo is now:
> 
> +++++++++
> > foo
> nonstandardGenericFunction for "foo" defined from package ".GlobalEnv"
> function (x)
> {
>     standardGeneric("foo")
> }
> <environment: 0x125b7c0c>
> -----------------
> So how can I solve this problem? Is it possible to assign a value to a
> variable on the upper level using S4?
> 
> Sincerely
> Christophe
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/deparse-substitute-then-assign-in-a-S4-
> methods-tp4356748p4356748.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list