Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a default comment header template in R?

Tags:

r

Is it possible to create a default comment header template in R for all new scripts?

I generally include some standard info at the top of all my scripts and would like to automate the process of creating the comment header.

For example:

##################################################
## Project:
## Script purpose:
## Date:
## Author:
##################################################
like image 750
TClavelle Avatar asked Nov 15 '16 17:11

TClavelle


People also ask

How do I make a header in R?

We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.

How do you make a comment in R?

Comments starts with a # . When executing code, R will ignore anything that starts with # .

How do I make a comment block in R?

The easiest way to create a multi-line comment in RStudio is to highlight the text and press Ctrl + Shift + C. You can just as easily remove the comment by highlighting the text again and pressing Ctrl + Shift + C.


Video Answer


2 Answers

Following the suggestion from @lmo above, I created two new code snippets in RStudio by editing the snippets file located at Preferences > Code > Edit Snippets

Note: Code below the snippet definition must be indented using tab and not two spaces

Snippet 1: Comment header for new scripts

snippet header_script
  ##################################################
  ## Project:
  ## Script purpose:
  ## Date:
  ## Author:
  ##################################################

Snippet 2: Comment header for sections of code within a script:

snippet header_section
  ## Section:
  ##################################################

Now, whenever I want to insert a given header, I just start typing the snippet name (e.g. header_script), select it from the autocomplete menu (do not type out in full), and hit enter to insert the comment header.

like image 133
TClavelle Avatar answered Sep 17 '22 17:09

TClavelle


I don't know if you can do it directly, but you may be able to approximate it by putting the following in your Rprofile.

make_r_template <- function(filename = "r_template.R", dir = getwd())
{
  if (file.exists(file.path(dir, filename))) invisible(NULL)
  else{
    write(c("##################################################",
            "## Project:",
            "## Script purpose:",
            "## Date:",
            "## Author:",
            "##################################################"),
          file = file.path(dir, filename),
          sep = "\n")
  }
}

make_r_template()

This will run every time R starts and write the blank template to the working directory, so long as it doesn't exist. You could also run the function at any point with a different filename value to create a blank template elsewhere.

A nice supplemental touch would be a second function that looks at a file, attempts to identify the header, and inserts it if the header is not found.

like image 36
Benjamin Avatar answered Sep 21 '22 17:09

Benjamin