yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Include `tab.cap` in `eval.after` and other chunk options (port to knitr) #2305

Closed rikudoukarthik closed 7 months ago

rikudoukarthik commented 8 months ago

Tables are an important part of documents generated using R markdown. Aside from the base kable, alternatives to creating tables like flextable recommend using tab.cap="" in the chunk options to define table captions. Very often, one would like to use inline code to generate elements of the caption, particularly since tables are often used to show data summaries. While this is easily done using a regular R string such as glue::glue("This is my caption, and {value1} is a value") for the fig.cap chunk option (after having set eval.after="fig.cap", which is now the default), this is currently not possible for tables because tab.cap is not an option in knitr but from officer.

There have been previous discussions (feature request) on porting this option over to knitr, but I don't think it has been prioritised. Given how frequently tables are generated in R markdown, I think it makes sense to allow the captioning for tables to follow the same format as for figures. My current workaround is to create a new initial chunk which creates the required objects, which are then referenced in-line in the subsequent chunks. But this is not very elegant, and it would be great to have caption options for tables and figures to be implemented in the same way.


By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

yihui commented 8 months ago

You can set it by yourself via opts_knit$set(), e.g.,

knitr::opts_knit$set(eval.after = c('tab.cap', 'fig.cap'))

If you want to apply this to all your documents, you can set a global option in your .Rprofile:

options(knitr.package.eval.after = c('tab.cap', 'fig.cap'))
github-actions[bot] commented 4 weeks ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.