[R] Strange display of saved functions

Duncan Murdoch murdoch.duncan at gmail.com
Mon Jan 30 21:08:03 CET 2017


On 30/01/2017 2:17 PM, William Dunlap via R-help wrote:
> This must be a pretty old RData file (pre-2.14.0).  news() says:
>
> Changes in version 2.14.0:
>
> NEW FEATURES
>
>     o   The "source" attribute on functions created with keep.source=TRUE
>         has been replaced with a "srcref" attribute.  The "srcref"
>         attribute references an in-memory copy of the source file using the
>         "srcfilecopy" class or the new "srcfilealias" class.
>
>         *NB:* This means that functions sourced with keep.source = TRUE and
>         saved (e.g., by save() or readRDS()) in earlier versions of R will
>         no longer show the original sources (including comments).

Yes, indeed.  Mik, if you do want the original formatting and comments, 
something like

cat(attr(payments, "source"), sep = "\n")

should display it.

Duncan Murdoch


>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> On Mon, Jan 30, 2017 at 9:29 AM, Mik Bickis <m.bickis at sasktel.net> wrote:
>> Hello:
>>
>> I recently upgraded to R version 3.3.2 (2016-10-31)
>>
>> Now I find that functions that I load from an older .RData file give a strange display:
>>
>> ---------------------------------------------------------------------------------------------------------------------------
>>> payments
>> function (a, n, tol = 1e-10)
>> {
>>     p <- a/(1 - (1 + a)^(-n))
>>     p[a < tol] <- 1/n + (n + 1) * a[a < tol]/(2 * n)
>>     p
>> }
>> attr(,"source")
>> [1] "function(a,n,tol=1E-10){# required proportion of principal required to "
>> [2] "\t# pay off loan in n installments at interest rate a"
>> [3] "\tp<-a/(1-(1+a)^(-n))"
>> [4] "\t# linear approximation for small rates"
>> [5] "\tp[a<tol]<-1/n+(n+1)*a[a<tol]/(2*n)"
>> [6] "\tp}"
>> ------------------------------------------------------------------------------------------------------------------------------
>>
>> In addition to the usual function definition it also prints the function contents as an attribute "source", including strange escape characters at the beginning of the lines.  Note that the comment only appears in this latter display.
>> This same display happens if I use the "edit" function.
>>
>> However, if I copy and paste the definition into a new function, then it displays as expected:
>>
>> ----------------------------------------------------------------------------------------------------------------------------
>>> puments<-function (a, n, tol = 1e-10)
>> + {
>> +     p <- a/(1 - (1 + a)^(-n))
>> +     p[a < tol] <- 1/n + (n + 1) * a[a < tol]/(2 * n)
>> +     p
>> + }
>>>
>>> puments
>> function (a, n, tol = 1e-10)
>> {
>>     p <- a/(1 - (1 + a)^(-n))
>>     p[a < tol] <- 1/n + (n + 1) * a[a < tol]/(2 * n)
>>     p
>> }
>> --------------------------------------------------------------------------------------------------------------------------------
>>
>> And I can successfully insert a comment right in the definition, as one would like
>>
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>> puments
>> function (a, n, tol = 1e-10)
>> {# required proportion of principal required to
>> # pay off loan in n installments at interest rate a
>>     p <- a/(1 - (1 + a)^(-n))
>>     p[a < tol] <- 1/n + (n + 1) * a[a < tol]/(2 * n)
>>     p
>> }
>> -------------------------------------------------------------------------------------------------------------------------------------------
>>
>> What is happening to the displays of my function definitions?   I have never witnessed such behaviour before.   Is there some option I have to set (globally) to suppress this redundancy?
>>
>> Mik Bickis
>> Department of Mathematics and Statistics
>> University of Saskatchewan
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list