Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

markdown

r

latex

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$
like image 877
George Dontas Avatar asked Jan 31 '26 17:01

George Dontas


1 Answers

You can use r R_code_here:

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

This should produce something like:

enter image description here

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$$
like image 83
agstudy Avatar answered Feb 02 '26 09:02

agstudy