Creating your first R package

Video recording for this session




Table of Contents









































































1. Anatomy of a R Package

find.package("package_name")
find.package("ggplot2")

[1] "/Library/Frameworks/R.framework/Versions/4.0/Resources/library/ggplot2"
setwd("/Library/Frameworks/R.framework/Versions/4.0/Resources/library/ggplot2")

list.files()

 [1] "CITATION"    "data"        "DESCRIPTION" "doc"         "help"       
 [6] "html"        "INDEX"       "LICENSE"     "Meta"        "NAMESPACE"  
[11] "NEWS.md"     "R"  
setwd("R")

list.files()

[1] "ggplot2"     "ggplot2.rdb" "ggplot2.rdx"
setwd("../")

system("cat NAMESPACE")

# Generated by roxygen2: do not edit by hand

S3method("$",ggproto)
S3method("$",ggproto_parent)
S3method("$<-",uneval)
S3method("+",gg)
...
importFrom(stats,setNames)
importFrom(tibble,tibble)
importFrom(utils,.DollarNames)




2. Ways to publish your package




3. Let’s create a package

One of the motivations behind creating a package is to be able to use a given function with ease, instead of having to hunt down the relevant code to repeat the analysis. Let’s walk through a few functions to see how they work:

3.1 Function to print numbers

printnum <- function(n){
    n <- readline("How many numbers do you want to print? ")
    print(1:n)
}

printnum()

How many numbers do you want to print? 25

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25



3.2 Function to convert temperature

f2c <- function(tempF){
    tempC <- (tempF - 32) * 5/9
    return(tempC)
}
f2c(32)

[1] 0


f2c(88)

[1] 31.11111



3.3 Function to generate genetic data

popdata <- function(allele1, allele2, popsize){
    allele1 <- readline("Provide name of the first allele - one alphabet only: ")
    allele2 <- readline("Provide name of the second allele: ")
    popsize <- readline("How many individuals in your population?: ")

    print("Possible diploid genotypes are:")
    print(paste0("First Homozygote: ", allele1, allele1, sep=""))
    print(paste0("Heterozygote: ", allele1, allele2, sep=""))
    print(paste0("Second Homozygote: ", allele2, allele2, sep=""))

    homoz1 <- paste(allele1, allele1, sep="")
    hetz <- paste(allele1, allele2, sep="")
    homoz2 <- paste(allele2, allele2, sep="")

    pop <- sample(c(homoz1, hetz, homoz2), popsize, replace=TRUE)

    print("Your sampled population is stored in object 'pop' and also printed below:")
    print(pop)
}
Provide name of the first allele - one alphabet only: A
Provide name of the second allele: D
How many individuals in your population?: 100
[1] "Possible diploid genotypes are:"
[1] "First Homozygote: AA"
[1] "Heterozygote: AD"
[1] "Second Homozygote: DD"
[1] "Your sampled population is stored in object 'pop' and also printed below:"
  [1] "AD" "DD" "DD" "DD" "DD" "DD" "AD" "AA" "AD" "AA" "DD" "AA" "AD" "DD" "AD"
 [16] "DD" "AA" "AA" "AA" "AA" "AD" "DD" "AD" "AD" "AD" "DD" "AA" "DD" "DD" "AA"
 [31] "AD" "DD" "AD" "DD" "AD" "DD" "AD" "AD" "AD" "AA" "DD" "DD" "AA" "AA" "DD"
 [46] "DD" "DD" "AA" "DD" "DD" "DD" "AA" "DD" "AD" "AA" "AA" "AD" "AA" "DD" "AD"
 [61] "DD" "AD" "DD" "DD" "AA" "DD" "DD" "AD" "AD" "AA" "AA" "DD" "DD" "AA" "AA"
 [76] "AD" "AD" "AA" "AD" "AA" "AA" "AD" "AA" "AD" "AD" "AD" "AA" "AA" "AA" "AA"
 [91] "DD" "AD" "AD" "AD" "DD" "AA" "AA" "DD" "AA" "AD"



3.4 Package creation setup



3.5 Framework for your package

setwd("~/Github")

devtools::create("popdata")

✔ Creating 'popdata/'
✔ Setting active project to '/Users/vikram/Dropbox/Github/popdata'
✔ Creating 'R/'
✔ Writing 'DESCRIPTION'
Package: popdata
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R (parsed):
    * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to
    pick a license
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
✔ Writing 'NAMESPACE'
list.files()

[1] "DESCRIPTION" "NAMESPACE"   "R"



3.6 Add your R functions

cd ~/Github/popdata/R

vim popdata.R
#' @export
#' @export

popdata <- function(allele1, allele2, popsize){
    allele1 <- readline("Provide name of the first allele - one alphabet only: ")
    allele2 <- readline("Provide name of the second allele: ")
    popsize <- readline("How many individuals in your population?: ")

    print("Possible diploid genotypes are:")
    print(paste0("First Homozygote: ", allele1, allele1, sep=""))
    print(paste0("Heterozygote: ", allele1, allele2, sep=""))
    print(paste0("Second Homozygote: ", allele2, allele2, sep=""))

    homoz1 <- paste(allele1, allele1, sep="")
    hetz <- paste(allele1, allele2, sep="")
    homoz2 <- paste(allele2, allele2, sep="")

    pop <- sample(c(homoz1, hetz, homoz2), popsize, replace=TRUE)

    print("Your sampled population is stored in object 'pop' and also printed below:")
    print(pop)
}



3.7 Document your function


#' popdata() function by FirstName LastName
#' This function takes input from the user on allele names and population size
#' It then prints out the genetic variation data
devtools::document()

Updating popdata documentation
ℹ Loading popdata
Writing NAMESPACE
Writing NAMESPACE



3.8 Install your package

devtools::install()

✔  checking for file ‘/Users/vikram/Dropbox/Github/popdata/DESCRIPTION’ ...
─  preparing ‘popdata’:
✔  checking DESCRIPTION meta-information
─  checking for LF line-endings in source and make files and shell scripts
─  checking for empty or unneeded directories
─  building ‘popdata_0.0.0.9000.tar.gz’
   
Running /Library/Frameworks/R.framework/Resources/bin/R CMD INSTALL \
  /var/folders/8z/8vr45rz94t95_gn426z64f5w0000gn/T//Rtmpqd3Prl/popdata_0.0.0.9000.tar.gz \
  --install-tests 
* installing to library ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library’
* installing *source* package ‘popdata’ ...
** using staged installation
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (popdata)
?popdata

popdata                package:popdata                 R Documentation

popdata() function by FirstName LastName This function takes input from
the user on allele names and population size It then prints out the
genetic variation data

Description:

     popdata() function by FirstName LastName This function takes input
     from the user on allele names and population size It then prints
     out the genetic variation data

Usage:

     popdata(allele1, allele2, popsize)
     

(END)
popdata()

Provide name of the first allele - one alphabet only: M
Provide name of the second allele: N
How many individuals in your population?: 100
[1] "Possible diploid genotypes are:"
[1] "First Homozygote: MM"
[1] "Heterozygote: MN"
[1] "Second Homozygote: NN"
[1] "Your sampled population is stored in object 'pop' and also printed below:"
  [1] "NN" "NN" "MN" "MN" "MN" "MM" "NN" "NN" "MM" "NN" "MM" "MM" "MM" "MM" "MM"
 [16] "MM" "MN" "MM" "MN" "MN" "MN" "MM" "NN" "MN" "MN" "NN" "NN" "NN" "MM" "MN"
 [31] "MN" "MM" "NN" "MN" "MN" "MN" "NN" "MM" "MM" "MM" "MM" "NN" "MM" "NN" "NN"
 [46] "MM" "NN" "MN" "NN" "NN" "NN" "MN" "MM" "NN" "MN" "MM" "MN" "MN" "NN" "MN"
 [61] "NN" "NN" "NN" "NN" "MN" "MM" "NN" "MM" "MM" "MN" "MN" "MN" "MN" "MN" "MM"
 [76] "MN" "MM" "MN" "MM" "MN" "MM" "MN" "NN" "MN" "MN" "MN" "NN" "MM" "MM" "NN"
 [91] "MN" "MM" "MM" "MN" "NN" "MN" "MM" "MM" "NN" "MM"




4. Distributing your package



4.1 How others will install your package

library(devtools)

devtools::install_github("YOUR_USER_NAME/popdata")




5. Exercise