How to Create a Pseudo-Package in Displayr
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.
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.
Try this yourself in Displayr, or check out more tips about using R in Displayr!