A great way to improve your efficiency when using R and Displayr is by creating custom functions that you can reuse over and over. One of R's best features is its package system that makes it easy to share new code and functions. In this post, I'll show you how you can create a pseudo-package in Displayr to mimic the behaviour of an R package.

Man carrying packages

An R package is a collection of functions, documentation, data, and tests that makes it easy to share code and automate workflows. All but the simplest one line tasks should be written up as an R function if they need to be done repeatedly. This saves time and helps ensure your work is easy to reproduce at a later time or by a colleague. Once you have a collection of functions for related tasks, R has a great package system that allows you to bundle and share the collection. There are currently more than 12,000 packages on the Comprehensive R Archive Network (CRAN), and if you use R, you've no doubt made use of some of them via calling install.packages() and library(). Today, we'll see how we can create a pseudo-package in Displayr to mimic the features provided by an actual R package without the hassle of creating one.

Creating a pseudo-package

The approach we will use makes use of R's environments, a collection of objects (functions, vectors, etc.) similar to a package. A less elegant solution would be to make use of lists. We will write one function that defines all the functions needed for our pseudo-package, adds them to a single environment and then returns the environment.

I will demonstrate the approach with a simple example involving working with names. Imagine we are frequently working with data sets that have a name variable in the format "Family name, Given name" and a variable containing email addresses. To keep things simple, we won't worry about things like middle names and name suffixes. Let's assume our name variable has inconsistent capitalization, which we need to fix so that the first letter of both names is capitalized.

Our pseudo-package will have three functions, one to capitalize the names, one to combine a single name and email into a pretty format, and one that applies our formatting function to name and email variables . I'll make use of the person function in the utils package, which offers useful functionality for working with names.

CreatePseudoPackage <- function(){
    CapitalizeString <- function(x){
        s <- strsplit(x, " ")[[1]]
        paste(toupper(substring(s, 1, 1)), 
              tolower(substring(s, 2)),
              sep = "", collapse = " ")
    }

    FormatSingleName <- function(name, email){
        name.caps <- CapitalizeString(name)
        names.split <- strsplit(name.caps, ", ")[[1L]]
        format(utils::person(given = names.split[2],
                     family = names.split[1],
                     email = email))
    }

    FormatNames <- function(names, emails)
        mapply(FormatSingleName, names, emails)

    environment()
}

We can store this code in an R script on a file sharing service, such as Dropbox, so that other people in our organization can easily use the functions in their projects as well. The exact code above could be copied to an .R file and uploaded. Our collaborator could then use the functions by inserting an R Output into their document and using R's source function with the URL to the .R file.

Inside Displayr, we go to Insert > R Output and either use source() or paste in the above code into the code editor. In the last line of code, we create and name our pseudo-package:

mypkg <- CreatePseudoPackage()

With the pseudo-package created, we can now use it in future R Outputs with either R's attach or with functions.

with(mypkg, FormatNames(names, emails))

or

attach(mypkg)
FormatNames(names, emails)

where names and emails are the labels we've given for the names and emails in our Data Set.

Output from running pseudo-package function FormatNames

 

Try this yourself in Displayr, or check out more tips about using R in Displayr!