{box}
klmr.me/box
MarEichler/rladies_box_intro
rladies_box_intro/presentation/presentation.pdf
Martha Yvonne Eichlersmith
R-Ladies Twin Cities
March 10, 2022
Introduce the {box}
package and show how it can be used to write modularized/organized code and manage the name space.
Disclaimer: I have been using this package for about 6 months and I’m still learning new things everyday! Please ask as many questions as you want, but I may not know the answer.
{box}
{box}
?{box}
: Name Space{dplyr}
and {stats}
use the same name for certain functions, CONFLICT in the namespace{box}
: Modular-izeNote: this is a simplified version of the example in the EX_LONG
folder of the repository, using mpg
data from the {ggplot2}
Create plots with different variables ⇒ more code (write or copy/paste)
More situations/dimensions ⇒ additional unstructured code:
Create plots with different variables ⇒ can easily use functions
More situations/dimensions ⇒ more functions:
Create plots with different variables ⇒ can easily use function
More situations/dimensions ⇒ Longer function
Item |
Option 1 Write Out Code |
Option 2 Multiple Functions |
Option 3 One Long Function |
Option 4 Box Modules |
---|---|---|---|---|
Namespace Usage |
Constantly have to create unique names for objects |
Only use name space for specific functions and/or dependencies |
Only use name space for specific function |
Only use name space for box modules |
Testing Code |
Testing code as written but shouldn’t repeat code multiple times |
Functions are sectioned so can test at each level |
Difficult to test individual sections once function get extremely long |
All testing can be done within the module |
Share/Re-Use |
Have to piece through which code to keep and then amend to new program |
Put functions together and document dependencies |
One function but may be challenging to amend to a different situation or different machine |
Just send the box modules .R files
|
Additional Dimensions |
New code either added to existing code or new section, bad practice if will use multiple times |
Manageable to difficult depending on how much each of the functions and their dependencies need to change |
Difficult given that the function is so large, adding a new dimension would be challenging |
Adjust easily within the box module or create a new box module (depending on situation) |
{box}
.R
files
{ggplot2}
, {dplyr}
, etc.)library()
commandbox::use()
, NOT library()
box::use
is a universal import declaration. It works for packages just as well as for modules. In fact, ‘box’ completely replaces the base R library
and require
functions. box::use
is more explicit, more flexible, and less error-prone than library
. At its simplest, it provides a direct replacement
box::use()
myboxes
: folder where box module file is located
Abox
: name of box module to load (without the .R
extension)
{dplyr}
and attached all exported names
library(dplyr)
{dplyr}
functions without specifying the package name
box::use(dplyr)
, would not be able to use functions without calling package (i.e. dplyr::filter
)
#’
: before each line with information on item
@param
: specifies variable inputs (each input has own line)
@return
: specify output (if function)
@export
: export item (REQUIRED if using outside of module)
@examples
: examples of use
File: box/greet.R
box::use(
glue[glue]
, stringr[str_to_title]
)
#' not exported, used WITHIN only
greeting <- "Hello"
#' Greet Someone when Given a Name
#' @param name A character string
#' @return Greeting with input name
#' @export
#' @examples
#' say_hello("Martha")
say_hello <- function(name){
to_use <- str_to_title(name)
glue("{greeting}, {to_use}")
}
If you make changes to box module, need to reload module in order to changes to be in effect
box::use()
)