Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tab.cap in eval.after not evaluating R code properly #2352

Closed
rikudoukarthik opened this issue Jun 20, 2024 · 3 comments
Closed

tab.cap in eval.after not evaluating R code properly #2352

rikudoukarthik opened this issue Jun 20, 2024 · 3 comments

Comments

@rikudoukarthik
Copy link

rikudoukarthik commented Jun 20, 2024

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?

@yihui
Copy link
Owner

yihui commented Jun 20, 2024

That's because fig.cap is needed only after a chunk has finished evaluation. Generating the figure is done after a chunk's evaluation is done. However, for the table case here, it's different: the table caption is needed (by flextable()) in the middle of the chunk evaluation.

add a single ` to chunk open and close calls

Please read the issue guide when filing issues.

@rikudoukarthik
Copy link
Author

Interesting, I didn't consider that! So the eval.after option doesn't work for flextable()?

Also, even if the table caption is evaluated in the middle of chunk evaluation, cap_x is already assigned in the previous chunk, so how come that doesn't appear either?

@yihui
Copy link
Owner

yihui commented Nov 1, 2024

So the eval.after option doesn't work for flextable()?

No. It's not specific to flextable(). The problem exists for any chunk option that needs to be used inside the chunk evaluation, instead of after the evaluation.

Also, even if the table caption is evaluated in the middle of chunk evaluation, cap_x is already assigned in the previous chunk, so how come that doesn't appear either?

I don't know. This issue appears to be specific to flextable(), e.g., kable() works fine:

---
title: "Test"
---
  
```{r}
cap_x <- '100'
```

```{r, tab.cap=glue::glue("This is my caption with {cap_x}.")}
knitr::kable(iris[1:10,])
```

@yihui yihui closed this as completed Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants