DIY Header Comments in R

It is always a good idea to include a header at the start of your R scripts that provide a title, record the author, the date of writing and provide a description of what your code actually does! This makes it easy for someone else (or the future you!) to understand what you have done.

There is no need to manually type these out. header_comment() from commentr package prints a really nice header to the R console:

library(commentr)

header_comment("Amazing Program", "Does everything you could ever want!",
               author       = "John Roberts",
               contact      = "john.roberts@someemailprovider.com",
               client       = "A Big Firm",
               date_created = date(),
               width        = "a4landscape")

Try it!

Otherwise, if you would rather the header took a different form, you could write your own function. What follows is an example function I quickly wrote that uses cat() to print a header. Go ahead and change it to suit your styles!

# the function takes 5 arguments:
# title (required with no default)
# version (default: NULL)
# author (default: system user)
# contact (default: NULL) 
# description (default: NULL)

header = function(title=NULL,version=NULL,
  author=Sys.getenv('USERNAME'),
  contact=NULL,description=NULL){  
  # you must have a title
  if (is.null(title)){
    stop('You need a title!')
  } 
  # defines the bars
  bar = c(rep('#',4), rep('-',nchar(date())), rep('#',4),'\n')
  # what will print regarding version
  version.str = c('Version:', version,'\n')
  # what will print regarding contact
  contact.str = c('Contact:',contact,'\n')
  # what will print regarding description
  description.str = c('Description:',description,rep('\n',2)) 
  # if no version was entered, nothing will print
  if(is.null(version)){
    version.str = NULL
  }
  # same for contact
  if(is.null(contact)){
    contact.str = NULL
  }
  # same for description
  if(is.null(description)){
    description.str = NULL
  }
  # comment construction
  cat(  
    '',
    rep(x=bar,times=2),'\n', 
    'Title:',title,'\n',  
    version.str,
    'Date:',date(),'\n', 
    '\n',
    'Author:', author,'\n', 
    contact.str,'\n',
    description.str,
    rep(x=bar,times=2) 
  ) 
}

header(title="Amazing Program",version=2.1,author="John Roberts",
contact="john.roberts@someemailprovider.com",
description="Does everything you could ever want!")

Output:

 # # # # - - - - - - - - - - - - - - - # # # 
 # # # # - - - - - - - - - - - - - - - # # # 
 
 Title: Amazing Program 
 Version: 2.1 
 Date: Mon Jul 20 21:14:30 2015 
 
 Author: John Roberts 
 Contact: john.roberts@someemailprovider.com 
 
 Description: Does everything you could ever want! 
 
 # # # # - - - - - - - - - - - - - - - # # # 
 # # # # - - - - - - - - - - - - - - - # # #

References
https://cran.r-project.org/web/packages/commentr/index.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s