在我看来,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/