FAIR-ifying R Software: Basic Operation

Key Points

Introduction to RStudio
  • Using RStudio can make programming in R much more productive.

  • Objects are created on demand whenever a value is assigned to them.

  • applying the mean calculation across rows or columns has shortcuts, but other statistics may need ?apply or ?purrr.

  • Use plot to create simple visualizations.

Creating Functions
  • Define a function using name <- function(...arguments...) {...body...}.

  • Specify default values for arguments when defining a function using name = value in the argument list.

  • Call a function using name(arg1 = value, ...).

  • R looks for variables in the current stack frame before looking for them at the top level.

  • Make code more readable by passing arguments preferably by name.

  • Arguments can be passed by matching based on name, by position, or by omitting them (in which case the default value is used).

  • Use ?name or ??name to find the help page of a function.

  • Write formal roxygen2 comments for your functions and generate help pages from those.

Making Packages In R
  • A package is the basic unit of reusability in R.

  • Every package must have a DESCRIPTION file and an R directory containing code. We have to fill these manually.

  • A FAIR DESCRIPTION means adding rich metadata, best in a machine-readable format.

  • A NAMESPACE file is needed as well, and a man directory containing documentation, but both can be autogenerated.

  • RStudio provides a package skeleton, shortcuts like Build and install, and a Git pane.

Unit-Testing And Test-driven Development
  • Changing code is not always necessary, but often useful.

  • Tests provide a safety net for changing code.

  • Practice more TDD at exercism.io

Tidying & Packaging Datasets
  • Spreadsheets incentivise the wide data format, which may spread variables across columns.

  • If variables in different dataset are comparable methodologically, their variable/column names should be spelled exactly alike.

  • (Raw) Data can be packaged by itself (and/or alongside related cleaning and analysis code).

Basic Operation

List objects in current environment ls()

Remove objects in current environment rm(x)

Remove all objects from current environment rm(list = ls())

Control Flow

if(x > 0){
	print("value is positive")
} else if (x < 0){
	print("value is negative")
} else{
	print("value is neither positive nor negative")
for (i in 1:5) {

This will print:



is_positive <- function(integer_value){
	if(integer_value > 0){

In R, the last executed line of a function is automatically returned

increment_me <- function(value_to_increment, value_to_increment_by = 1){
	value_to_increment + value_to_increment_by

increment_me(4), will return 5

increment_me(4, 6), will return 10

apply(dat, MARGIN = 2, mean) will return the average (mean) of each column in dat



A value given to a function or program when it runs. The term is often used interchangeably (and inconsistently) with parameter.
call stack
A data structure inside a running program that keeps track of active function calls. Each call’s variables are stored in a stack frame; a new stack frame is put on top of the stack for each call, and discarded when the call is finished.
comma-separated values (CSV)
A common textual representation for tables in which the values in each row are separated by commas.
A remark in a program that is intended to help human readers understand what is going on, but is ignored by the computer. Comments in Python, R, and the Unix shell start with a # character and run to the end of the line; comments in SQL start with --, and other languages have other conventions.
conditional statement
A statement in a program that might or might not be executed depending on whether a test is true or false.
dimensions (of an array)
An array’s extent, represented as a vector. For example, an array with 5 rows and 3 columns has dimensions (5,3).
Human-language text written to explain what software does, how it works, or how to use it.
The practice of hiding something’s implementation details so that the rest of a program can worry about what it does rather than how it does it.
for loop
A loop that is executed once for each value in some kind of set, list, or range. See also: while loop.
function body
The statements that are executed inside a function.
function call
A use of a function in another piece of software.
function composition
The immediate application of one function to the result of another, such as f(g(x)).
A subscript that specifies the location of a single value in a collection, such as a single pixel in an image.
loop variable
The variable that keeps track of the progress of the loop.
notional machine
An abstraction of a computer used to think about what it can and will do.
A variable named in the function’s declaration that is used to hold a value passed into the call. The term is often used interchangeably (and inconsistently) with argument.
A connection from the output of one program to the input of another. When two or more programs are connected in this way, they are called a “pipeline”.
return statement
A statement that causes a function to stop executing and return a value to its caller immediately.
silent failure
Failing without producing any warning messages. Silent failures are hard to detect and debug.
A regular subsequence of a larger sequence, such as the first five elements or every second element.
stack frame
A data structure that provides storage for a function’s local variables. Each time a function is called, a new stack frame is created and put on the top of the call stack. When the function returns, the stack frame is discarded.
standard input (stdin)
A process’s default input stream. In interactive command-line applications, it is typically connected to the keyboard; in a pipe, it receives data from the standard output of the preceding process.
standard output (stdout)
A process’s default output stream. In interactive command-line applications, data sent to standard output is displayed on the screen; in a pipe, it is passed to the standard input of the next process.
Short for “character string”, a sequence of zero or more characters.
while loop
A loop that keeps executing as long as some condition is true. See also: for loop.