How to set default template for new ".R" files in rstudio

Quite late and not really a template but I think the solution is close: go to

Tools => Global Options => Code => Tab Editing => Snippets "Edit Snippets".

Example:

snippet header2
    # Author:
    # Date: `r paste(date())`
    # --------------
    # Author:
    # Date:
    # Modification:
    # --------------

If you then type header {snippet} in a new script you get the text above with the date inserted automatically.


As you are asking specifically for a Windows solution, you create a templates folder (AppData/Roaming/RStudio/templates) and edit the default.R file.

# Create a templates folder
fs::dir_create(path = "~/AppData/Roaming/RStudio/templates")

# Create the file
fs::file_create("~/AppData/Roaming/RStudio/templates/default.R")

# Open the file in RStudio to edit it
usethis::edit_file("~/AppData/Roaming/RStudio/templates/default.R")

Now you can save the populated file and have created your new default R Script. Another possibility to not only keep this default template on your local machine would be to write a package for the sole purpose of containing a set of standardized default templates. I've written a short post about it here.


It was made possible to define user and system-wide templates for several file types with rstudio v1.3

A few gotchas:

  1. it is necessary to create a 'templates' folder inside the .config directory
  2. specific filetypes must be named according to this scheme

so, in this case, create a file called ~/.config/rstudio/templates/default.R or /etc/rstudio/templates/default.R for a user or system-wide .R file template, respectively.

I think this a 'less manual' solution than the accepted answer