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

Specify id attribute of generated image with chunk option #1703

Closed
3 tasks done
gvwilson opened this issue Apr 11, 2019 · 6 comments
Closed
3 tasks done

Specify id attribute of generated image with chunk option #1703

gvwilson opened this issue Apr 11, 2019 · 6 comments
Labels
question Questions (which should belong to forums instead of Github)
Milestone

Comments

@gvwilson
Copy link

I am able to specify the caption for a generated image using the fig.cap chunk option. I would like to be able to specify the id attribute of a generated image with a similar chunk option, so that (for example):

```{r fig.cap="My Title", fig.id="f:snubble"}
data %>% ggplot() + geom_point(mapping = aes(x = a, y = b))
```

would produce:

<p><img src="figures/unnamed-chunk-4-1.png" alt="My Title" id="f:snubble" /></p>

with HTML output and:

![My Title](figures/unnamed-chunk-4-1.png")
{: id="f:snubble"}

when generating GitHub Flavored Markdown. Note that Kramdown turns the latter into:

<p id="f:snubble"><img src="figures/unnamed-chunk-4-1.png" alt="My Title" /></p>

which isn't quite the same as setting the ID on the figure, but seems to be the closest we can get.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.name/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.
@cderv
Copy link
Collaborator

cderv commented Apr 12, 2019

What can also be done in Rmarkdown currently is using pandoc feature to add div with attributes

---
title: "Untitled"
date: "09/04/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Including Plots

You can also embed plots, for example:

::: {#f:snubble}
```{r pressure, echo=FALSE}
plot(pressure)
```
:::

But you'll get in html

<div id="f:snubble">
<p><img src="test_files/figure-html/pressure-1.png" width="672"></p>
</div>

or`in md

<div id="f:snubble">
![](test_files/figure-gfm/pressure-1.png)
</div>

Not quite the same... but enough to have a css selector for the image.

Does it need to be an id on the image tag exclusively ? (I don't know kramdown)

I think it could be something possible like class.output option in rmarkdown but I am not sure as the conversion is done by pandoc, and I don't know if we can easily add an id to an image for pandoc conversion. 🤔 (there is several ways to add attributes). I'll need to dig into this when I have time.
(could be a Rmarkdown feature maybe not knitr...)

Feel free to look into it if you see how it can be done so that me could see the impact on knitr (or rmarkdown)

@gvwilson
Copy link
Author

gvwilson commented Apr 12, 2019

Thanks for your reply. An id on the div would be fine - as long as there's something user-specified for me to target. However, I'm using Jekyll as a Markdown processor (because that's what this template uses), and this method doesn't seem to work - is it because the ::: syntax is specific to Pandoc?

Command line build script:

#!/usr/bin/env Rscript

library(knitr)

args <- commandArgs(trailingOnly = TRUE)
src = normalizePath(args[1])
dst = normalizePath(args[2])
knit(src, output = dst)

Source file:

---
title: "R Markdown"
output: md_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = T, comment = "#>")
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

Create a plot:

::: {#f:simple-plot}
```{r fig.cap="Simple Plot"}
data <- tribble(~a, ~b, 1, 10, 2, 22, 3, 35)
data %>% ggplot() + geom_point(mapping = aes(x = a, y = b))
```
:::

Command line:

./build.R test.Rmd test.md

Markdown output:

---
title: "R Markdown"
output: md_document
---



Create a plot:

::: {#f:simple-plot}

```r
data <- tribble(~a, ~b, 1, 10, 2, 22, 3, 35)
data %>% ggplot() + geom_point(mapping = aes(x = a, y = b))
```

![Simple Plot](figure/unnamed-chunk-1-1.png)
:::

@cderv
Copy link
Collaborator

cderv commented Apr 12, 2019

Yes this syntax is from pandoc.
https://pandoc.org/MANUAL.html#divs-and-spans
I assumed you used rmarkdown but your are just using knitr.
Thanks for the reprex. I'll think of something if this could be included in knitr... 🤔

@gvwilson
Copy link
Author

Merci beaucoup.

@atusy
Copy link
Collaborator

atusy commented May 13, 2019

You can add any attributes to image by specifying out.extra in chunk option.

```{r, out.extra='id="hoge"'}
plot(iris)
```

becomes something like

<img src="rmd-out-extra_files/figure-html/unnamed-chunk-1-1.png" width="672" id="hoge" />

cf. https://yihui.name/knitr/options/#plots

@yihui yihui added this to the v1.23 milestone May 16, 2019
@yihui yihui added the question Questions (which should belong to forums instead of Github) label May 16, 2019
@yihui yihui closed this as completed May 16, 2019
@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 Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Questions (which should belong to forums instead of Github)
Projects
None yet
Development

No branches or pull requests

4 participants