2024-08-06
What if I told you you can create an R package
in just 10 minutes
π€―
library(devtools)
create_package("~/htahackathon2024/stringsplitter")
use_git()
use_r("split-string.R")
check()
#> ... (output truncated) ...
#> ββ R CMD check ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> β checking DESCRIPTION meta-information ...
#> β checking R files for syntax errors ...
#> β checking whether the package can be loaded ...
#> β checking R code for possible problems (1.3s)
#> β checking examples ...
#>
#>
#> ββ R CMD check results ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ stringsplitter 0.0.1 ββββ
#>
#> 0 errors β | 0 warnings β | 0 notes β
RStudio keyboard shortcuts
Shift + Ctrl/Cmd + L
to load allShift + Ctrl/Cmd + E
to run checks :::Write some documentation in roxygen format for your new function
Tip
In RStudio, go to Code -> Insert Roxygen skeleton
to make your life easier
R/split-string.R
#' Split a string into a vector of strings
#'
#' @param x a character string to be split
#' @param sep the separator on which to split
#'
#' @return a vector of strings
#' @export
#'
#' @examples
#' splitstring("alfa,bravo,charlie")
#' splitstring("alfa,bravo charlie", sep = " ")
splitstring <- function(x, sep = ",") {
strsplit(x, split = sep)[[1]]
}
Generate the help pages by running document()
or Ctrl/Cmd + Shift + D
in RStudio
splitstring()
availableR/split-string.R
#' Split a string into a vector of strings
#'
#' @param x a character string to be split
#' @param sep the separator on which to split
#'
#' @return a vector of strings
#' @export
#'
#' @examples
#' splitstring("alfa,bravo,charlie")
#' splitstring("alfa,bravo charlie", sep = " ")
splitstring <- function(x, sep = ",") {
strsplit(x, split = sep)[[1]]
}
document()
will update the NAMESPACE
file for us
install()
#> ββ R CMD build ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> β checking for file β/Users/milan/htahackathon2024/stringsplitter/DESCRIPTIONβ ...
#> β preparing βstringsplitterβ:
#> β checking DESCRIPTION meta-information ...
#> β checking for LF line-endings in source and make files and shell scripts
#> β checking for empty or unneeded directories
#> β building βstringsplitter_0.0.1.tar.gzβ
#>
#> ... output truncated ...
#>
#> ** 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 (stringsplitter)
One of the things research programmers struggle with is the transition from exploration to infrastructure, i.e., from βcoding to figure out what the problem isβ to βIβm building a reusable toolβ. Habits from the first are often carried over to the second.
β Tweet from Greg Wilson 2018
rm(list = ls())
setwd()
Use a project-oriented workflow instead
Because readability is important
# Donβt do this
if(x<100){
y<-200}
# Do this instead
if (x < 100) {
y <- 200
}
# NEVER use the shorthand versions of TRUE and FALSE
# Why? This is why:
1T <- FALSE # this is valid R code
R packages: What, Why and How?