1.10 Project documentation
A quick note or two about writing R code and creating R scripts. Unless you’re doing something really quick and dirty we suggest that you always write your R code as an R script. R scripts are what make R so useful. Not only do you have a complete record of your analysis, from data manipulation, visualisation and statistical analysis, you can also share this code (and data) with friends, colleagues and importantly when you submit and publish your research to a journal. With this in mind, make sure you include in your R script all the information required to make your work reproducible (author names, dates, sampling design etc). This information could be included as a series of comments
# or, even better, by mixing executable code with narrative into an R markdown document. It’s also good practice to include the output of the
sessionInfo() function at the end of any script which prints the R version, details of the operating system and also loaded packages. A really good alternative is to use the
session_info() function from the
xfun package for a more concise summary of our session environment.
Here’s an example of including meta-information at the start of an R script
# Title: Time series analysis of snouters # Purpose : This script performs a time series analyses on # snouter count data. # Data consists of counts of snouter species # collected from 18 islands in the Hy-yi-yi # archipelago between 1950 and 1957. # For details of snouter biology see: # https://en.wikipedia.org/wiki/Rhinogradentia # Project number: #007 # DataFile:'/Users/Another/snouter_analysis/snouter_pop.txt' # Author: A. Nother # Contact details: email@example.com # Date script created: Mon Dec 2 16:06:44 2019 ----------- # Date script last modified: Thu Dec 12 16:07:12 2019 ---- # package dependencies library(PopSnouter) library(ggplot2) print('put your lovely R code here') # good practice to include session information xfun::session_info()
This is just one example and there are no hard and fast rules so feel free to develop a system that works for you. A really useful shortcut in RStudio is to automatically include a time and date stamp in your R script. To do this, write
ts where you want to insert your time stamp in your R script and then press the ‘shift + tab’ keys. RStudio will magically convert
ts into the current date and time and also automatically comment out this line with a
#. Another really useful RStudio shortcut is to comment out multiple lines in your script with a
# symbol. To do this, highlight the lines of text you want to comment and then press ‘ctrl + shift + c’. To uncomment the lines just use ‘ctrl + shift + c’ again.
In addition to including metadata in your R scripts it’s also common practice to create a separate text file to record important information. By convention these text files are named
README. We often include a
README file in the directory where we keep our raw data. In this file we include details about when data were collected (or downloaded), how data were collected, information about specialised equipment, preservation methods, type and version of any machines used (i.e. sequencing equipment) etc. You can create a README file for your project in RStudio by clicking on the
New File ->
Text File menu.