如何将R公式转换成文本?

时间:2022-05-08 14:57:41

I have trouble working with formula as with text. What I'm trying to do is to concatenate the formula to the title of the graph. However, when I try to work with the formula as with text, I fail:

我在使用公式和文本时遇到了麻烦。我要做的是将公式连接到图的标题上。然而,当我试着按照文本那样使用公式时,我失败了:

model <- lm(celkem ~ rok + mesic)
formula(model)
# celkem ~ rok + mesic

This is fine. Now I want to build string like "my text celkem ~ rok + mesic" - this is where the problem comes:

这是很好。现在我想构建像“我的文本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"

Now I see there is a sprint function in package gtools, but I think this is such a basic thing that it deserves a solution within the default environment!!

现在我看到在gtools包中有一个sprint函数,但是我认为这是一个如此基本的东西,它应该在默认环境中得到一个解决方案!

7 个解决方案

#1


26  

A short solution from the package formula.tools, as a function 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 might be useful in case of long formulas:

在长公式的情况下,减少可能是有用的:

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"

Which is because of width.cutoff = 60L in ?deparse.

这是因为宽度。切断= 60L ?

#2


19  

Try format :

试试格式:

paste("my text", format(frm))
## [1] "my text celkem ~ rok + mesic"

#3


10  

or as an alternative to Julius's version (note: your code was not self-contained)

或者作为Julius版本的替代品(注意:您的代码不是独立的)

celkem = 1
rok = 1
mesic = 1
model <- lm(celkem ~ rok + mesic)
paste("my model ", deparse(formula(model)))

#4


6  

I have found a solution, but it is ugly as hell...

我找到了一个解决办法,但它太丑了……

paste("my text", paste(as.character(formula(model))[c(2,1,3)], collapse = " "))

In fact, this is sooo hacky (especially the c(2,1,3)) I don't consider this a real solution... Please post something better :-)

事实上,这是sooo hacky(尤其是c(2,1,3))我不认为这是一个真正的解决方案……请张贴更好的东西:

#5


2  

Here a solution which use print.formula, it seems trick but it do the job in oneline and avoid the use of deparse and no need to use extra package. I just capture the output of the print formula, using capture.output

这里有一个使用打印的解决方案。公式,它看起来很巧妙,但是它在oneline中完成了工作,避免了使用,也不需要使用额外的包。我只是使用capture.output捕获打印公式的输出

paste("my text",capture.output(print(formula(celkem ~ rok + mesic))))
[1] "my text celkem ~ rok + mesic"

In case of long formula:

如公式较长:

 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"

#6


1  

Then add gsub to remove white spaces

然后添加gsub以删除空白

gsub("  ", "", paste(format(frm), collapse = ""))

#7


1  

The easiest way is this:

最简单的方法是:

f = formula(model)
paste(f[2],f[3],sep='~')

done!

完成了!

#1


26  

A short solution from the package formula.tools, as a function 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 might be useful in case of long formulas:

在长公式的情况下,减少可能是有用的:

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"

Which is because of width.cutoff = 60L in ?deparse.

这是因为宽度。切断= 60L ?

#2


19  

Try format :

试试格式:

paste("my text", format(frm))
## [1] "my text celkem ~ rok + mesic"

#3


10  

or as an alternative to Julius's version (note: your code was not self-contained)

或者作为Julius版本的替代品(注意:您的代码不是独立的)

celkem = 1
rok = 1
mesic = 1
model <- lm(celkem ~ rok + mesic)
paste("my model ", deparse(formula(model)))

#4


6  

I have found a solution, but it is ugly as hell...

我找到了一个解决办法,但它太丑了……

paste("my text", paste(as.character(formula(model))[c(2,1,3)], collapse = " "))

In fact, this is sooo hacky (especially the c(2,1,3)) I don't consider this a real solution... Please post something better :-)

事实上,这是sooo hacky(尤其是c(2,1,3))我不认为这是一个真正的解决方案……请张贴更好的东西:

#5


2  

Here a solution which use print.formula, it seems trick but it do the job in oneline and avoid the use of deparse and no need to use extra package. I just capture the output of the print formula, using capture.output

这里有一个使用打印的解决方案。公式,它看起来很巧妙,但是它在oneline中完成了工作,避免了使用,也不需要使用额外的包。我只是使用capture.output捕获打印公式的输出

paste("my text",capture.output(print(formula(celkem ~ rok + mesic))))
[1] "my text celkem ~ rok + mesic"

In case of long formula:

如公式较长:

 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"

#6


1  

Then add gsub to remove white spaces

然后添加gsub以删除空白

gsub("  ", "", paste(format(frm), collapse = ""))

#7


1  

The easiest way is this:

最简单的方法是:

f = formula(model)
paste(f[2],f[3],sep='~')

done!

完成了!