Aya Mitani
January 19, 2021
dplyrggplot2For yourself
For others
devtools & roxygen2 & usethis packagesRtools package (https://cran.r-project.org/bin/windows/Rtools/)
here availableinstall_github()There is more than one way!
available package – see next slide)Check to see if the name is unique, especially if you plan to submit your package to CRAN
install.packages("available")
library(available)
available("ayapack", browse = FALSE)
Create a git repository”document() (see next slide)library(devtools)
library(roxygen2)
library(usethis)
roxygen2 comments (see next slide)
#' @title inverse logistic link function
#' @description a function that returns the inverse of the logistic link function
#' @param x numeric vector
#' @return y
#' @author Aya Mitani
#' @examples
#' expit(2)
#' @export
expit <- function(x){
y <- exp(x) / (1 + exp(x))
return(y)
}
When you are finished with the comments, use document() to convert roxygen2 comments into
documentation
devtools::document()
This will create
myexpit.Rd inside the man direcotoryNAPESPACEBoth are read only – NEVER edit these files by hand!
View the help file
?myexpit
The DESCRIPTION file is pre-generated by roxygen2
Package: ayapack
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
You can edit the DESCRIPTION file by hand
If your package depends on other packages or you want to suggest other packages, then include them in the DESCRIPTION file
For example, if you require survival and suggest ggplot2, then
usethis::use_package("survival")
usethis::use_package("ggplot2", "suggests")
will add survival under Imports and ggplot2 under Suggests
Imports:
survival
Suggests:
ggplot2
Example data sets are very useful for demonstrating your functions
data/ # I like to use the here package to locate my files
library(here)
exdata <- read.table(here("exdata.txt"), header = TRUE)
# Alternatively, you can specify the exact location to your file by writing out the full path
.rda extension in data/ use_data(exdata)
Every data set should come with a description
.R file and add roxygen2 commentsR/ subdirectory with the name exdata.Rdevtools::document() to convert the comments to documentation?exdata#' Example data
#'
#' Data from a toxicology study blah blah blah
#'
#' @docType data
#' @format A data frame with 1028 rows and 4 variables:
#' \describe{
#' \item{LitID}{Litter ID}
#' \item{Dose}{Dose of ethylene glycol (EG) administered}
#' \item{FetalWt}{Fetal weight}
#' \item{FetalMal}{Indicator for fetal malformation}
#' }
#' @source https://content.sph.harvard.edu/fitzmaur/ala2e/
#' @references Price, C.J., Kimmel, C.A., Tyl, R.W. and Marr, M.C. (1985).
#' The developmental toxicity of ethylene glycol in rats and mice.
#' Toxicological Applications in Pharmacology, 81, 113-127.
"exdata"
Vignettes are very popular now – they are like tutorials for your package
Use R Markdown to write your vignette
This is easy to do
devtools::check()
Fix any errors that come up and rerun until no errors are detected
You can also perform a spell check by
devtools::spell_check()
Open Git Bash (in Windows) or Terminal (in Macs) and type
git config --global user.name "My Name"
git config --global user.email "myemail@email.com"
To confirm
git config --global user.name
git config --global user.email
Or use R
library(usethis)
use_git_config(user.name = "My Name", user.email = "myemail@email.com")
# to confirm, generate a git situation-report, your user name and email should appear under Git config (global)
git_sitrep()
git remote add origin https://github.com/username/reponame.git
git init
git add .
git commit -m "initial commit"
git push -u origin master
Go to your GitHub Repo page and refresh the browser to see the contents of your package
Add a README – you can include
You now have a package that you can share with others!
Installing your R package from GitHub is easy with devtools
install.packages("devtools")
library(devtools)
devtools::install_github("github_username/yourpackage")
library(yourpackage)
check() before pushing the new edits back onto GitHub