Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to hide figure captions when using knitr and pandoc to create docx files?

I am using knitr and pandoc to write reports into word (we need to be able to circulate for comments using track changes etc).

It is working very well so far, but I have found that the plots are all coming out with captions at the bottom, and I don't want captions. While I could just delete them in the word doc, if I can stop them showing in the code it would be better.

So for the following code in markdown:

Test test test

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plot(cars)
```

I then run the following code in R:

library("knitr")

# Stackoverflow table test 1.html

knit2html("captiontest.rmd")

FILE <- "captiontest"

system(paste0("pandoc -o ", FILE, ".docx ", FILE, ".md"))

And the graph, in the word document, has the caption "plot of chunk unnamed-chunk-2"

I know I can change this caption, e.g. {r fig.width=7, fig.height=6, fig.cap='hello'}, but I thought that fig.cap=NULL would make it hidden. Instead it seems to make the whole plot disappear.

Are plots required to have a caption - do I just have to go through each word doc and delete them manually? Or is there a way to hide them?

like image 376
Froom2 Avatar asked Jan 24 '14 10:01

Froom2


1 Answers

Kind of a dirty trick, but:

You can set fig.cap="" on the chunk in question:

Test test test

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6, fig.cap=""}
plot(cars)
```

Or, you can set fig.cap="" for all chunks at once in an initializing chunk at the beginning of your Rmd document:

Test test test

```{r options-chunk} 
opts_chunk$set(fig.cap="")
``` 

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6}
plot(cars)
```
like image 100
Brandon Bertelsen Avatar answered Oct 06 '22 08:10

Brandon Bertelsen