[R] deparse(substitute(x)) on an object with S3 class

R. Michael Weylandt michael.weylandt at gmail.com
Thu May 3 16:02:49 CEST 2012


Note that print.testclass(testlist) also works as expected so it might
be there's a forced evaluation somewhere inside S3 dispatch -- I was
playing around with this the other day, having gotten myself confused
by it and stumbled across the internal logic (or at least something
that made sense to me), though I seem to have forgotten it again.
Hopefully someone else can clarify.

Michael

On Thu, May 3, 2012 at 6:15 AM, Remko Duursma <remkoduursma at gmail.com> wrote:
> Dear list,
>
>
> can someone explain to me why deparse(substitute(x)) does not seem to work
> when x is of a user-defined S3 class?
> In my actual problem, my print method is part of a package, and the method
> is registered in the NAMESPACE, if that should make a difference.
>
>> print.testclass <- function(x,...){
>  xname <- deparse(substitute(x))
>  cat("Your object name is",xname,"\n")
> }
>
> testlist <- list()
> testlist[[1]] <- 1:10
> class(testlist) <- "testclass"
>
> # This does not work as expected:
>> testlist
> Your object name is structure(list(1:10), class = "testclass")
>
> # But this does :
>> testlist2 <- unclass(testlist)
>> print.testclass(testlist2)
> Your object name is testlist2
>
>
>
> thanks,
> Remko Duursma
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/deparse-substitute-x-on-an-object-with-S3-class-tp4605592.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