Chunks in text Rmarkdown - Xaringan - r

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

Related

Embedding https links in the figure ref captions of rbookdown

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")
```

Mermaid (diagrammeR) graph not displayed in RPres

I am trying to include a graph built with the diagrammeR package in an RPres. That's the graph:
library(DiagrammeR)
mermaid("graph TD
X1(X1)-->Z1(Z2)
X2(X2)-->Z2(Z2)
X1(X1)-->Z2(Z2)
Z1(Z1)-->Y(Y)
Z2(Z2)-->Y(Y)
")
Looking at the output in RStudio's viewer pane is no problem. No I include it in an RPres:
Untitled
========================================================
author:
date:
autosize: true
First Slide
========================================================
```{r,echo=FALSE, results = "asis"}
library(DiagrammeR)
mermaid("graph TD
X1(X1)-->Z1(Z2)
X2(X2)-->Z2(Z2)
X1(X1)-->Z2(Z2)
Z1(Z1)-->Y(Y)
Z2(Z2)-->Y(Y)
")
(note that the "```" to close the code chunk aren't displayed here because of markup...)
Alas, nothing but the abyss of emptiness:
Are you committed to RPres or would you consider alternative slide formats? For example, if you create a new R Markdown document and specify output: ioslides_presentation in the YAML header, the diagram will render properly:
---
title: "Untitled"
author: "Your Name"
date: "5/2/2020"
output: ioslides_presentation
---
Untitled
===========================================================
Here is the content for the second slide in different style
## Title of Mermaid Slide
```{r,echo=FALSE, results = "asis"}
library(DiagrammeR)
mermaid("graph TD
X1(X1)-->Z1(Z2)
X2(X2)-->Z2(Z2)
X1(X1)-->Z2(Z2)
Z1(Z1)-->Y(Y)
Z2(Z2)-->Y(Y)
")
Which produces this:

Can I align body text with margin note?

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:

xaringan slide separator not separating slides

In this example xaringan presentation, why are both the ## blank page and the leaflet map on the same slide, given I've separated them by the new-slide separator --- ?
---
title: "map test"
output:
xaringan::moon_reader:
css: ["default"]
nature:
highlightLines: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## blank page
content
---
leaflet page
```{r}
library(leaflet)
leaflet() %>%
addTiles()
```
---
Looks like you've got an unintended space after the new slide separator after blank content as "--- ". Remove that space and it'll be recognized as real slide separator:
---
title: "map test"
output:
xaringan::moon_reader:
css: ["default"]
nature:
highlightLines: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## blank page
content
---
leaflet page
```{r}
library(leaflet)
leaflet() %>%
addTiles()
```
---
In my case I'm adding mathjax/latex equations and I had:
$$
\begin{aligned} P(Y= k)=\comb{k-1}{r-1} * p^r q^{k-r}, \qquad k= r,r+1
\end{aligned}\label{pascal}\tag{5}
$$
And I had to remove the breaklines
$$\begin{aligned} P(Y= k)=\comb{k-1}{r-1} * p^r q^{k-r}, \qquad k= r,r+1
\end{aligned}\label{pascal}\tag{5}$$
and then it worked. I've that it renders mathjax better if all the code is in a single line.

How can I let a user control whether code is shown in a shiny rMarkdown document

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

Resources