1

I try to cross-reference a table in R Markdown. When I render the document (whether to .html or to .pdf), it simply prints "??" instead of the reference. output

Here is what my code looks like.

---
title: "reproducible example"
date: "2023-04-13"
output:
  bookdown::html_document2: default
  bookdown::pdf_document2: default
---

My reference \@ref(tab:test)

```{r test, echo=FALSE}
library(tidyverse)
kableExtra::kable(tibble(variable= "hello", another = "bye"))```

Any ideas? Thank you!

I use the latest version of Rstudio as well as these packages:

> sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] kableExtra_1.3.4 lubridate_1.9.2  forcats_1.0.0    stringr_1.5.0    dplyr_1.1.1      purrr_1.0.1     
 [7] readr_2.1.4      tidyr_1.3.0      tibble_3.2.1     ggplot2_3.4.2    tidyverse_2.0.0 

loaded via a namespace (and not attached):
 [1] highr_0.10        compiler_4.2.2    pillar_1.9.0      tools_4.2.2       digest_0.6.31     timechange_0.2.0 
 [7] evaluate_0.20     lifecycle_1.0.3   gtable_0.3.3      viridisLite_0.4.1 pkgconfig_2.0.3   rlang_1.1.0      
[13] cli_3.6.1         rstudioapi_0.14   yaml_2.3.7        xfun_0.38         fastmap_1.1.1     withr_2.5.0      
[19] httr_1.4.5        xml2_1.3.3        knitr_1.42        hms_1.1.3         generics_0.1.3    vctrs_0.6.1      
[25] systemfonts_1.0.4 tidyselect_1.2.0  webshot_0.5.4     grid_4.2.2        svglite_2.1.1     glue_1.6.2       
[31] R6_2.5.1          fansi_1.0.4       rmarkdown_2.21    tzdb_0.3.0        magrittr_2.0.3    scales_1.2.1     
[37] htmltools_0.5.5   MASS_7.3-58.3     rvest_1.0.3       colorspace_2.1-0  utf8_1.2.3        stringi_1.7.12   
[43] munsell_0.5.0
yuliaUU
  • 1,581
  • 2
  • 12
  • 33
  • Welcome to SO, Jan! Are you using an output format from `bookdown`? If not then that is most likely the reason for your issue as: *"Cross-referencing is not provided directly within the base rmarkdown package, but is provided as an extension in bookdown"* (see https://bookdown.org/yihui/rmarkdown-cookbook/cross-ref.html#cross-ref). If that is not your issue then please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), i.e. a minimal Rmd which others can render to reproduce your issue. – stefan Apr 13 '23 at 08:42
  • 1
    Thank you @stefan! Indeed I did not use bookdown as an output format, thanks for bringing this up. However, even when I do, I just get "??" instead of the correct reference. Here is (what I hope makes for) a reproducible example: --- title: "reproducible example" date: "2023-04-13" output: bookdown::html_document2: default bookdown::pdf_document2: default --- My reference \@ref(tab:test) ```{r test, echo=FALSE} library(tidyverse) kableExtra::kable(tibble(variable= "hello", another = "bye")) ``` – Jan Pfänder Apr 13 '23 at 09:04

1 Answers1

0

Besides an output format from bookdown and a chunk label the third necessary condition for cross-referencing mentioned in the R Markdown Cookbook is that you need

A caption to your figure (or table).

Hence, adding a caption to your kable table should fix your issue.

---
title: "reproducible example"
date: "2023-04-13"
output:
  bookdown::html_document2: default
  bookdown::pdf_document2: default
---

My reference \@ref(tab:test)

```{r test, echo=FALSE}
library(tidyverse)
kableExtra::kable(tibble(variable= "hello", another = "bye"), caption = "The mtcars data.")
```

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51