Package
.libPaths() # Get library locations containing R packages

View(available.packages()) # list the available package at CRAN

View(install.packages(packageName)) # install a package

# check installed libraries
installed.packages()
library()

library(ggplot2) # load ggplot2 library
search() # list the loaded libraries

# call functions defined in a package
devtools::install()

or

library(devtools) # add devtools into search list
install()

# check functions defined in a package
ls("package:myPackage")

# access help information
help(getRand, package = "myPackage")
help(getRand)
??myPackage::getRand # search functions starting with getRand
?getRand # get help document for getRand
		
Use Functions Defined in a File
# p1.r
getRand <- function(n)
{
	return(runif(n))
}
		
# main.r
source("p1.r")

print(getRand(10))
		
Create Package
  • myPackage.Rproj, RStudio project file
  • .Rbuildignore, exclude a specific file or directory
  • .Rproj.user, used for temporary files
  • DESCRIPTION, metadata
  • NAMESPACE, helps encapsulate your package and makes it self-contained
  • # DESCRIPTION
    Package: myPackage
    Title: What the Package Does (One Line, Title Case)
    Version: 0.0.0.9000
    Authors@R: 
        person(given = "Lin",
               family = "Chen",
               role = c("aut", "cre"),
               email = "lin.chen@ieee.org")
    Description: What the package does (one paragraph).
    License: What license it uses
    Encoding: UTF-8
    LazyData: true
    RoxygenNote: 6.1.0.9000
    Imports:
        ggvis (>= 0.2), # automatically download and install the required packages
        dplyr (>= 0.3.0.1)
    Suggests:
        MASS (>= 7.3.0)
    		
    # 1. Install needed packages
    install.packages("devtools")
    library("devtools")
    devtools::install_github("klutometis/roxygen")
    library(roxygen2)
    		
    # 2. Create package
    usethis::create_package("myPackage")
    		

    # 3. Add functions
    getRand <- function(n)
    {
    	return(runif(n))
    }
    		

  • Necessary description
  • Extra stuff
  • # 4. Add comments
    #' getRand Function
    #'
    #' get a sequence of random numbers between 0 and 1
    #' @author Lin Chen, \email{lin.chen@ieee.org}
    #' @references \url{https://lin-chen-va.github.io/}
    #' @param n the number of random numbers
    #' @keywords cats
    #' @export
    #' @examples
    #' v = getRand(10)
    getRand <- function(n)
    {
    	return(runif(n))
    }
    		
    # 5. Process your documentation
    library("devtools")
    setwd("./myPackage")
    document()
    		

    # 6. Generate documents
    build_site(path = ".", quiet = TRUE) # optional
    		

    # 7. Push the package to your github repository
    # 8. Install package
    install_github("lin-chen-VA/myPackage")
    
    or
    install("myPackage") # install from the parent directory of myPackage
    		
    # 9. Call functions defined in package
    getRand(10)
    myPackage::getRand(10) # call function with namespace
    		
    Create Binary Packages
    devtools::build(binary = TRUE) # in the package folder
    R CMD INSTALL packagename myPackage_0_0_0.9000.tgz # install from command line
    install.packages("myPackage_0.0.0.9000.tgz", repos = NULL) # install in interactive environment
    		
    Reference
  • R packages
  • Package metadata
  • Writing an R package from scratch
  • Introduction to roxygen2
  • Writing documentation with Roxygen2