How to add default code snippets to blogdown posts? - r

I've set up my blog using blogdown.
Now I would like to define some standard code snippets that should be included in each post (e.g., for centering images). Take this R-code as an example; it should be included automatically in each post:
knitr::opts_chunk$set(
out.width = "70%",
fig.align = 'center'
)
I've read the section on templates in the blogdown book, but I could only find html based templates, which appears not so straightforward to me.
How can I add R-code snippets per default to posts using blogdown?

You can do this in .Rprofile, as explained in Section 1.4 of the blogdown book. The easiest way is probably to use an RStudio project, and create a .Rprofile file in the root directory of the website project, in which you set the knitr global chunk options.

Related

chunk code plot from rmd file not shown in the post of blogdown

When I use the blogdown and hugo academic create a website, one example post created by rmd file (2015-07-23-r-rmarkdown) has chunk code, but the plot of this chunk code did not show in the post see here. I can see the chunk code created figures in the figure_html folder though
besides, when insert image from addin in rstudio, it automatically provides a path: static/blog/2020-11-09-creating-a-website/index_files/1.JPG, however the image does not show in the post. When I use a path static/blog/2020-11-09-creating-a-website/1.JPG, it works. What could be wrong with the index_files folder?

Use Rmd files for content pages in blogdown website

I am considering switching my website to blogdown. My current set-up only uses .Rmd files with a less-than-ideal blog implementation. It seems like one advantage of blogdown is that it automates the blogging part of the website (posts, lists, dates, etc.) and allows RMarkdown. This is my main motivation to switch. However, the downside, I think, is that I cannot create content pages in RMarkdown (i.e., content > about.Rmd)... it seems this is limited to .md files. I would like to use .Rmd for all the pages so that I can incorporate r code. My question: Is it possible to create .Rmd content pages in blogdown?
EDIT...
Specifically, I created by hand research.Rmd in the content folder. I can include a trivial example, such as...
`r 2 + 2`
and when I run serve_site() the r code is not rendered. I can knit the file by hand, but the file does not have the style of the theme I am using and once I again try to serve the site the file is rendered as if it were a .md file.
```
EDIT 2...
You can reproduce the behavior I am referring to by doing the following...
Create a new blowdown site: new_site(dir = ".", theme = 'gcushen/hugo-academic')
Create new content: new_content('test/index.Rmd')
Set draft to false and add trivial r code: I used 2 + 2 as shown above.
Serve site and go to test/index.html. There is no r code rendered.
From your Edit 2, you seem to have fallen into a common Hugo trap: content/test/index.Rmd does not necessarily generate test/index.html. Hugo uses the convention _index.md to generate a homepage for a subfolder, and you have to use content/test/_index.Rmd in your case. For content/about.Rmd or content/research.Rmd, they will generate about/index.html and research/index.html respectively by default.

.Rmarkdown to .markdown: Use {{< figure >}} shortcode instead of <img> HTML

I've been looking into using blogdown for my existing Hugo blog and I think I've narrowed it down to one shortcoming. I'm using the .Rmarkdown file extension because I want to use the Blackfriday markdown processor to take advantage of Hugo features.
Among those is a customization I've added to my theme, which takes any image embedded with the built-in shortcode uses PhotoSwipe to make them appear in a lightbox on click. I've made it so that anything using the {{< figure >}} shortcode does this.
Is it possible either via blogdown or knitr (assuming knitr is part of the process of rendering the Rmarkdown) to customize the output of plots so they are wrapped in the shortcode rather than HTML tags? I think it might even be possible to do this with Go/Blackfriday if I could at least get the plots to be formatted in Markdown ![](/path/to/img.jpg), if that would somehow be easier.
There are two ways: either redefine the plot hook of knitr (requires more knowledge about knitr), or use the following trick:
```{r cars-plot, fig.show='hide'}
plot(cars)
```
{{< figure src="`r knitr::fig_chunk('cars-plot', 'png')`" >}}
In the above example, the plot was generated but hidden in the chunk output (fig.show='hide'; if you want hide the whole code chunk, use include=FALSE). Then its path is retrieved via the function knitr::fig_chunk() and inserted in the figure shortcode.

How to add feature or thumbnail image for post in .Rmd file

I'm currently trying to setup a hugo blog with blogdown and can't find a way to add feature or thumbnail images to posts from within .Rmd files, which would like this with the tranquilpeak theme:
As far as I understand, it is easy to do in .md files by just adding some syntax like this:
+++
featuredImage = "img/foobar.jpg"
+++
I found this in the minos theme and in a discourse post. But how can I do this inside a .Rmd file?
It does not matter whether you use .md or .Rmd: if the theme supports the featuredImage option, you can also use it in .Rmd. The only thing you need to make sure is to write metadata in YAML instead of TOML if the post format is .Rmd (see documentation), i.e.,
---
featuredImage: "img/foobar.jpg"
---
One option is to change the .Rmd filename to index.Rmd and use the featured.png/jpg image in the page's folder.
Works fine for me.

bookdown with specific latex template

I very much believe that Rmarkdown and bookdown are the future of content publishing, however some publishers use specific LaTeX templates, and in order to submit the manuscript they need it in that precise template.
For example, see this Springer manuscript template:
http://resource-cms.springer.com/springer-cms/rest/v1/content/20566/data/v3/monographs
Is it possible to make bookdown use this template in order to produce a PDF file?
EDIT: in this book (https://bookdown.org/yihui/bookdown/) on section 4.2 Theming I read it is indeed possible, but I might end up designing a "custom Pandoc LaTeX template".
I guess the question is now about how to design this Pandoc template, and section 4.3 provides some details about it.
You don't really have to use a custom Pandoc LaTeX template (of course you can if you want). Tweaking a few options and you will be done. I just put an example in the bookdown-demo repository. See this commit for what I changed, and see here for a PDF example.
You almost surely still have to tweak other things, one of which might be the index page. I also made an example for that.

Resources