Introduction
This article describes how to share custom R functions across your organization's Q projects without the need to manually copy, paste, and recreate them. This can work similarly to loading packages in R, but is more of a pseudo-package of your custom code.
Requirements
- A cloud drive with security permissions set to allow file-sharing/download to anyone with a link.
- An R source file (.R) of the pseudo-package code per the specifications below.
Method
1. Outside Q, write the R source file containing the code needed. The basic format for a pseudo-package is to create a function that includes all the functions and code that you need (keep in mind the caveats of using R in Q). At the end, the function will need to return the environment object with everything loaded. Here's the general framework:
Pseudo_package_name <- function() {
CustomFunction1 <- function(x){
...
}
CustomFunction2 <- function(x){
...
}
environment()
}
The example code we'll use is as follows:
#create a master function to encompass all of the custom functions
loadmypackage <- function(){
#include custom functions - this one prints "Yes"
doesthiswork <- function(){
print("Yes")
}
#finally, return the environment object with the custom functions loaded
environment()
}
2. Save your .R source file to your cloud drive.
3. Create a share link that can be accessed by anyone and not just those within your organization. The link will need to download the file automatically. For example, if you're using Dropbox, you will need to edit the ending of your link from dl=0 to dl=1 similar to: https://www.dropbox.com/s/xxxxxx/testfunctioncloud.R?dl=1
4. We'll create an R Output in Q to read in your .R file from the cloud and load the functions. In your Q project, right-click on the Report tree and select Add R Output.
5. Paste in code similar to below, where your download link is inside the quotes and loadmypackage() is the name of the master function in your .R source file:
#read in the source code from the cloud
source("https://www.dropbox.com/s/xxxxxx/testfunctioncloud.R?dl=1")
#call the pseudo-package and set it to a local variable
mypseudopackage=loadmypackage()
The above code names your R Output mypseudopackage, which you can call from other R Outputs and R Variables in the project.
6. Click Calculate if the R Output doesn't calculate automatically. You should see the word environment printed on the output similar to below:
7. To use your custom functions in a new R Output. Right-click on the Report tree and select Add R Output.
8. Use the attach() function to load the R Output environment with your functions to the new R Output (or R variable). The code below does this and runs the doesthiswork() custom function to test that everything loads correctly:
#attach the R Output with the functions/environment variable to load the functions
attach(mypseudopackage)
#test a function from the package
doesthiswork()
The doesthiswork() function prints Yes to the output. You'll see this is used successfully here:
See Also
How to Reference Different Items in Your Project in R