Bernardo Niebuhr, Elie Gurarie
This is a tutorial on a few steps to create R packages.
library(available)
available("howtoRpackage", browse = F)
We can create the structure of a package using the function
create_package
from the usethis
package. This create the very
minimal structure before building the package.
library(usethis)
# create the package structure
usethis::create_package("howtoRpackage")
One can define a license by hand or using a few pre-determined functions
from the usethis
package. If we use the function, this will modify
both the DESCRIPTION
file and also add a LICENSE.md
file.
# Begin readme
usethis::use_readme_md()
# Define GPL3 as our license
usethis::use_gpl3_license(name = "Bernardo Niebuhr, Elie Gurarie et al")
Now we add some functions. We can create them by hand (just copy and
paste now) within the R
folder. Now we can build
# Necessary packages
usethis::use_package("sp", type = "Imports")
# Suggested packages
usethis::use_package("plyr", type = "Suggests")
First we create the raw-data
folder using the use_data_raw
function.
It will also create a script for preparation of the raw data into the R
data format, .rda
.
# add raw data
usethis::use_data_raw()
Now, we put the our dataset in the data-raw
folder. Then we can edit
the script for preparing the dataset. There we read and prepare the
data, then we save it as .rda
using, e.g. the function
usethis::use_data
or save()
:
# read data
juveniles <- read.csv("data-raw/subsetGPSlocs.csv")
# create data for use in the Rpackage
usethis::use_data(juveniles, overwrite = TRUE)
# or
dir.create("data/")
save(juveniles, file = "data/juveniles.rda")
Now we can use the package, and then save this as an example script on how to use our functions.
roxyxen2
documentation to the scriptWe can add some info to the functions, using the roxygen2
notation,
and it will be transformed into documentation in the man
folder.
usethis::use_git(message = "initial commit")
usethis::use_github()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.