Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

figure captions, references using knitr and markdown to html

I'm writing an Rmd file, to be processed by knitr into HTML. It contains some R chunks that generate figures, which get stored as data URIs in HTML.

1) How do I add a caption to such an image? I'd like to have a caption that says something like "Figure 3: blah blah blah", where the "3" is automatically generated.

2) How do I later on reference this image, i.e., "as you can see in Figure 3, blah blah".

like image 493
Jack Tanner Avatar asked Dec 12 '12 20:12

Jack Tanner


People also ask

What is Knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

What is the difference between markdown and RMarkdown?

R Markdown (markup language)R Markdown files are plain text files that typically have the file extension . Rmd . They are written using an extension of markdown syntax that enables R code to be embedded in them in a way which can later be executed.

How do I embed a plot in R markdown?

You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE} plot(cars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


1 Answers

I'm late to the party, but I wanted to mention a small package I recently built to do figure captioning and cross-referencing with knitr. It is called kfigr and you can install it using devtools::install_github('mkoohafkan/kfigr'). It is still in active development but the main functionality is there. Be sure to check out the vignette, it shows some usage examples and defines some hooks for figure captions and anchors (I may later choose to have the package import knitr and define those hooks on load).

EDIT: kfigr is now available on CRAN!

like image 94
mikeck Avatar answered Oct 20 '22 10:10

mikeck