When using the JGR, the menu selections are translated into R commands that are visible in the console. The GUI however, only has the most common statistical methods defined. For more advanced statistical methods, commands need to be entered into the console manually. Frequently, a series of commands is required to obtain the required output. To enter multiple commands sequentially can be rather tedious. Furthermore, if a mistake is made, you will need to start again!

Generally, it is preferable to save the commands as a script file (a text file of sequential R commands) that can be opened in R. A new script file can be created in JGR by selecting the ‘New Script File’ icon form the menu bar: newA script can be opened by selecting ‘Open Script File’ icon: openand saved by selecting the ‘Save Script File’ icon:save

This page contains useful scripts that are used in this book.

To use a script, select the appropriate hyperlink below. This should open a new browser window that contains the script (code). Select ALL (Ctrl-A) the text and copy it to the clipboard (Ctrl-C).

Create a new script in JGR:newand paste (Ctrl-V) the contents of the clipboard into the script editor.

Save the file saveand give it an appropriate name in your preferred location.

To run the script; select all the text in the script editor (Ctrl-A), copy it to the clipboard (Ctrl-C), paste it into the console (Ctrl-V) and hit return.


Mode (calculate the mode of a variable)

BAplot (create Bland-Altman 1 plots)

ba_ggplot (create Bland-Altman 1 plots with ggplot)

nhs_number_validation (check if an NHS number is valid)

Dot Plot Data Function for comparison of genetic sequences. Returns a matrix with the first sequence as rows and second sequence as columns. Adapted from seqinr package 2.

KMatRisk (create Kaplan-Meier survival curves with numbers at risk)

DeducerSurvival (menu to create Kaplan-Meier survival curves)

It is however preferable to install the DeducerSurvival 3 function as a package from the CRAN website:

DeducerSurvival – CRAN

cool survival function

For this function, the RcmdrPlugin.KMggplot2, survival and ggfortify packages 4;5;6 should be installed, but there is no need to load the packages with the library command.

Export2Office (function by Tom Wenseleers that exports the active plot window directly to a Microsoft Powerpoint PPTX slide or a Microsoft Word DOCX file). The library ReporteRs 7 and it dependencies should be installed.


Pairwise alignment of DNA Please refer to a little book of R for bioinformatics 8

Plots as PDF, JPG or TIF (example on how to create pdf, jpg and tif files from plots created)

To create an SVG file, install the package svglite 9. Open the library and create a new file for the plot in the default directory (called Rplots.svg):


Now populate this file with a plot and close the file:


Group dates by year (example on how to group dates by year; use the data file dates.rda with this example)

Create a survival curve from dates; a data frame that contains the date of diagnosis and date of failure (example on how to convert dates to follow up time and how to create the censor variable; use survivaldates.rda with this example).

Revalue of map values (factors) with the plyr package 10 (for example month names from the first date of the month:


Convert incorrectly formatted data into appropriately declared variables that allow subsequent analysis.

It is a common error to group variables incorrectly and create a separate variable (column) for each group. However, variables should be in columns and the group is a separate variable. An example is show here.

Open an SPSS .sav file in R

Method 1:

  • Enter a1<-file.choose() in the console and maneuver to the file.
  • Activate the ‘foreign’ library: library(foreign)
  • dataset1<-read.spss(a1, reencode=’utf-8′,

This may give an error like: re-encoding from utf-8 Warning message: In read.spss(a1, reencode = “utf-8″, = TRUE) : Unrecognized record type 7, subtype 18 encountered in system file

Method 2:

  • Install the package ‘memisc’ 11
  • Enter library(memisc)
  • Enter a2<-file.choose() in the console and maneuver to the file.
  • enter: dataset2<

Solar Functions (Equation of Time)






Other functions

R Objects and Operators

Common R Functions

Helpful R Functions in Packages

Defining R Functions

Importing and Cleaning Data in R

Bland JM, Altman DG. Statistical methods for assessing agreement between two methods of clinical measurement. Lancet. 1986 Feb 8;1(8476):307–10.
Charif D, Clerc O, Frank C, Lobry JR, Necsulea A, Palmeira L, et al. seqinr [Internet]. 2017. (seqinr: Biological Sequences Retrieval and Analysis). Available from:
Ockendon M, Cool P. DeducerSurvival: add survival dialogue to Deducer. R package version 0.1 [Internet]. 2012. Available from:
Horikoshi M, Tang Y. ggfortify: Data Visualization Tools for Statistical Analysis Results [Internet]. 2017. Available from:
Sou T, Nagashima K. RcmdrPlugin.KMggplot2: R Commander Plug-in for Data Visualization with “ggplot2” [Internet]. 2016. Available from:
Therneau T. Survival: a package for survival analysis in R. R package version 2.37. 2014.
Gohel D. ReporteRs: Microsoft Word, Microsoft PowerPoint and HTML Documents Generation [Internet]. 2016. Available from:
Coghlan A. Welcome to a Little Book of R for Bioinformatics! [Internet]. Available from:
Wickham H, Luciani T, Decorde M, Lise V, Plate T, Gohel D, et al. svglite [Internet]. 2015. (A SVG Graphics Device). Available from:
Wickham H. plyr [Internet]. 2015. Available from:
Elff M, Lawrence CN, Atkins D, Morgan JW, Zeileis A. memisc: Tools for Management of Survey Data and the Presentation of Analysis Results [Internet]. 2016. Available from: