rmd child cross referencing

215 Views Asked by At

I am currently working on a report I need to output as a word document.The corresponding rmd file starts to be long and long to run. For this reason I tried using rmd child files.

While I can do this, cross referencing does no longer work properly. After the first call to a child document, references to tables and/or figure in both the docx legends and text are displayed as 'code'.

I would appreciate your input on this. Below are 2 pieces of code for the parent and child rmd files.

Parent rmd file code

---
title: "Cross ref with child rmd"
author: "Guillaume"
output: 
  bookdown::word_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(magrittr)
```

<!-- ################### first figure and table from the parent document ###################-->

Here is a reference to the first figure \@ref(fig:fig.num.1)

```{r fig.num.1, echo=FALSE, fig.cap = "This is the first figure"}
rnorm(100) %>% boxplot
```

Here is a reference to the first.table \@ref(tab:tab.num.1)

```{r tab.num.1, echo=FALSE}
rnorm(9, 1, 1) %>% matrix(., 3, 3) %>% as.data.frame() %>% knitr::kable(., caption = "This is the third table")
```

<!-- ################### second figure and table from the child ###################-->
```{r child="child.rmd", echo=FALSE}
```

<!-- ################### third figure and table from the parent document ################### -->

Here is a reference to the third figure \@ref(fig:fig.num.3)

```{r fig.num.3, echo=FALSE, fig.cap = "This is the third figure"}
rnorm(100) %>% boxplot
```

Here is a reference to the third table \@ref(tab:tab.num.3)

```{r tab.num.3, echo=FALSE}
rnorm(9, 1, 1) %>% matrix(., 3, 3) %>% as.data.frame() %>% knitr::kable(., caption = "This is the third table")

Child rmd file code

<!-- ################### second figure and table from the child ################### -->

Here is a reference to the second figure from the child rmd \@ref(fig:fig.num.2)

```{r fig.num.2, echo=FALSE, fig.cap = "This is the second figure, coming from child rmd"}
rnorm(100) %>% boxplot
```

Here is a reference to the second table from the chold rmd \@ref(tab:tab.num.2)

```{r tab.num.2, echo=FALSE}
rnorm(9, 1, 1) %>% matrix(., 3, 3) %>% as.data.frame() %>% knitr::kable(., caption = "This is the second table, coming from child rmd")
```   
0

There are 0 best solutions below