Is there a way to use results of r code in embedde

2019-07-06 12:27发布

问题:

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


标签: r latex markdown