Is there a way to use results of r code in embedded LaTeX equations in R Markdown

心已入冬 提交于 2019-12-10 17:48:54

问题


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$

回答1:


You can use r R_code_here:

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

This should produce something like:

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$$


来源:https://stackoverflow.com/questions/19406717/is-there-a-way-to-use-results-of-r-code-in-embedded-latex-equations-in-r-markdow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!