use devtools
Publish date: Apr 23, 2019Tags: 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)
mpg | cyl | disp | hp | drat | wt | |
---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 |