Search code examples
rr-markdownkable

RMarkdown is not referencing tables


Somehow my RMarkdown document is not crossreferencing tables or figures. Here is a stripped down version of my document.

---
title: "Test"
author: "Me"
date: "01/04/2022"
output: bookdown::pdf_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
var1<-sample(LETTERS)
tab1<-table(var1)

My table is in Table \@ref{tab:tab1}

library(knitr)
kable(tab1, caption="my table")

AS we see in Figure \@ref{fig:plot1}

plot(seq(1,10,1))

Solution

  • You should call your tab1 in the code chunk like this {r tab1}. And use () instead of {} for your @ref. In that case it reference to your figures and tables. You can use the following code:

    ---
    title: "Test"
    author: "Me"
    date: "01/04/2022"
    output: bookdown::pdf_document2
    ---
    
    My table is in Table \@ref(tab:tab1)
    
    ```{r tab1, echo =FALSE}
    var1<-sample(LETTERS)
    tab1<-table(var1)
    
    library(knitr)
    kable(tab1, caption="my table")
    ```
    
    \newpage
    
    AS we see in Figure \@ref(fig:plot1)
    
    ```{r plot1, fig.cap ="plot", echo=FALSE}
    par(mar = c(4, 4, .2, .1))
    plot(seq(1,10,1))
    ```
    

    Output:

    enter image description here

    As you can see on the image, when you click on 1 in will go to your table.