有没有办法在R Markdown中的嵌入式LaTeX方程中使用r代码的结果 [英] Is there a way to use results of r code in embedded LaTeX equations in R Markdown

查看:400
本文介绍了有没有办法在R Markdown中的嵌入式LaTeX方程中使用r代码的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R Markdown文档的嵌入式LaTeX方程中是否可以使用类似\Sexpr的命令?我想使用像这样的东西来形成一个简单的线性回归方程:

Is there a command like \Sexpr that can be used inside embedded LaTeX equations in R Markdown documents? I would like to form the equation of a simple linear regression using something like this:

$\hat{Y}= \Sexpr{coef(model)[[1]]} + \Sexpr{coef(model)[[2]]} \cdot Length$

推荐答案

您可以使用r R_code_here:

$$  \hat{Y}= `r coef(model)[[1]]` + `r coef(model)[[2]]` \cdot Length$$

这应该会产生类似的内容:

This should produce something like:

但是我认为最好在单独的R块中定义系数并使方程尽可能简单.

But I think it is better to define your coefficients in a separate R chunk and keep your equation as simpler as possible.

```{r}
model <- lm(mpg~.,mtcars)
 coef1 <- coef(model)[[1]]
 coef2 <- coef(model)[[2]]
```

$$latex \hat{Y}= `r coef1` + `r coef2` \cdot Length$$

这篇关于有没有办法在R Markdown中的嵌入式LaTeX方程中使用r代码的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆