This tutorial explains how to setup an R package so that it complies with a default style. We would like to apply this default style to all KWB-packages that are hosted on our GitHub account KWB-R. The aim is to present all packages in a common manner (consistent DESCRIPTION and documentation) and to use the same services (continuous integration, code coverage) for all our packages.

This package provides functions to:

  • generate a DESCRIPTION file in which fields are already set to “our” defaults. For example, the licence field will be set to the open source licence MIT. An additional LICENCE file will be added that informs about KWB being the copyright holder of the package.
  • generate files that control the automatic generation of documentation files.
  • generate files that control continuous integration, i.e. the automatic building and testing of the package.

1 Install required R packages

Before you can use the package kwb.pkgdown you need to

Install required packages that are not yet available in your local R user library from CRAN:

The remotes package that should now be installed, allows to install packages directly from GitHub. Therefore, the functions from this package need to communicate with the GitHub server. As it is best for users to get explicitly authenticated by the GitHub server we will set a so called Private Access Token (PAT) in the following. This token will then automatically be used whenever the GitHub server is accessed. Please see the Installation vignette first to learn how to get your personal GitHub PAT and use that token instead of Put your Private Access Token here in the following command:

Sys.setenv(GITHUB_PAT = "Put your Private Access Token here")

Now, that the access token is set, you can use install_github() from the remotes packages to install packages directly from GitHub. Let’s start by installing a fully working version of the remotes package itself (unfortunately, the current CRAN version as installed above contains a bug…):

remotes::install_github("r-lib/remotes@18c7302637053faf21c5b025e1e9243962db1bdc")

Finally we can install the latest version of this package from our GitHub account:

remotes::install_github("KWB-R/kwb.pkgbuild")

2 Configure and create your package

Secondly you need to personalise your R package by adapting a default template that we provide for KWB packages. This template is contained in the R package kwb.pkgbuild.

2.1 Prepare a package directory

In the best case, your package will be under version control. We encourage you to create a GitHub repository on our GitHub account that represents the package. Having a package as a repository on GitHub has the advantage that it can be easily installed directly from there with no more than running remotes::install_github("kwb-r/<package_name>"). Once you have created the GitHub repository, clone it to a folder on your local machine.

You can let RStudio do the cloning for you. Therefore,

  • select “File > New Project…” from the main menu,
  • select “Version Control > Git”,
  • set “Repository URL” to your new repository, such as https://github.com/kwb-r/<package_name>,
  • set “Project directory name” to your <package_name> (this should be the default),
  • set “Create project as subdirectory of” to the folder in which you want to store your local copies of your github repositories, e.g. to ~/github-repos.
  • click on “Create Project” to let RStudio copy the package repository from GitHub onto your local machine.

For the steps described in the following, you need to provide the name of the package in a variable package and the path to the local directory to which GitHub repositories are cloned in a variable repo_dir:

2.3 Parameterise your R package

In the following, we present the commands required to setup and create your new package. We suggest that you write the corresponding code into a .R script file (e.g. setup_package.R). Once the package is created, we suggest to put the script file into the inst/extdata folder of your package. This makes the creation of the package reproducible and can be used as a template for the creation of further packages.

Author

Minimum requirement: the author needs at least to have a (full) name:

author <- list(name = "Max Mustermann")

You can add further information such as a personal website or the ORCID of the author:

The ORCID uniquely identifies the author of a web ressource and thus allows to find different works of one and the same author on the web.

If you do not know your ORCID, have a look at our package kwb.orcid. It allows to search ORCIDs by name, once you have created an account at orcid.org.

In addition, this package stores the ORCIDs of KWB researchers of whom we know their ORCID. Once you have kwb.orcid installed, you can access these ORCIDs with:

Package description

The package description needs three entries

  • name: name of the package

  • title: title of your R package (which is automatically converted to title case with the function tools::toTitleCase())

  • desc: package description. Should be at least one sentence long and needs to end with a period!

2.4 Create R package structure in KWB-R style

Running the following code not only creates an R package structure but also adds some KWB-R specfic styling, e.g.:

Running the following R function will create the R package with the version = 0.0.0.9000 and development stage experimental (defined here).

2.5 Add your R functions

Add your R functions in the folder R/. By using usethis::use_r with the parameter name = function an empty R script is already stored in the right folder R/.

usethis::use_r("function")

For writing your R code/functions please follow the tidyverse coding style (https://style.tidyverse.org/), which serves as our default KWB-R style.

Now you just need to fill it with content (i.e. your functions) and document it using roxygen2. If you have already defined a function you can add a roxygen2 skeleton by using clicking on the Insert Roxygen Skeleton button in RStudio as shown below.

More information on documentation in R is provided here: http://r-pkgs.had.co.nz/man.html

3 Check your package

Once you completed all the steps above go to the upper right panel in RStudio and click on Build -> More -> Configure build tools as shown below.

Then click on Configure and a new window opens. Here you select everything as shown below:

After doing so accept the settings by two times clicking Ok.

Subsequently click the Check button so that your package is cross-checked for possible problems (e.g. wrong documentation, missing package dependencies).

In case of missing package dependencies as shown below these should be added to the DESCRIPTION file.

This can be done using the function usethis::use_package() as shown below:

Subsequently you should re-click on the Check button again and it should finish without errors.

4 Build your package

Now you are ready for building your R package by clicking on the Install and Restart button. A successful installation should finish with Done as shown below:

5 Document your package

5.1 Manually

Finally you should run pkgdown::build_site() in order to create an documentation website for your R package. Running this command will store the website in the subfolder docs within your R package.

Once you upload your R package to Github this can be easily used as documentation page that you define in the settings page for your R package which is available at:

https://github.com/KWB-R/ kwb.mycoolrpackage /settings

5.2 Automatically

In case you have already a Github repo defined for your R package you can also automate the process of updating the pkgdown::build_site() by with the wrapper function kwb.pkgbuild::use_autopkgdown(), which:

Finally to need to go to:

https://github.com/KWB-R/ kwb.mycoolrpackage /settings

and set the “source” for Github Pages to the branch “gh-pages”. After each successful Travis build the documentation website is now also updated!