简体   繁体   中英

dis-able automatic figure numbering in knitr

I am writing a HTML document with knitr. My settings are:

---
fontsize: 12pt

output:
  html_document:
    fig_height: 7
    fig_width: 9
    keep_md: yes
    smart: no
---

```{r global_options, include = FALSE}
knitr::opts_chunk$set(comment = NA)
```

I have a few code chunks that produce figures. Each of these chunks produces a single figure. I label the chunks with the understanding that, when I knit the document, the chunk labels become the file-names of the figures. For example, the chunk that I label "mtcar_histogram" should produce the file "mtcar_histogram.png".

But when I navigate to the "figure-html" sub-directory of the directory containing my document, I see that all the figures have an automatic numerical suffix. For example, I see "mtcar_histogram-1.png" instead of "mtcar_histogram.png".

When I checked the knitr options documentation , I read that:

"Note any number of plots can be recorded in a single code chunk, and this package does not need to know how many plots are in a chunk in advance -- it can figure out automatically, and name these images as fig.path-label-i where i is incremental from 1"

But as I stated earlier, none of my chunks produce multiple plots, so the numbering is un-necessary for me.

Is there a means of dis-abling the numbering?

Late answer, but might be useful. Taken from here:

https://github.com/yihui/knitr/issues/505

add this option to your setup chunk

knitr::opts_chunk$set(
                  fig.process = function(x) {
                      x2 = sub('-\\d+([.][a-z]+)$', '\\1', x)
                      if (file.rename(x, x2)) x2 else x
                      }
                  )

This will remove the suffix. To be used with caution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM