Spencer Graves
2017-Jan-09 09:53 UTC
[Rd] problem with print.generic(x)deparse(substitute(x))
Hi, All: I'm having trouble getting deparse(substitute(x)) inside print.generic to consistently I'm having trouble getting a print.something to work consistently. Consider the following toy example: # Define an object of class 'dum' k <- 1 class(k) <- 'dum' str(k) # as expected # Define print.dum print.dum <- function(x, ...) deparse(substitute(x)) print(k) # Prints "k" as expected #####**** THE FOLLOWING PRINTS NOTHING: k # Why? ### Thanks, ### Spencer Graves sessionInfo() R version 3.3.2 (2016-10-31) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: macOS Sierra 10.12.2 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 [5] datasets methods base loaded via a namespace (and not attached): [1] tools_3.3.2
peter dalgaard
2017-Jan-09 10:24 UTC
[Rd] problem with print.generic(x)deparse(substitute(x))
On 09 Jan 2017, at 10:53 , Spencer Graves <spencer.graves at prodsyse.com> wrote:> # Define an object of class 'dum' > k <- 1 > class(k) <- 'dum' > str(k) # as expected > > # Define print.dum > print.dum <- function(x, ...) > deparse(substitute(x)) > > print(k) # Prints "k" as expected > #####**** THE FOLLOWING PRINTS NOTHING: > k # Why?Because it doesn't work that way... First of all, your print.dum relies on autoprinting of its return value, it doesn't print anything itself. That's not how one usually writes print methods: You should print something and (usually) return the argument invisibly. Autoprinting calls the print method to do the actual printing and returns the object invisibly, irrespective of the return value from the print method. To wit:> k > dput(.Last.value)structure(1, class = "dum") (Trying to print the return value would invite infinite looping.) However, there's another catch: deparse(substitute(...)) relies on knowing the argument to print() before evaluation, but autoprinting does not retain that information, it just looks at the object that has been computed and passes it to the relevant print method, so you get this effect:> print.dum <- function(x, ...)+ print(deparse(substitute(x)))> k[1] "x" -pd -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Spencer Graves
2017-Jan-09 14:19 UTC
[Rd] problem with print.generic(x)deparse(substitute(x))
Hi, Peter et al.: On 2017-01-09 4:24 AM, peter dalgaard wrote:> On 09 Jan 2017, at 10:53 , Spencer Graves <spencer.graves at prodsyse.com> wrote: > >> # Define an object of class 'dum' >> k <- 1 >> class(k) <- 'dum' >> str(k) # as expected >> >> # Define print.dum >> print.dum <- function(x, ...) >> deparse(substitute(x)) >> >> print(k) # Prints "k" as expected >> #####**** THE FOLLOWING PRINTS NOTHING: >> k # Why? > > Because it doesn't work that way... > > First of all, your print.dum relies on autoprinting of its return value, it doesn't print anything itself. That's not how one usually writes print methods: You should print something and (usually) return the argument invisibly. > > Autoprinting calls the print method to do the actual printing and returns the object invisibly, irrespective of the return value from the print method. To wit: > >> k >> dput(.Last.value) > structure(1, class = "dum") > > (Trying to print the return value would invite infinite looping.) > > However, there's another catch: deparse(substitute(...)) relies on knowing the argument to print() before evaluation, but autoprinting does not retain that information, it just looks at the object that has been computed and passes it to the relevant print method, so you get this effect: > >> print.dum <- function(x, ...) > + print(deparse(substitute(x))) >> k > [1] "x"Thanks. Permit me to ask my real question: Is there a way to modify print.findFn{sos} so I can print "k" in the second line of the HTML from the following: (k <- findFn('spline', 1)) Currently, the HTML starts as follows: findFunction Results call: findFn(string = "spline", maxPages = 1) I want it to start as follows: findFn Results call: (k <- findFn(string = "spline", maxPages = 1)) For a package summary: installPackages(k); writeFindFn2xls(k) When I added "ds <- deparse(substitute(x))" to "print.findFn", I got ds = "x", not "k" from "(k <- findFn('spline', 1))" and from "k" by itself (autoprint) but "x" from "print(k)". Thanks to Peter for explaining this problem. Is there a work around? Spencer> -pd > > >
Possibly Parallel Threads
- problem with print.generic(x)deparse(substitute(x))
- problem with print.generic(x)deparse(substitute(x))
- deparse(substitute(x)) fails in implied call to an S3 print method
- print.<strorageMode>() not called when autoprinting
- print.<strorageMode>() not called when autoprinting