Skip to content
Prev 53435 / 63424 Next

deparse(substitute(x)) fails in implied call to an S3 print method

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.

       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