Boris Leroy
Boris Leroy

Reputation: 500

Add raw / uninterpreted content to Rmarkdown file

I have prepared a .Rmd file with some R code, which I knit in Rstudio into a .md file. This .md file will, in turn, be pushed into a GitHub repository in order to produce a GitHub page (using the GitHub pages system) with a specific layout.

The issue I have is that when I specify the layout in my Rmarkdown file, e.g. with the following code:

---
title: "Untitled"
output: github_document
layout:mylayout
---

It does not appear in the .md file, and so the Github pages do not use my layout. To include my layout, I need to modify the .md file manually to add the following header:

---
title: "Untitled"
output: 
  html_document
layout:mylayout
---

I would like to not have to modify my .md file manually, i.e. to include the aforementioned header automatically when I knit my .Rmd file into a .md file.

I have unsuccessfully tried numerous methods, including print, cat, paste as well as writing r chunk codes with argument results="asis".

Is there a possibility to put raw content in a .Rmd which will not be interpreted at all?

Here is a very simple example of my .Rmd file:

---
title: "Untitled"
output: github_document
layout:mylayout
---

# I would like to add the next part at the beginning of my .md file,
# but it is always removed or altered such that it does not appear 'as is' in the .md
---
title: "Untitled"
output: 
  html_document
layout:mylayout
---

# After this header comes the content of the file
Content with R code

Upvotes: 0

Views: 92

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30124

GFM does not support YAML metadata, so github_document doesn't support it, either. Only md_document supports YAML, e.g.,

---
title: "Untitled"
output:
  md_document:
    variant: markdown_github
    preserve_yaml: true
layout: mylayout
---

But if you use Jekyll and Github Pages, you may consider using blogdown instead of knitting posts individually: https://bookdown.org/yihui/blogdown/jekyll.html Then you won't need to specify the output field in YAML. The output will be .md, and all your YAML metadata will remain untouched.

Upvotes: 1

Related Questions