[Rd] problem with print.generic(x)deparse(substitute(x))

Spencer Graves spencer.graves at prodsyse.com
Mon Jan 9 15:19:49 CET 2017


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
>
>
>



More information about the R-devel mailing list