EDIT: the weird #tab thing disappeared after a reboot.
asked Sep 23, 2018 at 3:39 19.3k 37 37 gold badges 139 139 silver badges 249 249 bronze badgesSorry if my memory is vague since I was dealing with a similar issue 8+ years ago. I think I reran the document 'weaving' twice. In what I can remember, latex, on the first run, creates an index and inserts the values into the document only on the second run. This was outside R, so you may need to do some magic here, e.g. keep the intermediate files when rendering.
Commented Sep 23, 2018 at 7:01 thanks but there has to be a fix using rmarkdown or bookdown.. some weird option or something Commented Sep 23, 2018 at 17:57Why did you roll back the changes? Tried to remove a lot of the extra details which weren't relevant to the question
Commented Oct 12, 2018 at 18:47@MichaelHarper sorry buddy but you removed way too many things. The question is fine as is, and the screenshot is useful. I appreciate your initiatiive, but leave it be
Commented Oct 12, 2018 at 18:50 What else is relevant? At least remove all the LaTeX packages other than float Commented Oct 12, 2018 at 18:55The problem is that you are working against the intentions of kable by using it outside of an R chunk:
The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label.
So the following workaround is definitely on the hacky side. Using a file foo.Rmd with
--- output: bookdown::pdf_document2: toc: no header-includes: - \usepackage --- ``` knitr::opts_chunk$set(echo = TRUE) ``` ## Including Tables You can also embed tables, for example: \@ref(tab:tw) ``` mytable ``` You can also embed tables, for example: \@ref(tab:tw2) ``` mytable2 ``` Referencing images is easier: \@ref(fig:plt) ``` myplot ```
one can process this file with a second file foo.R :
library(knitr) library(kableExtra) # add the label to the options that would normally be populated from the chunk options opts_current$append(list(label = "tw")) mytable % kable(format = "latex", booktabs = T, caption = "Demo Table", escape = F) %>% kable_styling(latex_options = 'HOLD_position') opts_current$restore() opts_current$append(list(label = "tw2")) mytable2 % kable(format = "latex", booktabs = T, caption = "Demo Table", escape = F) %>% kable_styling(latex_options = 'HOLD_position') opts_current$restore() myplot
In principle, you can do these commands also just at the R prompt, but I try to not use the prompt directly. BTW, I do not get the (#tab) output with your code.
However, I think it makes more sense to not work against the workings of kable . I can understand that it can make sense to separate the data manipulation fro the presentation. However, creating the table is presentation from my point of view. So instead of creating the table externally I would just create the data externally. To make this concrete, let's use a file bar.Rmd :
--- output: bookdown::pdf_document2: toc: no header-includes: - \usepackage --- ``` knitr::opts_chunk$set(echo = TRUE) library(kableExtra) ``` ## Including Tables You can also embed tables, for example: \@ref(tab:tw) ``` mydata %>% kable(format = "latex", booktabs = T, caption = "Demo Table", escape = F) %>% kable_styling(latex_options = 'HOLD_position') ```
together with a file bar.R :
# insert data processing here mydata
This gives me the same output and the data processing is (initially!) separated from the presentation.