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

3

21

Background:
I like 'r'. I use Rstudio for it - it is a nice IDE. I use the revolution Analytics version of 'r', "revolution R Open".

I find that I type the same stuff in the annotation and structured programming regularly, and I want to save myself the re-typing.

Question:
How do I change the default file template so that the one I want, with some text already populated, comes up when I create a new blank R-script in Rstudio.

Clarifications:

  • I am not looking for this to be a manual process where I open one file, renaming it to a proper directory, and then work on it. I am looking to change the default so that this happens automatically.

Previous approach:

  • google
  • rstudio search (example)
  • search on stack-overflow
  • poking around rstudio menus/preferences

Thanks.

Clevelandclevenger answered 2/2, 2016 at 16:27 Comment(3)
@RichardScriven I don't think so. I suspect he is trying to use a template for creating new .R files, just like there is some default text for .Rmd files. AFAIK, this is currently not possible but would love to be proven wrong. This is very flexible in Eclipse+StatET combo.Envoy
@RichardScriven - I updated the question with a clarification. No, I am not looking for the highly manual solution. As the title says I am looking to change the default, so that when i hit CTRL + SHIFT + N, and a new file appears, it ALREADY has the text content that I have specified. Roman correctly identified the problem, and my be correct about the answer, but like him, I also hope that he is proven wrong.Clevelandclevenger
Would pressing CTRL + SHIFT + N and then typing some sort phrase or word? RStudio supports "snippets" so if you define a snippet that is what you want the default to be you could just type that right after creating a new file and it will populate with your template.Actinozoan
U
9

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

Upholstery answered 1/12, 2020 at 23:24 Comment(3)
Nice! I prefer non-hacks to hacks, when I have the choice. Where do those directories roll out for Windows machines?Clevelandclevenger
Right, AppData/Roaming/RStudio on Windows, and ~/.config/rstudio on other systems.Upholstery
So you make "templates" as a subdirectory of appdata, and put content there. I have code in my snippet for that gets name and puts it in the script name. $r rstudioapi::getActiveDocumentContext()[["path"]]$ and code that creates the date as well. $r paste(Sys.Date())$ it seems that doesn't work (yet) in the template.Clevelandclevenger
M
20

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.

Miss answered 2/9, 2016 at 14:59 Comment(4)
neat. I typed 'header' then hit tab. Snippet referenceClevelandclevenger
Is there a way to put the "now" or "today" in the date?Clevelandclevenger
@Clevelandclevenger see my edit. Does that answer your question? ;-)Miss
Great answer ! I like the fact that you can start with a blank .R file if needed, and only use the snippet when you need it. ThanksAbutilon
U
9

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

Upholstery answered 1/12, 2020 at 23:24 Comment(3)
Nice! I prefer non-hacks to hacks, when I have the choice. Where do those directories roll out for Windows machines?Clevelandclevenger
Right, AppData/Roaming/RStudio on Windows, and ~/.config/rstudio on other systems.Upholstery
So you make "templates" as a subdirectory of appdata, and put content there. I have code in my snippet for that gets name and puts it in the script name. $r rstudioapi::getActiveDocumentContext()[["path"]]$ and code that creates the date as well. $r paste(Sys.Date())$ it seems that doesn't work (yet) in the template.Clevelandclevenger
S
6

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.

Sneaky answered 31/1, 2022 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.