Reputation: 1438
It seems to me like the class call
does not respect the getOption('width')
. This makes long calls ugly if I want to print a summary
of a model in for example knitr
.
Is there any way to work around this issue?
Here is a small example:
dataframe <- data.frame(response = seq(10),factor1 = seq(10),
factor2 = seq(10), factor3 = seq(10))
model <- glm("response ~ factor1 + factor2 + factor3",
data = dataframe,
family = Gamma(link = 'log'))
which gives the (ugly) output:
Call: glm(formula = response ~ factor1 + factor2 + factor3, family = Gamma(link = "log"),
data = dataframe)
Coefficients:
(Intercept) factor1 factor2 factor3
0.2923 0.2253 NA NA
Degrees of Freedom: 9 Total (i.e. Null); 8 Residual
Null Deviance: 3.886
Residual Deviance: 0.4238 AIC: 33.05
I found a similar question: Is it possible to make print.formula respect the environment width option?
and with this I have been able to grab the model$call
by
strwrap(capture.output(print(model$call)))
## [1] "glm(formula = \"response ~ factor1 + factor2"
## [2] "+ factor3\", family = Gamma(link = \"log\"),"
## [3] "data = dataframe)"
which gives the nice printed output by cat
when collapsed with linebreaks:
cat(paste(
strwrap(capture.output(print(model$call)))
,collapse = "\n"))
## glm(formula = "response ~ factor1 + factor2
## + factor3", family = Gamma(link = "log"),
## data = dataframe)
but I cannot assign a variable to a cat
, i.e. do something like
model$call <- cat(paste(
strwrap(capture.output(print(model$call)))
,collapse = "\n"))
## glm(formula = "response ~ factor1 + factor2
## + factor3", family = Gamma(link = "log"),
## data = dataframe)
model$call
## NULL
Upvotes: 2
Views: 89
Reputation: 121568
You can use again capture.output
to assign the result in a variable :
xx <- paste(
strwrap(capture.output(print(model$call)))
,collapse = "\n"))
model$call <- capture.output(cat(xx))
Upvotes: 2