
Package and function design

Figure adapted from Packages, by Hannah Frick
BLT βββ BLT.Rproj βββ DESCRIPTION βββ NAMESPACE βββ R β βββ AR1.R β βββ BLT-package.R β βββ BLT_ggplot_na_distribution.R β βββ data.R β βββ perc_missing.R β βββ perc_missing_tidy.R βββ README.Rmd βββ README.md βββ data βββ data β βββ bacon.rda β βββ lettuce.rda β βββ tomatoes.rda βββ man β βββ AR1.Rd β βββ BLT-package.Rd β βββ BLT_ggplot_na_distribution.Rd β βββ bacon.Rd β βββ figures β β βββ README-pressure-1.png β βββ perc_missing.Rd β βββ perc_missing_tidy.Rd βββ tests β βββ testthat β βββ testthat β β βββ _snaps β β β βββ BLT_ggplot_na_distribution β β β βββ tomato-na-dist.new.svg β β β βββ tomato-na-dist.svg β β βββ test-BLT_ggplot_na_distribution.R β β βββ test-perc_missing_tidy.R β βββ testthat.R βββ vignettes βββ BLT.Rmd βββ BLT.html
unchanged
changed
changed by you
Write programs for people, not computers.
Let the computer do the work.
Make incremental changes.
Donβt repeat yourself (or others).
Plan for mistakes.
Optimize software only after it works correctly.
Document design and purpose, not mechanics.
Collaborate.
Data management
Manuscripts
Software
Collaboration
Project organization
Keeping track of changes
rm(list = ls()) -> computer on fireIf the first line of your R script is
setwd("C:\Users\jenny\path\that\only\I\have")I will come into your office and SET YOUR COMPUTER ON FIRE π₯.
If the first line of your R script is
rm(list = ls())I will come into your office and SET YOUR COMPUTER ON FIRE π₯.
β Jenny Bryan, Project-oriented workflow
Many (most?) people have coalesced around the tidyverse style guide.
You can use the {styler} package to automatically style your code. Once it is installed, you get additional Addins in RStudio.
Style Active File
Style Package (!!)
Letβs try styling our package, and then look at the diff
Now would be a good time to commit your changes
Jason Long, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons
If writing a smaller package, consider prefixing your functions:
BLT_perc_missing()Use a verb next:
dplyr::mutate(), stringr::str_split()Use a noun if building up a specific type of object:
ggplot2::ggplot(), ggplot2::geom_point()Tidyverse uses snake_case; Shiny prefers camelCase
Python prefers snake_case
JavaScript prefers:
camelCase for functionsPascalCase for classes, interfacesPick a convention according to your domain, follow it.
Here, mtcars is an argument:
In R, we sometimes use these terms interchangeably; we sometimes use the term formals.
Β―\_(γ)_/Β―
Like naming functions, strive to be:
There are only two hard things in Computer Science: cache invalidation and naming things.
β Phil Karlton
And off-by-one errors β Leon Bambrick
...): stuff that gets passed to other functionsI have seen the order of dots and details reversed.
However, data and descriptors almost always come first.
Which are: data, descriptors, details?
Which are: data, descriptors, details?
Object of type βclosureβ is not subsettable, Jenny Bryan. rstudio::conf video, slides
Code βsmellsβ and feels, Jenny Bryan