Building my first R package
Notes on structuring an R package: dependencies, DESCRIPTION files, documentation and testing.
On this page
Installing Packages
install.packages("devtools")
install.packages("usethis")
Create Package
library(usethis)
create_package('~/Documents/siliconPackage')
Create file
library(usethis)
use_r('silicon')
Package vs R Script
In normal R Scripts you call libraries but in a package you track dependencies differently with using use_package() which adds the package to the DESCRIPTION file.
use_package('ggplot2')
By default, r looks for function inside your own package first so we do not explicitly state the package name. But for external packages like ggplot2 we would use ggplot2::function
.Rprofile
With an .Rprofile file we can add libraries that should be loaded after opening the Package Project in R
if (!require(devtools)) {
install.packages("devtools")
library(devtools)
}
Do Checks
devtools::check()
Publish on Github
First init git with use_git('init package')
use_github()
#or
use_github('NameOfOrganization')
Download Package
devtools::install_github('username/packagename')
Documentation
Roxygen2 is a way of documenting function and with @export we tell roxygen2 that this function is ready for export and is accessible for other users that load the package. We can also call the usual ?function the returns our short description. Other function without @export are internal function and can’t be accessed.
#' Title
#'
#' Short description of function with *markdown* syntax
#'
#' @export
function_name <- function(){
}
To make the documentation call the following in the console:
devtools::document()
use_mit_license()
Test created functions
use_testthat()
use_test('nameOfRRcript')
test()
Alternatively in RStudio we can use Build to run all tests.
Rename file
rename_file('old', 'new')
Add readme for github
use_readme_rmd()
build_readme()
Back to top