Is there a way to suppress automatic figure numbering made by rmarkdown? The first plot rendered by R is the second figure, it added a Figure 1: prefix which is not what I need. I see a solution for PDF output but not html output.
Best,
Shixiang
It's difficult to know what you are looking for without a minimal reproducible example. But maybe you are looking for bookdown::html_document2 output.
Try the following:
---
title: "Example Document"
author: "Your name here"
output:
bookdown::html_document2
---
```{r cars, fig.cap = "plot"} # Figure with caption
plot(cars)
```
```{r cars3, fig.cap = ""} # Figure without caption
plot(cars)
```
This is the output of the above:
Related
This section of the R Markdown Cookbook shows how to create a plot and to display it later in the document:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
After another paragraph, we introduce the plot:
![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)
The problem I have is that I can't reference this plot in the text. See the following code and output:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot).
After another paragraph, we introduce the plot:
![A nice plot.](`r knitr::fig_chunk('cars-plot', 'png')`)
Note that if I display the plot immediately, the reference works well:
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, fig.cap="A nice plot."}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot).
How can display the plot later but still be able to reference it? I know that I could simply move the chunk where I want the plot to appear but this is not ideal in my case.
Use ref.label in a later chunk and refer to the later chunk name in the figure reference.
---
output: bookdown::pdf_document2
---
We generate a plot in this code chunk but do not show it:
```{r cars-plot, dev='png', fig.show='hide'}
plot(cars)
```
Here's a reference to this plot: figure \#ref(fig:cars-plot-show).
After another paragraph, we introduce the plot:
```{r cars-plot-show, ref.label="cars-plot", fig.cap="A Nice Plot"}
```
How can I add an empty code chunk in R markdown? I have found several options to manipulate the html to give more white space. But I would like to present some empty lines in the well known gray code box in order to indicate space for assigments.
---
title: "Untitled"
author: "Author"
output: html_document
---
## R Markdown
```{r cars}
summary(cars)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
```
A hacky way... almost there:
```{r, code="'\n\n\n\n'", results=F}
```
A possible solution using results = 'asis' and relying on chunck HTML class:
```{r, results='asis', echo=F}
cat(
'<pre class="r">
<code class = "hlsj"> <span class="hljs-string"> <br> <br> </span> </code>
</pre>
')
```
Just add <br> to increase the number of lines.
There does not seem to be a way to get knitr to recognise a completely empty chunk as a chunk. It will always omit it, regardless of the chunk options.
You have to insert something to get it to render, for example a comment. So you can put the empty lines between two comments:
---
output: html_document
---
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code here
# End
```
Or with the strip.white=FALSE chunk option we can use a single comment line, but strangely this only works for leading, not trailing, whitespace:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(strip.white = FALSE)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code above
```
I am putting together an R Markdown document in HTML, and I have the following YAML:
---
title: "R Markdown Example"
author: "Me"
date: "October 30, 2017"
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
code_folding: "show"
---
I am trying to insert a figure with auto-numbered caption with the following code:
```{r, fig.cap="Figure caption \\label{fig_1}"}
plot(mtcars$hp, mtcars$mpg)
```
But the caption number won't show up. When I try to reference the image in the text
(Figure \ref{fig_1})
I just get:
(Figure )
I have the same problem. This only happens for Word and HTML outputs, so I think I may be using LaTeX documentation instead of the proper input for these types. I've tried a lot of the different recommendations for figure captions, but I can't seem to get any of it to work.
I needed to output to Word to adhere to a journal that only accepted Word... So I ended up using the captioner package. This should also work with html and pdf.
library(captioner)
fig_nums <- captioner()
In my r chunk I put my plot code and then the caption comes directly after the chunk in ticks, using the fig_nums function ``:
```{r}
plot(pressure)
```
`r fig_nums("pressure-plot", "pressure against temperature")`
It looks like this in the Word output:
Then I can refer to it in text like this:
As you can see from the nice visualisation in r fig_nums("pressure-plot", display = "cite") the pressure rises as
temperature rises.
This will show as:
As you can see from the nice visualisation in Figure 1 the pressure rises as
temperature rises
Be sure to run the first time you use, and check-out for more info:
install.packages("captioner")
vignette("using_captioner")
You can use the bookdown::html_document2 format.
---
title: "Untitled"
output: bookdown::html_document2
---
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
\#ref(fig:pressure)
Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this
I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.
---
output: ioslides_presentation
---
## Slide with Plot
```{r, echo=TRUE}
plot(cars)
```
Any one have any idea on how to do this in Rstudio?
I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.
It's easier to run the code twice, once not evaluating the code, the second time not showing the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot 1
```{r, eval = FALSE}
plot(1:10)
```
## Plot 2
```{r, echo = FALSE}
plot(1:10)
```
It also avoids the error when smaller is true.
To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:
```{r setup, include=FALSE}
library(knitr)
local({
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
paste0('\n\n----\n\n', hook_plot(x, options))
})
})
```
If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.
---
title: "Plot Separation"
output: ioslides_presentation
---
## Plot code
```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```
## Plot display
```{r cars_plot, echo = FALSE, eval = TRUE}
```
```
I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.
You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.