An introduction to loadr

An introduction to loadr

Motivation

In a large, complex R analysis, I often find myself with dozens of resource data objects that I use as a reference for my computation. These are usually some kind of large dataset that I read into R and then use as reference data. As an example, I have a file with a big matrix with values from chromatin accessibility for 120 different cell-types across about a million different genomic regions (so, a 120-by-1 million matrix). I read this into my R sessions occasionally when I want to see, for example, how a new set of regions looks across that data resource. I'm not really changing the resource data, I'm just using it as a reference. I may load dozens of these kinds of resources into a single R session, and I want to re-use them across multiple scripts and multiple sessions, so I find myself reading them in a lot. I also start writing functions around these kinds of resources.

Given this complexity, sometimes it gets confusing for me to keep track of all the variables I keep in my primary R workspace, so I started to investigate how I could use R environments to declutter things. I discovered that I could load these kinds of "shared data reference" sets into a separate R environment, let's call it SV for "Shared Variables" -- and then they don't clutter my primary R workspace and I can still access them with SV$variable. This cleaned things up a bit for me and so I've developed a series of functions that make it really easy to interact with reference data like this kept in its own separate environment. That's what loadr is.

Before we start loading data, it's important to tell loadr the name of the environment where I want to store these reference objects. loadr uses a global variable (LOADRENV) to keep track of this environment name and provides a getter/setter function (loadrEnv()) to change or retrieve this. By default, we'll use an environment named SV. To make it explicit, just do this:

library(loadr)
loadrEnv("SV")

(This is optional, SV is the default environment name. You can also use this to name your shared variable environment something else). Now, let's generate some random data and load it into that environment with loadr functions:

eload(list(sampleData = rnorm(1e7, 0,1)))

Notice that the eload function takes a named list. The name of item in the list becomes the variable name of the object. If you now look at the R objects in your workspace, you won't see an object named sampleData, but you will see one named SV, which is a object of type environment:

ls()
class(SV)

The sampleData object has been put in the SV environment:

ls(envir=SV)

We can get that sampleData object back like this:

head(SV$sampleData)

If you have multiple objects to load at the same time, no problem:

eload(list(x=5, y=7))

The function has notified us that the x and y objects are newly loaded in to the shared variable environment. What happens if we try to load objects with the same name? They will get updated:

eload(list(x=526, y=234))
SV$x
SV$y

In practice, I write functions that load up data and then I want to load up the results of these functions. Let's define a function that will read data from a (potentially large) file:

loadMyData = function() {
    filePath = system.file("extdata", "mydata.txt", package="loadr")
    myData = read.table(filePath)
    return(myData)
}
eload(list(myvector=loadMyData()))
SV$myvector

Loading local variables or variably named variables with vload

What if I just want to load a local R variable, myLocalVar, under its current name? eload(myLocalVar) doesn't work because there is no named list of variables, so that will fail. It's possibe but a bit cumbersome to use eload(list(myLocalVar=myLocalVar)).

Here's where an alias called vload() can help:

myLocalVar = 15
vload(myLocalVar)

vload can accept any number of variables:

myLocalVar2 = 22
myLocalVar3 = 31
vload(myLocalVar2, myLocalVar3)

You can also use vload with a special named argument varNames to assign the objects to variably named variables. For example:

myLocalVar4 = 15
vload(myLocalVar2, myLocalVar3, varNames=c("var1", "var2"))

To see how this could be useful, let's imagine our data loading function depends on some variable which is script dependent, like the reference genome assembly:

loadMyData = function(genome) {
    filePath = system.file("extdata", "mydata.txt", package="loadr")
    myData = read.table(filePath)
    return(myData)
}

Let's load up some data from this function for a few different genomes at the same time:

genome="hg19"
vload(loadMyData(genome), varNames=paste0("myvector_", genome))

genome="mm10"
vload(loadMyData(genome), varNames=paste0("myvector_", genome))

An easier way to accomplish the same thing that will be to just lapply across our list of genomes:

genomes=c("hg19", "mm10")
res = lapply(genomes, function(genome) { 
    vload(loadMyData(genome), varNames=paste0("myvector_", genome))
    })


Try the loadr package in your browser

Any scripts or data that you put into this service are public.

loadr documentation built on April 17, 2021, 9:06 a.m.