在glm-output方面调用getOption(" width")

时间:2015-06-07 10:23:21

标签: r

在我看来,班级call不尊重getOption('width')。如果我想在例如summary中打印knitr模型,这会使长时间调用变得难看。

有没有办法解决这个问题?

这是一个小例子:

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

给出了(丑陋的)输出:

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

我发现了一个类似的问题:Is it possible to make print.formula respect the environment width option? 通过这个,我可以通过

抓住model$call
strwrap(capture.output(print(model$call)))
## [1] "glm(formula = \"response ~ factor1 + factor2"
## [2] "+ factor3\", family = Gamma(link = \"log\"),"
## [3] "data = dataframe)" 

,在使用换行符折叠时,cat提供了很好的打印输出:

cat(paste(
    strwrap(capture.output(print(model$call)))
    ,collapse = "\n"))
## glm(formula = "response ~ factor1 + factor2
## + factor3", family = Gamma(link = "log"),
## data = dataframe)

但我无法将变量分配给cat,即执行类似

的操作
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

1 个答案:

答案 0 :(得分:2)

您可以再次使用 capture.output将结果分配到变量中:

xx <- paste(
    strwrap(capture.output(print(model$call)))
    ,collapse = "\n"))

model$call <- capture.output(cat(xx))