+ - 0:00:00
Notes for current slide
Notes for next slide



R packages: Part I

Dr. Mine Dogucu

1 / 15

Naming your package

available::available("mypackage")
2 / 15

Creating your package

File > New Project > New Directory > R Package

3 / 15

Package directory

  • R where R scripts for functions are stored.
  • man where documentation files are stored.
  • DESCRIPTION contains the metadata about the package.
  • NAMESPACE will be generated automatically. More on this later.

More components to follow later (e.g. data storage).

4 / 15

Shortcuts

Install Package: Cmd + Shift + B Check Package: Cmd + Shift + E Test Package: Cmd + Shift + T

5 / 15

Adding a license

usethis::use_mit_license("Mine Dogucu")
✓ Writing 'LICENSE' # what CRAN requires
✓ Writing 'LICENSE.md' # created only for human-readers
✓ Adding '^LICENSE\\.md$' to '.Rbuildignore' # LICENSE.md is ignored when package is built

usethis license functions

6 / 15

Writing functions

  • Functions are written in an .R file and stored in the R file.

  • You do not need one .R file per function. If functions are closely related you may want to keep them in a single file.

7 / 15

Documenting functions

When your cursor is inside a function code, then click on Code>Insert ROxygen Skeleton

8 / 15

#' Mean and Median Summaries for Numerical Vectors
#'
#' @param some_variable a numeric vector
#' @param na.rm indicates whether missing data should be removed or not
#' #' @return a numeric vector with mean and median values
#' @export
#' @importFrom stats median dbinom
#' @examples
#' print_mean_median(mtcars$mpg, na.rm = TRUE)

9 / 15

Dependencies

  • All functions that are from packages other than base need to be explicitly imported, either as a function or as a full package.

  • All functions from a package can be imported with @import

  • Specific functions can be imported with @importFrom stats median dbinom

10 / 15

11 / 15

Documentation

devtools::document()
12 / 15

Workflow so far

  1. Write function
  2. Add ROxygen Skeleton and write documentation
  3. devtools:document()
  4. devtools:check()
13 / 15

Testing

devtools::test()
x No testing infrastructure found. Create it?
1: Yes
2: No

This will create the tests folder where we will store the previously written tests. We will use the tests from last week.

14 / 15

Workflow

  1. Write function
  2. Add ROxygen Skeleton and write documentation
  3. devtools:document()
  4. devtools:check()
  5. Write tests
  6. devtools:test()
15 / 15

Naming your package

available::available("mypackage")
2 / 15
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow