knitr icon indicating copy to clipboard operation
knitr copied to clipboard

`tab.cap` in `eval.after` not evaluating R code properly

Open rikudoukarthik opened this issue 8 months ago • 2 comments

Firstly, apologies for the huge delay in responding---this is following on from #2305 which got locked after inactivity.

While @yihui showed how eval.after = "tab.cap" can be included in the knitr::opts_knit$set() call, the issue is still unresolved because the tab.cap is not functioning as expected. Please see the small reprex below (add a single ` to chunk open and close calls).

---
title: "Test"
output: 
  bookdown::word_document2:
editor_options: 
  chunk_output_type: console
---
  
```{r, echo=FALSE}

library(glue) 

knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
knitr::opts_knit$set(eval.after = "tab.cap")


# function to add commas to big numbers in one go
good_num <- function(number) {
  
  if (number < 10) {
    xfun::numbers_to_words(number)
  } else {
    format(number, big.mark = ",", scientific = FALSE)
  }
  
}

cap_x <- good_num(12345)
```
  
  
```{r, eval.after = "tab.cap", tab.cap=glue("This is my caption; neither {cap_x} or {cap_y} appears.")}

library(tidyverse)
library(flextable)

cap_y <- good_num(45678)

iris %>% 
  slice(1:10) %>% 
  flextable()
  
```

Ideally, assuming fig.cap behaviour, the caption for the above table should read "This is my caption; neither 12,345 or 45,678 appears.", but instead I get this:

image

Why is tab.cap not behaving as fig.cap does?

rikudoukarthik avatar Jun 20 '24 12:06 rikudoukarthik