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:> testlistYour 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. Michael Weylandt
2012-May-03 14:02 UTC
[R] deparse(substitute(x)) on an object with S3 class
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.