Reading time: 2 minute(s) @ 200 WPM.
Yes, it doth work. The blogdown package can nicely deal with R Markdown containing embedded R code for production of statistical analyses and graphics.
But.
It cannot deal with R Notebooks, which contain a special flavor of R Markdown allowing for interactive display of R code alongside the results they produce. R Notebooks, when rendered into HTML, produce a special file named *.nb.html
to signify that they are an HTML notebook. These simply don’t work inside of a Hugo-rendered website.
We hope that the developers of the blogdown
package will provide support for R Notebooks.
In the meantime, we will have to produce our R Notebooks in the usual way, stash them somewhere, and just link to them.
Like here.
And if you want to include Hugo shortcodes in an R Markdown (.Rmd
) file, do as follows. Shortcodes are enclosed in double curly braces, and can be inserted directly into regular Markdown files (.md
), but for R Markdown they need to be wrapped in the HTML
function from the htmltools package. (See Support for Hugo Shortcodes in Rmd files.)
So this shortcode will insert one of my tweets:
r htmltools::HTML("{{< tweet 853356395164557317 >}}")
Or, we could insert an image using the figure
shortcode instead of the usual Markdown figure tag (![optional text](/path to image/image.png)
) because it gives us the ability to add titles, captions, etc:
r htmltools::HTML(‘{{< figure src=“/img/willie.png” title= “Willie reclining at leisure.” caption=“The domestic cat (Latin: Felis catus) is a small, typically furry, carnivorous mammal. They are often called house cats when kept as indoor pets or simply cats when there is no need to distinguish them from other felids and felines. Cats are often valued by humans for companionship and for their ability to hunt vermin. There are more than 70 cat breeds, though different associations proclaim different numbers according to their standards.” >}}’)
This yields:
Note that in the above figure
short code, because the internal strings for figure src
, title
, and caption
are enclosed in double quotes, we have to enclose the whole shortcode in single quotes, or else we’ll get an unexpected string constant
error when we render the website.
The rest of this example is lifted directly (slightly edited) from the R Markdown template that is provided by RStudio when you create a new R Markdown document from the File|New File
menu.
R Markdown
This is an R Markdown document. 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. You can embed an R code chunk like this:
```{r cars, comment=""}
summary(cars)
```
And the output of the code chunk is:
speed dist
Min. : 4.0 Min. : 2.00
1st Qu.:12.0 1st Qu.: 26.00
Median :15.0 Median : 36.00
Mean :15.4 Mean : 42.98
3rd Qu.:19.0 3rd Qu.: 56.00
Max. :25.0 Max. :120.00
Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.