Not sure how this is to be done, but I have a few citations and a source link in the caption for an image. However, the following doesn't seem to work:
(ref:my-caption) This is a [google link](https://google.com) in the caption of the figure.
```{r gim, fig.cap="(ref:my-caption)", fig.align='center', fig.asp=0.75, echo=FALSE, out.width='85%'}
knitr::include_graphics("./google-images.png")
```
The error from rbookdown build included:
! Argument of \caption#ydblarg has an extra }.
<inserted text>
\par
l.338 ...ogle link} in the caption of the figure.}
\label{fig:gim}
You could try this:
```{r gim, fig.cap=paste(ref_caption), fig.align='center', fig.asp=0.75, echo=FALSE, out.width='85%'}
ref_caption <- "This is a \\href{https://google.com}{google link} in the caption of the figure."
knitr::include_graphics("./google-images.png")
```
While the solution offered by #bttomio didn't solve the problem entirely, a small modification to the same did all the magic! I followed up on the solution offered here, and the following works:
```{r gim, fig.cap=paste(ref_caption), fig.align='center', fig.asp=0.75, echo=FALSE, out.width='85%'}
ref_caption <- "This is a \\protect\\href{https://google.com}{google link} in the caption of the figure."
knitr::include_graphics("./google-images.png")
```
Related
I am making a xaringan presentation and I am trying to show my chunk options in R Markdown, and I need that it appears in a gray square.
Like this image:
But I just get my code without that nice gray square:
And of course I don't know how to put the highlights in this kind of code chunks. If somebody knows how to do it, I will really appreciate it.
This is the code I wrote in order to get the chunk without the square:
```{r echo=FALSE, comment = NA}
cat("````
```{r, echo = TRUE, eval=TRUE, out.width = '50%', fig.align = 'center'}`r ''`
url <- 'https://media.springernature.com/full/springer-static/\nimage/art:10.1186%2Fs13059-020-02088-y/MediaObjects/13059_2020_2088_Fig1_HTML.png'
knitr::include_graphics(url)
```\n````")
```
Following the example in the "Ninja" presentation template, you can produce "raw chunks" by using the markdown formatting with some tricks:
# Test
````markdown
`r ''````{r, echo = TRUE, eval=TRUE, out.width = '50%', fig.align = 'center'}
url <- 'https://media.springernature.com/full/springer-static/\nimage/art:10.1186%2Fs13059-020-02088-y/MediaObjects/13059_2020_2088_Fig1_HTML.png'
knitr::include_graphics(url) #<<
```
````
Output:
```{r, echo = TRUE, eval=TRUE, out.width = '50%', fig.align = 'center'}
url <- 'https://media.springernature.com/full/springer-static/\nimage/art:10.1186%2Fs13059-020-02088-y/MediaObjects/13059_2020_2088_Fig1_HTML.png'
knitr::include_graphics(url) #<<
```
Line highlighting is done with #<<. For this, you need to enable the highlighting in the yaml header (again, I've taken the default from the Ninja template):
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
I'm using the tufte R package to create an html document with margin notes. Some of my margin notes are figures that are fairly tall. For example:
---
title: Big sidenote
output:
tufte::tufte_html: default
---
```{r setup, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r fig.margin = TRUE, fig.cap="Fig1. My margin figure is kind of tall.", echo=FALSE}
plot(mtcars)
```
Here is paragraph 1. It's pretty short and it's associated with Fig 1.
```{r fig.margin = TRUE, fig.cap="Fig 2. Related to the second paragraph.", echo=FALSE}
plot(subset(mtcars, cyl==6))
```
I'd like this paragraph to start in line with Fig 2.
```
I would like the paragraph in the main body to begin below the bottom of the figure in the margin.
Is this possible within the markdown? My CSS skills/understanding are limited.
I figured this out. Simple for those who know CSS well, but here for those who don't. The margin notes are created with a float property. You can use the float property to disallow floating elements to the side of your text.
I created a new "cleared" class that clears elements to the right:
<style>
.cleared {clear: right;}
</style>
Then, whenever I wanted text to skip down to the next figure, I created a div of the cleared class:
<div class = "cleared"></div>
Here is the full example:
---
title: Big sidenote
output:
tufte::tufte_html: default
---
<style>
.cleared {clear: right;}
</style>
```{r setup, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra = packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```
```{r fig.margin = TRUE, fig.cap="Fig1. My margin figure is kind of tall.", echo=FALSE}
plot(mtcars)
```
Here is paragraph 1. It's pretty short and it's associated with Fig 1.
<div class = "cleared"></div>
```{r fig.margin = TRUE, fig.cap="Fig 2. Related to the second paragraph.", echo=FALSE}
plot(subset(mtcars, cyl==6))
```
I'd like this paragraph to start in line with Fig 2.
And the result:
I want to do something along the lines of
```{r, echo=FALSE, message=FALSE, error=FALSE, warning=FALSE}
radioButtons("code","Show code", choices=c("No","Yes"), inline = T)
if (input$code=="No") {
showCode <- FALSE
} else {
showCode <- TRUE
}
```
```{r,echo= showCode, message=FALSE, error=FALSE, warning=FALSE}
# Main Code
```
But not sure how to wrap the code reactively
Is this feasible?
TIA
We recently added code folding to R Markdown (the rmarkdown package >= v0.9.5): http://rmarkdown.rstudio.com/html_document_format.html#code_folding
If I have the MWE:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
summary(cars)
```
then I do not get a caption. But if I do:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers"}
summary(cars)
```
i.e. remove eval=FALSE then the caption no longer loads.
why I wish to do this?
I want to put example bits of code into my document. the code won't actually work, hence why I want to supress it. Something like
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
Text text text
```{r fig.cap="Figure 1. Some random numbers",eval=FALSE}
for (i in 1:length(c){
#do something
}
```
where I am merely demonstrating a for loop, but not actually running the code.
As far as I know, knitr doesn't support captions for code by default. The easiest way to label your code blocks would be to add an explanation below the box in the markdown.
If you must have captions in the r code, you can use chunk hooks. Here's an example for your case:
---
title: "Example"
output:
pdf_document:
fig_caption: yes
---
```{r}
library(knitr)
knit_hooks$set(wrapper = function(before, options, envir) {
if (!before) {
sprintf(options$comment)
}
})
```
```{r comment="Figure 1. Some random numbers",wrapper=TRUE,eval=FALSE}
for (i in 1:length(c){
#do something
}
```
We have defined a hook (wrapper), where if we call wrapper=TRUE in any chunk options, the comment argument is printed below.
I have a situation where, for display purposes, I need to wrap an outputted plot in a <div> container.
At the most basic level, this is what I would like to do:
```{r fig.width=7, fig.height=6,results='asis',echo=FALSE}
cat('<div>')
plot(cars)
cat('</div>')
```
However, the output document looks like this:
![plot of chunk unnamed-chunk-2](figure/unnamed-chunk-2.png)
Is there a workaround if you need to "wrap" output?
The same behaviour only seems to occur when it's wrapping the plot. Otherwise, including closed tags works as expected:
```{r fig.width=7, fig.height=6,results='asis',echo=FALSE}
cat('<div>')
cat('</div>')
plot(cars)
cat('<h1>Hello</h1>')
```
Yet wrapping the image seems to break it. I'm also noticing that <img> is wrapped in <p> is it possible to stop this behaviour?
Here is one way to do it.
First, we create a chunk hook to wrap chunk output inside a tag.
We pass wrap = div as chunk option to wrap inside div.
Set out.extra = "" to fool knitr into outputting html for plot output. Note that this is required only for div tag and not for span, as markdown is parsed inside span tag.s
DONE!
Here is a gist with Rmd, md and html files, and here is the html preview
## knitr Chunk Hook to Wrap
```{r setup, echo = F}
knit_hooks$set(wrap = function(before, options, envir){
if (before){
paste0('<', options$wrap, '>')
} else {
paste0('</', options$wrap, '>')
}
})
```
```{r comment = NA, echo = F, wrap = 'div', out.extra=""}
plot(mtcars$mpg, mtcars$wt)
```