Accessing objects from another mardown file

Solution 1:

Use knitr::knit_child() or (equivalently) the child option in a chunk and set to include = FALSE.

file2.Rmd:

---
output: word_document
---

```{r include=FALSE}
knitr::knit_child("file1.Rmd")
```

The value of y is `r y`

enter image description here

Or file2.Rmd (however in this solution, text output is also displayed):

---
output: html_document
---

```{r, child="file1.Rmd", include=FALSE}
```

The value of y is `r y`

Then file1.Rmd:

---
output: html_document
---

```{r variables, include=FALSE}
y <- 10
```

This is text.