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.
Related
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>
I have a summary figure that I want to insert at the top of my HTML report generated using knitr/rmarkdown. But the inputs of this figure depend on outputs generated from downstream code chunks and child documents. Is there a way to do this without having to put the summary figure at the end of the document?
I have tried ref.label='last' option in the top level code chunk (with eval=FALSE) to add a duplicate code chunk in the end of the document {r last, eval=TRUE}, but has not worked for me.
Any suggestions?
You can insert HTML anywhere into your document referencing the figure by the name knitr will use for it.
To figure out the name, temporarily change the YAML header to something like this:
output:
html_document:
self_contained: FALSE
This won't embed figures in the output, they'll be kept in a separate directory. Open the output in an editor, or view source in a browser, and look for the line that displays the figure. In my test example below, it was being displayed by this code:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
I copied that line into the document, and removed the self_contained setting, and got this source code:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This is the figure before it was generated:
<p><img src="Untitled2_files/figure-html/rnormplot-1.png" width="672" /></p>
And here it is being generated:
```{r rnormplot}
x <- rnorm(100)
plot(x)
```
One key is to name the code chunk that generates the plot (mine is named rnormplot) because that helps to make sure the figure name is independent of the rest of the document.
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?
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.
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.