Changing default Rmd script

When I make a new markdown file it generates the header based on my input followed by default code that looks like this:

---
title: "Example Report"
author: "segreenb"
date: "7/30/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 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>.

...

And so on. What I want is to have the default look like this:

---
title: "Example Report"
author: "segreenb"
date: "7/30/2019"
output:
  pdf_document:
    toc: yes
  html_document:
    code_folding: hide
    df_print: paged
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(ggplot2)
library(dplyr)
library(readr)
library(reshape2)
library(knitr)
library(stringr)
library(tidyr)

```

```{r message=FALSE, echo=FALSE}
```

How can I do this?

1 Like

There isn't currently a way to change the default R Markdown template, but there is an existing feature request. You can contribute by voting with an emoji reaction here:

In the meantime, you can create your own templates and have them appear in the templates list within the new document dialog box. It's a bit more involved than one might expect (you need to make a package for the template to live in), but on the upside you'll learn a lot in the process! :grin: You can read all the details here:

Or try this tutorial:
https://ismayc.github.io/ecots2k16/template_pkg/

2 Likes

Another possibility will be to edit the r_markdown_v2.Rmd file.

In Windows, it is located in C:\Program Files\RStudio\resources\templates\r_markdown_v2.Rmd.

And where is that file or equivalent on OSX?

In RStudio 1.2.1335 on macOS, here's how you can find that file:

  1. In Applications, right-click on RStudio.app and select "Show Package Contents" (application packages are just fancy folders underneath)
  2. A new Finder window opens, and you are inside a folder called "Contents". The default template lives in: Contents/Resources/resources/templates/r_markdown_v2.Rmd

:exclamation: :rotating_light: Big Disclaimers:

  • Since you're editing a file that lives inside the application package, all your changes will disappear the next time you update RStudio. Be prepared to do this all over again each time you update.
  • You are always at risk of breaking things if you go fishing around inside application packages.
  • Future updates to RStudio could completely change the way this works, without warning.

I wonder if package ymlthis will be useful for this sort of thing in the long run. It appears you can write both the YAML header and the setup chunk with this package. It's not on CRAN at this point but the GitHub repository is here. I haven't tried it, but it might be worth checking out! :grinning:

In the introduction vignette section on workflows here it looks like you can put things into your .Rprofile as "an ad-hoc way to make R Markdown templates".

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.