Producing an R Package

Han Oostdijk

2019/12/17

Things to remember:

Use Document , Clean and Rebuild, Test Package in the Build tab.

To include vignettes use devtools::install(build_vignettes=TRUE).

When testing in the package project use Load All in the Build tab.

Create a new package (example)

This section documents how I created the package tabraux. I can not get the GitHub connection working unless I start with a GitHub repository. Therefore:

First create interactively a new GitHub repository

Do this by choosing new in the GitHub desktop and filling in the following screen:

This generates repository https://github.com/HanOostdijk/tabraux.git

Create a new project in RStudio

Indicate in RStudio that you want to create a new project:

Execute the following code in a new session and answer No way on the question Overwrite pre-existing file 'tabraux.Rproj'?. Because in a later step the documentation could not be produced (in first instance) maybe it is better to answer yes. (However I was wondering if the Git-connection was at risk)

create_package("../tabraux")

Create a DESCRIPTION file

Execute the following code and

usethis::edit_file('DESCRIPTION')

and edit the DESCRIPTION file so that it looks like (in my case)

Package: tabraux
Title: Auxiliary functions for package tabr
Version: 0.0.0.9000
Authors@R: 
    person(given = "Han",
           family = "Oostdijk",
           role = c("aut", "cre"),
           email = "han@hanoostdijk.nl",
           comment = c(ORCID = "0000-0001-6710-4566"))
Description: Auxiliary functions for package tabr.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.2

Create a README.Rmd file

On GitHub the file README.md was already created but we also create a README.Rmd file:

usethis::use_readme_rmd()

After executing this code we copy the contents of the .md file and knit the .Rmd document so that the two files are in sync.

Create the testthat structure

Copy the file tabraux.R to the R subfolder and generate a test structure for it. Actual tests will be done later on.

usethis::use_testthat()
usethis::use_test('tabraux.R')  

Indicate the packages that will be used

usethis::use_package('tabr')
usethis::use_package('purrr')
usethis::use_package('stringr')

Create the NEWS.md file

After committing all files to Git the NEWS.md file was created:

use_news_md()

Build the documentation and the package

I did not see the Document menu choice in the Build tab. However I could choose there Configure Build Tools ... and indicate that I want to use Roxygen. After that I could use the Document and afterwards the Clean and Rebuild options in the Build tab.

devtools::document(roclets = c('rd', 'collate', 'namespace')) # Document (control-shift-D)
# Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source tabraux # Clean and Rebuild