One of the ways that you can use R in Displayr is to create custom functions to do specific things multiple times in the document without having to rewrite the full code each time. You can create custom R functions in various ways, including standalone outputs within a document, see How to Create Custom R Functions. This article describes how to share custom R functions in Displayr as a pseudo-package across multiple documents without the need to manually copy and paste or recreate them. This is a more advanced, and secure, method to sharing functions across documents, versus just copying and pasting the Calculations with the custom functions from one document to another.
Requirements
Please note these steps require a Displayr license.
- A dataset loaded in a document.
- Either a Professional or Enterprise user license.
- One or more custom R functions created via Calculation
> Custom Code from the toolbar.
In this example, we have 2 functions for working with names which culminate in a third FormatNames
function. These are designed to address issues with inconsistent capitalization and then format the name and email address.
Method - Create your pseudo-package
The basic format for a pseudo-package is to create a function that defines all the functions needed, that is to wrap all your functions within another function:
Pseudo_package_name <- function() {
CustomFunction1 <- function(x){
...
}
CustomFunction2 <- function(x){
...
}
environment()
}
We additionally need to add them to a single environment and then return this using the environment()
command.
Here, our pseudo-package is called CustomFunctions()
:
CustomFunctions <- 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()
}
Method - Save your custom functions to the Displayr Cloud Drive
Add the below line to the end of your Calculation:
flipAPI::QSaveData(CustomFunctions,"CustomFunctions.rds")
This uses the QSaveData
function to save our CustomFunctions
pseudo-package as CustomFunctions.rds
in the Displayr Cloud Drive.
See How to Access the Displayr Cloud Drive Using R for details on how to use R code for importing and exporting to the Cloud Drive.
Method - Add your custom functions to another document
1. Open another document where you want to use your custom functions.
2. Select a page to import your CustomFunctions
pseudo-package and tick Hidden from exports & publishing in the object inspector.
3. In the toolbar, select Calculation > Custom Code > click your page.
4. Paste the below into the R Code field:
CustomFunctions = flipAPI::QLoadData("CustomFunctions.rds")
5. Press Calculate.
Note, if you update the original saved file in the Cloud Drive and wish to then update it in this document, you simply need to press Calculate again.
Method - Use these custom functions in your document
1. Select Calculation > Custom Code from the toolbar on a page where you wish to use the
CustomFunctions()
functions.
2. Insert the following line so Displayr can then call these functions within this calculation:
attach(CustomFunctions())
3. Now enter the function name and its arguments:
FormatNames("John Smith","john.smith@test.com")
4. The code will then return the below as if the function was written in the same document:
John Smith <john.smith@test.com>