Skip to content
Back to formatted view

Raw Message

Message-ID: <CCE952776B6679469977532BD863C39C3A8F4C70@Lewis.autuni.aut.ac.nz>
Date: 2013-02-28T22:40:50Z
From: Steve Taylor
Subject: using a personal template for new R scripts

Does anyone know if there's an easy facility to create and specify a template file to be used for new R scripts?

I found myself creating this function which works well (in RStudio on Windows).

newR = function(filename=tempfile(tmpdir='.',fileext='.R'), open=TRUE) {
  template = paste(Sys.getenv('R_USER'), 'R_template.R', sep='/')
  lines = readLines(template)
  lines = sub('Sys.time', format(Sys.time(), '%A, %d %B %Y'), lines)
  lines = sub('getwd', getwd(), lines)
  lines = sub('R.version.string', R.version.string, lines)
  writeLines(lines, filename)
  if (open) shell.exec(filename)
  filename
}

Rather than the default of an empty file, it would be good practice to start with a structured template script.  Such a template might contain opening comments (author, date, project folder, aims, inputs, outputs etc.) and section comment headings for the various components of what a script does (load packages, get data, process data, produce outputs).

cheers,
    Steve