########################################### ########################################### ### ### Making R packages: ### a brief introduction ### ------------------------ ### Bob Muscarella ### Aarhus University ### May 7, 2015 ### ########################################### ########################################### ####################################### ###### 1. DEFINE A NEED ############## ####################################### ### We have these data: x <- sort(rnorm(100)) x ### But we want them scaled from 0-1: plot(x, pch=16, col=1); abline(h=c(0,1), lwd=4, lty=5, col=2) ### This function rescales a numeric vector to [0-1]: Rescale01 <- function(x){ (x - min(x, na.rm=T)) / (max(x, na.rm=T) - min(x, na.rm=T)) } # Use the function x2 <- Rescale01(x) x2 # Plot the results plot(x, pch=16); abline(h=c(0,1), lwd=4, lty=5, col=2) points(x2, pch=16, col=4) ############################################################ ### 2. SAVE THE FUNCTION AS AN R SCRIPT IN DESIGNATED FOLDER ############################################################ dir.create('package_files') # Create a folder in your working directory to hold your package files # Save your function to an R script in that folder sink("package_files/Rescale01.R") cat(" Rescale01 <- function(x){ (x - min(x, na.rm=T)) / (max(x, na.rm=T) - min(x, na.rm=T)) } ",fill=TRUE) sink() # You could source the function each time you need it: source("package_files/Rescale01.R") Rescale01 # Note, however, that there is no help file ?Rescale01 # Or, you could make it into an R package: library("MyPackage") # You will get an error because we haven't made MyPackage yet! ############################################################ ###### 3. STOP: SHOULD WE MAKE IT A PACKAGE? ############## ############################################################ ### If you decided to make an R package move forward ### Then make a skeleton for the package... # 3.1. Create a character vector to hold the path names of files of your package files <- character(length(list.files("package_files/"))) # 3.2. Fill the vector with the path names for(i in 1:length(files)){ files[i] <- paste(getwd(), "package_files", list.files("package_files/")[i], sep="/") } # 3.3. Use the package.skeleton function to build the skeleton for your package package.skeleton(name="MyPackage", code_files=files, path="package_files/") # 3.4. There are several other ways to create the package skeleton. For instance, see: ?package.skeleton ########################################### ###### 4. EDIT PACKAGE FILES ############## ########################################### # 1. Read and delete the “Read-and-delete-me” file. # 2. Edit the DESCRIPTION file. # 3. Edit the .Rd (help) files in the MAN (manual) folder. # 4. If necessary, edit the NAMESPACE file. # 5. Other edits may be needed for more complicated packages... ######################################## ###### 5. BUILD THE PACKAGE ########### ######################################## # Open a Terminal window on Mac, or a cmd window on PC # Change the directory to the location of your package files using "cd" # Type: "R CMD BUILD MyPackage" where 'MyPackage' is your package name # You should see a file called "MyPackage_1.0.tar.gz" appear in your package folder # This is the source code for your package!!! ######################################### ###### 6. CHECK THE PACKAGE ########### ######################################### # In the Terminal (or cmd window), type: "R CMD check --as-cran MyPackage_1.0.tar.gz" # Substitute 'MyPackage' for your package name and the correct version number ############################################### ###### 7. LOAD FINISHED PACKAGE ############## ############################################### ## install.packages("MyPackage") ## library(MyPackage) ######################################################## ###### 8. SUBMIT TO CRAN (or other repository) ############## ######################################################## # http://cran.r-project.org/submit.html ################################# ###### 9. OTHER RESOURCES ###### ################################# ### The official and ***Essential*** guide: # http://cran.r-project.org/doc/manuals/r-release/R-exts.html ### Other great guides and information: # http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf # http://r-pkgs.had.co.nz/