r - 在 glm-output 中进行调用 getOption ("width")

标签 r

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

有什么办法可以解决这个问题吗?

这是一个小例子:

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

最佳答案

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

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

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

关于r - 在 glm-output 中进行调用 getOption ("width"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692433/

相关文章:

r - 将列表转换为多个数据框,以便能够仅将每个数据框的一列转换为数字数据

r - 如何获取在指定环境中评估调用对象时使用的所有参数的值

r - `R` 中是否有 "output of the last command"的表达式?

r - 如何从模块内部切换 Shiny 的选项卡面板?

r - 如何以 Shiny 的方式正确输出 Plotly 图?

r - 使用 cut() 和 group_by()

在 R 数据帧中用 0 替换 NA

对具有特定名称模式的几组列进行 Rowsum

r - 如何重写这些 for 循环以在 R 中应用

javascript - 如何在 Node js中的exec中传递多个参数