Package build

Developing R Packages

Jasmin Ludolf

Content Developer

Time unit converter

time_converter <- function(time, unit_from, unit_to) {
  # Define conversion factors for different time units
  conversion_factors <- c(hour = 3600, minute = 60, second = 1)

  # Convert time to seconds
  in_seconds <- time * conversion_factors[unit_from]

  # Convert time from seconds to the desired unit
  return(unname(in_seconds / conversion_factors[unit_to]))
}
Developing R Packages

Use time_converter() and add to package directory

time_converter(24, "hours", "seconds")
86400
dump("time_converter", file = "R/time_converter.R")
Developing R Packages

Load all files in the package

library(devtools)
load_all()
- Loading unitConverter
  • Loads package files into the current R session
  • Enables testing, debugging, and code correctness checks before installation
Developing R Packages

Silly typo

time_converter <-
function(time, unit_from, unit_to) {
  if ( (unit_from == "hours" && unit_to == "minutes") ||
       (unit_from == "minutes" && unit_to == "seconds") ) {
    return(time * 60)
  } else if ( (unit_from == "minutes" && unit_to == "hours") ||
              (unit_from == "seconds" && unit_to == "minutes") ) {
    return(time / 60)
  } else if (unit_from == "hours" && unit_to == "seconds") {
    return(time * 3600)
  } else if (unit_from == "seconds" && unit_to == "hours") {
    return(time / 3600)
  } else {
    return(time)  # No conversion needed

}
Developing R Packages

Detecting errors with load_all()

load_all()
- Loading unitConverter
Error in `load_all()`:
! Failed to load R/time_converter.R
Caused by error in `parse()`:
! ~/unitConverter/R/time_converter.R:17:0: unexpected end of input
15: 
16: }
   ^
Run `rlang::last_trace()` to see where the error occurred.
Developing R Packages

Differences between load_all() and install()

load_all()

  • Used during development
  • Loads package contents into current R session
  • Test functionality before installing

install()

  • Can be used during development after load_all()
  • Install a package
  • Update an installed package
Developing R Packages

Check package requirements programmatically

check()
  • Syntax checks
  • Package dependencies
  • Documentation quality
  • Coding standards
  • And more
Developing R Packages

Some check() output

Sample last line of output from check()

0 errors  | 2 warnings X | 1 note X

Ideal state

0 errors  | 0 warnings | 0 notes
  • Errors: Significant issues to fix before package considered acceptable
  • Warnings: Highlight potential issues or best practice violations
  • Notes: Suggestions for improvements such as additional documentation
Developing R Packages

Let's practice!

Developing R Packages

Preparing Video For Download...