date | Species |
---|---|
12-01-1990 | Pinus contorta |
12-01-1990 | Heterotheca villosa |
12-02-1990 | Arctostaphylos uva-ursi |
UW R-Ladies
4/28/23
When you write an R package, you’re sending your functions out into the world for others to use , and you want them to be successful! An ideal R function…
Example: To work in your function, a dataset must have a column called “date” with a date in MM-DD-YYYY POSIXct format and a column with character strings of species names called “Species”. There must be data from at least two different consecutive days.
date | Species |
---|---|
12-01-1990 | Pinus contorta |
12-01-1990 | Heterotheca villosa |
12-02-1990 | Arctostaphylos uva-ursi |
Strategy 1: Require the user to provide exact function inputs, and return informative errors when they doesn’t meet the function’s requirements
Strategy 2: Require specific data types, etc., but allow users to provide their own column names
Strategy 3: Allow users to provide their own column names, and make function options for multiple possible data types
devtools
to make a dummy R package!Make sure the devtools
R package is downloaded and installed
In the R studio project window for the package, you’ll see a “Build” tab
usethis::use_git()
to make the package directory a git repository (on your local drive)getRow <- function(dat, rowInd = 1, ...) {
# check that the 'dat' argument is a data frame
if (is.data.frame(dat) == FALSE) {
stop("The 'dat' argument must be a data frame")
}
# check that the 'ind' argument is numeric
stopifnot("The 'rowInd' argument must be numeric" = is.numeric(rowInd))
newRow <- dat[rowInd,]
return(newRow)
}
With your cursor inside of the function definition, click the “wand” button on the top of the script, and select “Insert Roxygen Skeleton”
Title: Written in sentence case without a period at the end, followed by an empty line. Clearly state what the function does in a few words
Description: A short paragraph that describes what the function does and it’s most important features. Isn’t automatically in the Roxygen skeleton, must be added with ‘@description’
getRow()
extracts a row or rows from a data frame. Rows are extracted by numerical index that is supplied by the user.”Arguments: Describe each of the arguments to the function in detail. Explain the required format and type, as well as any defaults
@param dat A data frame from which a row is extracted.
@param rowInd Either a single value or a vector of numeric arguments that indicates the index or indices of the rows to be extracted from dat
. The default value is “1”
@param … further arguments to be passed to getRow
.
Returns: Describe the structure and format of everything that is returned by the function
getRow
returns a data frame that contains the row or rows of the dat
argument that are specified in the rowInd
argument. The column names and types are the same as in dat
.The @export
tag is automatically included in the Roxygen skeleton. This indicates that you want this function to be “exported” by your package (i.e. not an internal function)
Examples: Give one or more examples of the function, which will be run every time that the function is checked and that will be viewed in the documentation.
Roxygen documentation uses a fixed text width. You can make R Studio automatically wrap text in a comment with “Code > Reflow Comment” (Ctrl/Cmd+Shift+/).
You can also make R studio show a line at the margin edge in “Global Options > Code > Display”
devtools::document()
Title: One-line description of the package in Title Case without a period at the end
Description: A more detailed description of the package, up to one paragraph long
URL or BugReports fields, which give the URL for the package or the GitHub page where you can report bugs
Author Information: Information about the author(s), in a specific format
License: Indicates which License you want to use.
usethis
package to automatically fill in the relevant fields, depending on which license you want to use
use_mit_license()
or use_gpl3_license()
Imports: lists the R packages (or specific functions in those R packages) that are required by the functions in your package
slice()
function from the dplyr
R package. To add dplyr
to the DESCRIPTION file, useSuggests: gives a list of R packages that are used in the development of your package (e.g. in making vignettes), or improve the package but aren’t required
use_package()
function, but include “Suggests” inside the function calldevtools::load_all()
to internally build package binaries and load them into memory. This is how we test functions, and make sure the package passes important checks!
devtools::check()
function runs the R CMD CHECK command, which runs a series of formal checks. Your package must pass all of these checks, especially if you want to submit to CRAN!Your package has passed the R CMD CHECK, and now you want to build it into a “tarball” to distribute to others. You can do this easily using devtools::build()