How to use code folding in Distill for R Markdown articles? - r-markdown

We have code folding in RMarkdown documents, but only when the output is html_document. The answer: https://stackoverflow.com/a/37839683/5359328 doesn't work in Distill for R Markdown Articles since the way of adding CSS and JS is unknown. How to use code folding in Distill for R Markdown Articles?

You can use the codefolder package for either Distill documents or Bookdown.
For Distill this is done by including the following in the Rmarkdown document:
<aside>
```{r codefolder, echo=FALSE, results='asis'}
codefolder::distill(init = "hide")
```
</aside>

Related

R Markdown - HTML Output in Powerpoint

I try to get Highouts Output in my Powerpoint presentation, but the suggestion does not work .This feature is still pretty new and I was not able to find something valueable in the docu.
---
title: "Untitled"
output:
beamer_presentation: default
powerpoint_presentation: default
ioslides_presentation: default
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(highcharter)
```
### R Markdown
This is an R Markdown presentation. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents. For more
details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that
includes both content as well as the output of any embedded R code
chunks within the document.
### Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
### Slide with R Output
```{r cars, echo = TRUE}
highchart() %>%
hc_add_series(data = mtcars, type = "bar", hcaes(y = cyl))
```
### Slide with Plot
```{r pressure}
plot(pressure)
```
Produces:
Functions that produce HTML output found in document targeting pptx output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
I added the always_allow_html: yes part to the top, but it still does not work. Can anyone help me?

Add markdown code chunk to R Markdown document

I would like to write an R Markdown document which presents code examples of how to write an R Markdown document. For example, I want to show in the document how to render text as bold.
`**this is bold**` will render 'this is bold' with bold text, i.e. **this is bold**
That works fine, the ` render the text within as code. However, I can't figure out how to get a code chunk to display properly. e.g.
```{markdown, eval=FALSE}
```{r}
x = rnorm(1)
```
```
This doesn't work because markdown isn't a supported language. I can't enclose the r code block in ` because I need that symbol to mark the beginning of the code chunk and it only works inline.
I can do some hoop jumping by actually using R
```{r, echo="FALSE"}
o = "```{r}\nx=sample(1)\n```\n"
cat(o)
```
which renders as
## ```{r}
## x=sample(1)
## ```
But this is more complicated for me writing the document and the code it generates doesn't allow for simple copy/paste.
Is there a native way to render as code the markdown necessary to create the R code block?
I found the bookdown book on Rmarkdown which has examples of R code chunks being rendered via Rmarkdown. The book source is available on GitHub and the relevant chapter is located here.
The chunk below works as desired. I can't explain why the `r ''` is required at the beginning of the block, but it is.
````markdown
`r ''````{r}
x = sample(1)
```
````
If you don't put {r} after the three backticks, rmarkdown will just pass the block on to markdown and it almost works. For example, this
```
```{r}
x = rnorm(1)
```
```
displays as
It's not quite right; the braces around the r have been removed. (I think r-markdown did that, not markdown.) I don't know if there's an option to force them in, but they will appear if there's a non-letter ahead of the r. You can put in a space, or (if you can figure out how) a zero width space, which R will display using "\u200B".
You can use the experimental knitrhooks package, which adds the chunk option chunk_head to enable us to keep the header. The package is in development on GitHub here.
To install the package, use the command devtools::install_github("nathaneastwood/knitrhooks"). Once installed, you can use the chunk hook by loading the package and then calling the function chunk_head() in your header.
Here is an example:
---
title: "Untitled"
output: pdf_document
---
```{r, include = FALSE}
library(knitrhooks)
chunk_head()
```
```{r, chunk_head = TRUE, echo = TRUE}
x <- sample(1)
```
Unfortunately, this will not display syntax highlighting, but as discussed within this issue, this is a limitation within how knitr processes the file.

Self-contained rmarkdown file contains only some embedded images

I'm seeing different behaviour in an R Markdown (Rmd) file depending on how I reference the image I'd like to embed in an HTML document. In the simple example below, the second image is embedded in the document, but the first (using the R chunk) is not.
---
title: "title"
output:
html_document:
mode: selfcontained
theme: null
---
```{r packages, echo=FALSE}
library(htmltools)
```
```{r imgTest, echo=FALSE}
img(src = "http://placehold.it/350x150")
```
<img src="http://placehold.it/350x150">
This is the output in the HTML (for the relevant bit):
<p><img src="http://placehold.it/350x150"/></p>
<p><img src="data:image/png;base64,iVBORw0KG<SNIPPED>"></p>
In summary, using the htmltools function img() within a code chunk does not embed the image but instead it remains a link.
For various reasons, I need the document to be truly self-contained (no links) and to also avoid raw HTML.
Can anyone explain why this is happening and offer a solution? I've tried a variety of chunk options without success so far.

Creating a footer for every page using R markdown

I'm writing a document in R Markdown and I'd like it to include a footer on every page when I knit a PDF document. Does anyone have any idea on how to do this?
Yes, this question has been asked and answered here: Adding headers and footers using Pandoc. You just need to sneak a little LaTeX into the YAML header of your markdown document.
This markdown header does the trick:
---
title: "Test"
author: "Author Name"
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy header}
- \fancyfoot[CO,CE]{And this is a fancy footer}
- \fancyfoot[LE,RO]{\thepage}
output: pdf_document
---
Works for me with an Rmd file in RStudio Version 0.98.1030 for Windows.
Another option would be to use the argument includes provided by rmarkdown::pdf_document() (documentation). This allows you to keep the footer in a separate file. If your footer is defined in footer.tex, the header of your R Markdown file would look like this:
---
output:
pdf_document:
includes:
after_body: footer.tex
---
This also assumes that footer.tex is in the same directory as the R Markdown file.
Update: The file footer.tex can contain any valid LaTeX that you want to be inserted at the end of your PDF document. For example, footer.tex could contain the following:
This \textbf{text} will appear at the end of the document.
To manage the height of the footer, you can use the following:
date: '`r paste("Date:",Sys.Date())`'
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \setlength{\footskip}{-50pt} # set the footer size
Keep Coding!

Comment out text in R Markdown (Rmd file)

In an R Markdown (.Rmd) file, how do you comment out unused text? I'm not referring to the text in the R code chunk, but the general texts, like % in LaTex for example.
I think you should be able to use regular html comments:
<!-- regular html comment -->
Does this work for you?
Extra yaml blocks can be used anywhere inside the document, and commented out with #
---
title: "Untitled"
output: html_document
---
No comment.
---
# here's a comment
# ```{r}
# x = pi
# ```
---
Note however that this does not prevent knitr from evaluating inline r code.
After drag the lines you want to make comment, press SHIFT+CMD+C (macOS), SHIFT+CTRL+C (Windows). This is the shortcut of R Markdown editor (R Studio) to comment out.
You can always turn off code by putting it within an if(F){} statement.