model <- lm(celkem ~ rok + mesic)
formula(model)
# celkem ~ rok + mesic
这很好。现在,我想建立像
"my text celkem ~ rok + mesic"
的字符串 - 这是问题所在的地方:
paste("my text", formula(model))
# [1] "my text ~" "my text celkem" "my text rok + mesic"
paste("my text", as.character(formula(model)))
# [1] "my text ~" "my text celkem" "my text rok + mesic"
paste("my text", toString(formula(model)))
# [1] "my text ~, celkem, rok + mesic"
现在我看到包装中有一个
sprint
函数,但是我认为这是一个基本的事情,在默认环境中应该得到解决方案!!
包装中的简短解决方案
formula.tools
::
as.character.formula
frm <- celkem ~ rok + mesic
Reduce(paste, deparse(frm))
# [1] "celkem ~ rok + mesic"
library(formula.tools)
as.character(frm)
# [1] "celkem ~ rok + mesic"
对于长公式而言可能很有用:
Reduce
是因为
frm <- formula(paste("y ~ ", paste0("x", 1:12, collapse = " + ")))
deparse(frm)
# [1] "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + "
# [2] " x12"
Reduce(paste, deparse(frm))
# [1] "y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12"
Try
width.cutoff = 60L
:?deparse
R4.0.0(发布2020-04-24)引入了
format
paste("my text", format(frm))
## [1] "my text celkem ~ rok + mesic"
wo,它仍然具有一个
deparse1
参数(默认值(最大值):f <- y ~ a + b + c + d + e + f + g + h + i + j + k + l + m + n + o +
p + q + r + s + t + u + v + w + x + y + z
deparse(f)
# [1] "y ~ a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + " " p + q + r + s + t + u + v + w + x + y + z"
deparse1(f)
# [1] "y ~ a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z"
),然后引入了线路破裂,但线路分隔为
width.cutoff
(默认值:
500
)而不是collapse
,留下额外的白色空格(即使使用" "
)(如果需要的话,请删除它们,请参阅wemoss d's Answer\n
在r
collapse = ""
中使用它(建议)
或复制其实现:
gsub
或作为朱利叶斯版本的替代方案(注意:您的代码不是独立的)
backports
最简单的方法是:
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2019 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# https://www.R-project.org/Licenses/
deparse1 <- function (expr, collapse = " ", width.cutoff = 500L, ...)
paste(deparse(expr, width.cutoff, ...), collapse = collapse)
使用
celkem = 1
rok = 1
mesic = 1
model <- lm(celkem ~ rok + mesic)
paste("my model ", deparse(formula(model)))
f = formula(model)
paste(f[2],f[3],sep='~')
捕获打印公式的输出
print.formula
deparse
基于
capture.output
(基于paste("my text",capture.output(print(formula(celkem ~ rok + mesic))))
[1] "my text celkem ~ rok + mesic"
)是 ff <- formula(paste("y ~ ", paste0("x", 1:12, collapse = " + ")))
paste("my text",paste(capture.output(print(ff)), collapse= ' '))
"my text y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12"
deparse
):rlang::expr_text()
他们确实有一个宽度的论点以避免线断裂,但这也仅限于500个字符。至少这是一个很可能已经加载的单个功能...
然后添加GSUB以删除白色空间
rlang::quo_text()
今天优化了一些功能。到目前为止尚未提及的一些方法。