You need to use a format from bookdown to get cross references, see https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html
---
title: "document title"
author: "author here"
date: "2022-01-20"
output:
bookdown::pdf_document2:
fig_caption: yes
number_sections: yes
toc: yes
toc_depth: 4
latex_engine: xelatex
keep_tex: true
---
```{r, include = FALSE}
library(flextable)
library(knitr)
library(bookdown)
opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width = "85%", fig.align = "center")
```
```{r, tab.id = "anyTable", tab.cap = "Invented data"}
a = data.frame(id = LETTERS[1:3], x = 1:3)
flextable(a) |> theme_vanilla()
```
```{r secondTable}
b = data.frame(id = LETTERS[4:6], x = 4:6)
flextable(b) |> theme_vanilla() |> set_caption(caption = "This is secondTable")
```
# Bookdown crossreference
Trying anyTable: \@ref(tab:anyTable).
Trying secondTable: \@ref(tab:secondTable).
CLICK HERE to find out more related problems solutions.