Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing user-created functions in R from seperate scripts

Tags:

r

I'm trying to re-use some code that I've already written but often need to re-execute for various projects (IE I'd like to apply some Object-Oriented principles to my R code). I know that a framework exists for publishing new packages on CRAN, but the code I have isn't something that would be valuable for other parties.

Essentially I'd like to either create my own local packages and reference them using a require() call or at the very least call functions that I've saved in separate .r files as-needed.

I've searched around online and found several lengthy articles about creating packages and compiling them using RTools (I'm on a Windows OS) but since I'm not writing C this seems overkill for my simple purposes. To offer an example of what I'm referring to, I have a script to remove unwanted characters from string data that I constantly need to copy/paste into new scripts; I don't want to do this and would prefer to just do something like require(myFunction).

Is there a simple way to solve this problem or am I best served by grabbing RTools and compiling my custom functions locally?

like image 838
Sevyns Avatar asked Dec 13 '25 17:12

Sevyns


1 Answers

Creating an R package is actually super easy. The link from Alex is how I started my first package. Here's a slightly simplified version I have to give my students. (NB: full credit to Hilary Parker, the author of the original blogpost).

First install devtools and roxygen:

install.packages("devtools")
library("devtools")
install.packages("roxygen2")
library("roxygen2")

Make a new directory for your functions:

setwd("/path/to/parentdirectory")
create("mypackage")

Add your functions to a file (or files) named anything.R in the R directory. The file should look like this, you can have one function per file, or multiple:

mymeanfun <- function(x){
       mean(x)
}

myfilterfun <- function(x, y){
       filter(x, y)
}

Now you should document the code. You can document (and import) using roxygen. Make sure you @import functions from any other packages, and @export the functions you want available. Roxygen and devtools will take care of everything else (namespace, requires etc etc.) until you get more advanced. Everything else is optional:

#' My Mean Function
#'
#' Takes the mean
#' @param x any default data type
#' @export
#' @examples
#' mymeanfun(c(1,2,3))
mymeanfun <- function(x){
       mean(x)
}
#' My Filter Function
#'
#' Identical to dplyr::filter
#' @param x a data.frame
#' @export
#' @importFrom dplyr filter
myfilterfun <- function(x, y){
       filter(x, y)
}

Now run the document() from roxygen2 in the directory you created:

setwd(".\mypackage")
document()

You are now up and running - I'd recommend putting it on github and installing from there:

install_github("yourgithubname/mypackage")

From then on, you can just call:

library(mypackage)

Every time you need your functions.

For more details and better documentation practices, see Hadley's book

like image 169
jeremycg Avatar answered Dec 16 '25 10:12

jeremycg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!