[Rd] deparse(substitute(x)) fails in implied call to an S3 print method
Duncan Murdoch
murdoch.duncan at gmail.com
Sun May 7 22:46:12 CEST 2017
On 07/05/2017 3:56 PM, Spencer Graves wrote:
> In an implied call to an S3 print method, deparse(substitute(x)) returns
> "x", regardless of the name of object in .GlobalEnv, as indicated in the
> following:
>
>
> > Xnamed <- 1
> > class(Xnamed) <- 'name.x'
> > print.name.x <- function(x, ...){
> + namex <- deparse(substitute(x))
> + cat('How can I get the name of x in .GlobalEnv?\n',
> + 'deparse(substitute(x)) gives only ', namex, '\n')
> + }
> > Xnamed
> How can I get the name of x in .GlobalEnv?
> deparse(substitute(x)) gives only x
>
>
> My real application is print.findFn{sos}, which displays in a web
> browser. If the results of multiple searches are "printed", the name of
> the object in .GlobalEnv could help the user keep track of what was done.
I don't think there's any way around this. Auto-printing of Xnamed
isn't equivalent to print(Xnamed); it doesn't happen until after Xnamed
is evaluated. It assigns the result to a local variable called x, and
calls print(x). (All of this is done in C code, and at that point I
don't think the original expression "Xnamed" is available any more.)
For your application, you may have to allow users to attach names to the
object. I don't know how you're creating those things, but part of the
work could save a name as an attribute "SOSname", and then the print
method would use that.
Duncan Murdoch
>
> Thanks,
> Spencer Graves
>
>
> > sessionInfo()
> R version 3.4.0 (2017-04-21)
> Platform: x86_64-apple-darwin15.6.0 (64-bit)
> Running under: macOS Sierra 10.12.4
>
> Matrix products: default
> BLAS:
> /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
> LAPACK:
> /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list