[R] options()$width ignored by print.formula
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Nov 7 07:48:46 CET 2012
On 06/11/2012 17:54, Sarah Goslee wrote:
> Thanks, Prof. Ripley.
>
> Using deparse(b$formula, width.cutoff = options()$width) works as I'd
> expected (full example below). So I can use that to write a custom
> summary.gam(), though that still seems like a lot of work to get
> summary(b) to follow the usual R output conventions.
I think you are misreading the documentation. ?options says
width: controls the maximum number of columns on a line used in
printing vectors, matrices and arrays, and when filling by
cat.
Columns are normally the same as characters except in CJK
languages.
Nothing abut language objects there (nor the output from cat(),
frequently used in print() methods without a 'fill' argument).
Contrast that with deparsing expressions, when 'width.cutoff' is a lower
rather than upper bound, and is in bytes (not characters nor columns).
As I said, I can see a case from allowing the default cutoff used by
print() to be under user control, but it would need to be a different
option from 'width' since it is not even in the same units.
>
> Sarah
>
> library(mgcv)
> # start with example from ?gam
> set.seed(2) ## simulate some data...
> dat <- gamSim(1,n=400,dist="normal",scale=2)
>
> colnames(dat)[1:5] <- c("reallylongnamey", "reallylongnamex0",
> "reallylongnamex1", "reallylongnamex2", "reallylongnamex3")
> b <- gam(reallylongnamey~s(reallylongnamex0)+s(reallylongnamex1)+s(reallylongnamex2)+s(reallylongnamex3),data=dat)
>
> options(width=20)
> deparse(b$formula, width.cutoff = options()$width) # not wrapped to 20
> characters
>
> options(width=150)
> deparse(b$formula, width.cutoff = options()$width) # the formula is
> wrapped even though it fits in the specified width
>
>
> On Tue, Nov 6, 2012 at 12:37 PM, Prof Brian Ripley
> <ripley at stats.ox.ac.uk> wrote:
>> On 06/11/2012 17:01, Sarah Goslee wrote:
>>>
>>> Hi all,
>>>
>>> I'm working with summary.gam() and noticed that the options()$width
>>> argument is ignored by some components of that function, in particular
>>> the formula, which is printed at an arbitrary length regardless of the
>>> desired width.
>>>
>>> I've tracked the problem back to print.formula(), so at a lower level
>>> than summary.gam(). Is there a way around this that I'm missing?
>>
>>
>> Use deparse(): print() on a language type uses (at C level) deparse with the
>> default width.cutoff of 60. E.g. (subject to vagaries of mailers)
>>
>>> form <-
>>> reallylongnamey~reallylongnamex0+reallylongnamex1+reallylongnamex2+reallylongnamex3
>>> form
>> reallylongnamey ~ reallylongnamex0 + reallylongnamex1 + reallylongnamex2 +
>> reallylongnamex3
>>> deparse(form, 150)
>> [1] "reallylongnamey ~ reallylongnamex0 + reallylongnamex1 +
>> reallylongnamex2 + reallylongnamex3"
>>
>> I am not sure this is something we would want to make depend on
>> getOption("width"), but it might merit a separate option.
>>
>>>
>>> library(mgcv)
>>> # start with example from ?gam
>>> set.seed(2) ## simulate some data...
>>> dat <- gamSim(1,n=400,dist="normal",scale=2)
>>>
>>>
>>> colnames(dat)[1:5] <- c("reallylongnamey", "reallylongnamex0",
>>> "reallylongnamex1", "reallylongnamex2", "reallylongnamex3")
>>> b <-
>>> gam(reallylongnamey~s(reallylongnamex0)+s(reallylongnamex1)+s(reallylongnamex2)+s(reallylongnamex3),data=dat)
>>>
>>> options(width=20)
>>> b$formula # not wrapped to 20 characters
>>>
>>> options(width=150)
>>> b$formula # the formula is wrapped even though it fits in the specified
>>> width
>>>
>>> # also true for lm()
>>> b <-
>>> lm(reallylongnamey~reallylongnamex0+reallylongnamex1+reallylongnamex2+reallylongnamex3,data=dat)
>>>
>>> options(width=20)
>>> b$call
>>>
>>> options(width=150)
>>> b$call
>>>
>>>
>>> Thanks,
>>> Sarah
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list