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

Assign default id to each fig for html output #2169

Closed
jooyoungseo opened this issue Sep 8, 2022 · 5 comments
Closed

Assign default id to each fig for html output #2169

jooyoungseo opened this issue Sep 8, 2022 · 5 comments
Assignees

Comments

@jooyoungseo
Copy link

I am not sure whether this is a reasonable feature request, but it would be great if knitr could assign default id attribute to each fig element for html output. I think the best default would be the same as image file name (chunk label and fig.cur suffix).

There would be many possible use cases, but I am working on accessibility manipulation and accessing to id attribute is necessary.

In #2163, @yihui kindly explained how to assign id the same as options$label to out.extra via knitr hooks, however, that way cannot assign unique id value for sub-figs plotted within the same chunk.

I assume that I can employ fig.cur and fig.num options to address this, but I need more time to figure out how to make them work.

In the following code example, fig.num and fig.cur options return NULL instead of specific figure numbers.

```{r, out.extra="foo='bar' foo2='bar2'"}
hist(airquality$Ozone, col = "red")

library(tidyverse)

ggplot(mpg, aes(class)) +
  geom_bar()

knitr::opts_current$get(name = "label")
knitr::opts_current$get(name = "out.extra")
knitr::opts_current$get(name = "fig.cur")
knitr::opts_current$get(name = "fig.num")
```

Therefore, I cannot assign unique suffix to each sub-fig via knitr hooks:

knitr::opts_hooks$set(label = function(options) {
  if (!any(stringr::str_detect(options$out.extra, "id\\s?=")) || is.null(options$out.extra)) {
    number <- ifelse(is.null(options$fig.cur), 1L, options$fig.cur)

    options$out.extra <- paste(options$out.extra, paste0("id=\"", paste0(options$label, "-", number), "\""))
  }

  options
})
yihui added a commit to yihui/yihui.org that referenced this issue Oct 18, 2022
@yihui yihui closed this as completed in c5a1747 Oct 18, 2022
@yihui
Copy link
Owner

yihui commented Oct 18, 2022

I've added a chunk option fig.id. When set to TRUE, it will generate an ID for each figure automatically. Internally, it just appends an id attribute to out.extra.

Detailed documentation at https://yihui.org/knitr/options/

@jooyoungseo
Copy link
Author

Fantastic! This works nicely :) Thanks for the implementation, @yihui !

@jooyoungseo
Copy link
Author

@yihui -- I was wondering if you would mind advising me how to obtain current id from inside custom knit_print(). Say, I want to redefine the knit_print() S3 function that keeps every default behavior but I want to do some tweaks with current plot id. I know the following is the nitty-gritty, but how can I pass current chunk options within the custom knit_print()?

function(options) {
  id <- sprintf("%s%s-%s", options$fig.lp, options$label, options$fig.cur)
  id <- xfun::alnum_id(id)
}

@yihui
Copy link
Owner

yihui commented Oct 19, 2022

I'm not sure what your use case is, but I'm afraid it would be too late for knit_print() to know the plot id, because the id is not generated until when the plot is to be written to the <img> tag. At that time, knit_print() has already been called. However, if you mean to use the id from a previous chunk in a later code chunk, that would be perfectly fine (in this case, you can store the id anywhere you like, e.g., use <<- inside the fig.id function to assign the id to a global variable).

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants