<- list(arg1 = arg1, arg2 = arg2) result
My workflow in working with functions in a package
RSE
Rpkg
Structure them, write them, test them, document them.
Ongoing notes
Some of the content are being added as I go.
Functions make up the whole R package, except for data-only packages. The workflow should help me navigate the process.
A short example borrowed from GSWEP4R workshop is documented here, function code and test code
- function 1: implement a function with class attribute
SimulatedResult
- function 2: generic function
print.SimulatedResult
that prints what f1 produces - test files for function 1 and 2
Structure
Write
What to return?
Put the arguments in a list named result
, then attach other outputs to result
.
Afterwards, set class attribute to something meaningful, for example,
<- structure(result, class = 'SimulationResult') result
This allows us to implement generics.
Generics
Common generics
- summary
- plot
<- function(x, ...){
print.classname # x is an object of `classname`
# it should be a list, and have class attribute `classname`
}
Test
Tests for functions and generic functions seem to be the same as before. Need to go back to the function and add error messages.
Document
The way to document generic functions is exactly the same as any other function.
#' Print method
#'
#' @description
#' Generic function to print a `SimulationResult` object
#'
#' @param x a \code{SimulationResult} object to print
#' @param ... further arguments to pass from other methods
#'
#' @return something printed
#' @export
#'
#' @examples
#' simd <- fsim(n1 = 10, n2 = 10, mean1 = 0, mean2 = 5, sd1 = 1, sd2 = 1)
#' print(simd)