use devtools

Publish date: Apr 23, 2019
Tags: tips example

Although a blog post is not a R package the devtools package provides some useful tools. But who knows maybe a package will sprout from a post.

plain devtools

Provides some functions to create bundled data.
I like to use devtools::load_all() to load up all R functions from the R subdirectory.

I placed a simple function under R/hello_world.R

devtools::load_all()
## Loading DevtoolsPost

Now I can use the function:

hello("you")
## [1] "hello you"

usethis

Having the post setup with package structure usethis.

I like how this makes using packages easier. Say I want to use kableExtra

So the first chunk is:

library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]

However knitr or kableExtra may not have been installed and could get an error.
This can not only happen to strangers using your code but also when one switches machines. So weeding through the library commands could take some work. With use_this you can declare the required packages.

usethis::use_package("knitr")
usethis::use_package("kableExtra")

Should you start on another machine one quickly could install all missing packages using the command:

  devtools::install_deps()

Now assuming the libraries are all present.

library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:6]

and the command will work:

kable(dt)
mpgcyldisphpdratwt
Mazda RX421.061601103.902.620
Mazda RX4 Wag21.061601103.902.875
Datsun 71022.84108933.852.320
Hornet 4 Drive21.462581103.083.215
Hornet Sportabout18.783601753.153.440