Spencer Graves
2017-May-07 19:56 UTC
[Rd] 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
Duncan Murdoch
2017-May-07 20:46 UTC
[Rd] deparse(substitute(x)) fails in implied call to an S3 print method
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 >
Spencer Graves
2017-May-07 20:52 UTC
[Rd] deparse(substitute(x)) fails in implied call to an S3 print method
On 2017-05-07 3:46 PM, Duncan Murdoch wrote:> 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.Thanks for the reply. I'm already saving match.call() and displaying that. That's adequate, but I was hoping to do better. Spencer Graves> > 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 >> >
Possibly Parallel Threads
- deparse(substitute(x)) fails in implied call to an S3 print method
- deparse(substitute(x)) on an object with S3 class
- Strange behavior of assign in a S4 method.
- months not working with local language (weekdays does)
- Bug: floating point bug in nclass.FD can cause hist() to crash