简体   繁体   English

字符串中的 Markdown 数学公式(RStudio knitr)

[英]Markdown math formula from character string (RStudio knitr)

I have a character string variable (extracted from a regression model) that I would like to display as a math formula in a report using markdown in Rstudio knitr.我有一个字符串变量(从回归模型中提取),我想在报告中使用 Rstudio knitr 中的 markdown 将其显示为数学公式。 For example例如

> formula
[1] "dGU ~ L(ec, 1) + dHH + L(dHH, 1) + L(dHH, 2) + L(dHH, 3) + dJKM + L(dJKM, 1) + L(dJKM, 2) + L(dJKM, 3) + L(dJKM, 4) + dTTF + L(dGU, 1) + L(dGU, 2) + L(dGU, 3) + L(dGU, 4) + L(dGU, 5) + L(dGU, 6) + L(dGU, 7) + L(dGU, 8) + L(dGU, 9) + L(dGU, 10) + L(dGU, 11) + L(dGU, 12) + L(dGU, 13) + L(dGU, 14) + L(dGU, 15)"
> 

I know I can always manually type a math formula using markdown, but I would like to use whatever character string variable is returned by the regression model programmatically.我知道我总是可以使用 markdown 手动键入数学公式,但我想使用回归 model 以编程方式返回的任何字符串变量。

How about something like this?这样的事情怎么样?

Put this in your code chunk first:首先把它放在你的代码块中:

fml <- formula(dGU ~ L(ec, 1) + dHH + L(dHH, 1) + L(dHH, 2) + L(dHH, 3) + dJKM + L(dJKM, 1) + L(dJKM, 2) + L(dJKM, 3) + L(dJKM, 4) + dTTF + L(dGU, 1) + L(dGU, 2) + L(dGU, 3) + L(dGU, 4) + L(dGU, 5) + L(dGU, 6) + L(dGU, 7) + L(dGU, 8) + L(dGU, 9) + L(dGU, 10) + L(dGU, 11) + L(dGU, 12) + L(dGU, 13) + L(dGU, 14) + L(dGU, 15))


md_fml <- function(fml) {
  Reduce(paste, deparse(fml))
}

And call this in markdown after:然后在 markdown 中调用它:

Here is my formula: r md_fml(fml)这是我的公式: r md_fml(fml)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM