R Markdown Template for Developing R Package π¦
pkgtemp contains as series of
usethis and other commands in an
R Markdown that provides a template to kick-start your R package
development process. If you want to use your own template from
GitHub, this package will facilitate that too.
You should be somewhat familiar with how to create R Package. Checkout these resources to learn more:
-
R Packages Book by Hadley Wickham and Jenny Bryan.
-
usethis: a workflow package that
pkgtemprelies on.
(pkgtemp assumes that R package is develop in RStudio. If you use
other IDE you may need to adapt your workflow accordingly.)
You can install the development version of pkgtemp like so:
# install.packages("remotes")
remotes::install_github("Lightbridge-KS/pkgtemp")- Build Package Template: contains a set of R functions to run in each step of package development.
Create R Package by:
usethis::create_package("~/path/to/yourpkg")Create R Markdown Template for package development by:
pkgtemp::use_pkgbuild_rmd()This will:
-
Create folder
dev/. -
Write and open R Markdown file
dev/build.Rmd(generate locally)
Go to build.Rmd. You will see 2 types of command in there:
-
Non-commented commands: are the command for initial setup. I recommend you run at the first visit. They are at the top of R Markdown.
-
Commented commands: you can choose to run in any order as you like.
# Create a new package -------------------------------------------------
path <- file.path(tempdir(), "yourpkg")
usethis::create_package(path)
#> β Creating '/var/folders/ry/z9m8k9cs4594pv3458npy1zw0000gn/T/RtmpU7eQdS/yourpkg/'
#> β Setting active project to '/private/var/folders/ry/z9m8k9cs4594pv3458npy1zw0000gn/T/RtmpU7eQdS/yourpkg'
#> β Creating 'R/'
#> β Writing 'DESCRIPTION'
#> Package: yourpkg
#> Title: What the Package Does (One Line, Title Case)
#> Version: 0.0.0.9000
#> Authors@R (parsed):
#> * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
#> Description: What the package does (one paragraph).
#> License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#> license
#> Encoding: UTF-8
#> Roxygen: list(markdown = TRUE)
#> RoxygenNote: 7.1.2
#> β Writing 'NAMESPACE'
#> β Setting active project to '<no active project>'
# only needed since this session isn't interactive
usethis::proj_activate(path)
#> β Setting active project to '/private/var/folders/ry/z9m8k9cs4594pv3458npy1zw0000gn/T/RtmpU7eQdS/yourpkg'
#> β Changing working directory to '/var/folders/ry/z9m8k9cs4594pv3458npy1zw0000gn/T/RtmpU7eQdS/yourpkg/'
.old_wd <- setwd(path) # Only in this example
# Create Rmd Template for PKG Development
pkgtemp::use_pkgbuild_rmd(open = FALSE)
#> β Creating 'dev/'
#> β Writing 'dev/build.Rmd'
#> β Adding '^dev$' to '.Rbuildignore'
#> β’ Edit 'dev/build.Rmd'Everyone can have a different package development workflow. This second approach will allows you to use your default template from a file in GitHub.
Here how it works:
-
Create your own R Markdown template for building package in Github. (for example, mine is here)
-
Provide a default GitHub URL where
pkgtempwill find as your remote template. Youβll need to config global option in~/.Rprofileas follows.
# To edit `~/.Rprofile`
usethis::edit_r_profile()Copy code below to ~/.Rprofile and provide your pkgbuild_url as URL
to your default template.
if (interactive() && requireNamespace("pkgtemp", quietly = TRUE)) {
pkgtemp::set_github_template_url(
pkgbuild_url = "https://github.com/OWNER/REPO/blob/REF/path/to/default-template.Rmd"
)
}- To use your default template from GitHub, create R package as
usual and call the previous function
use_pkgbuild_rmd()withremote = TRUE.
pkgtemp::use_pkgbuild_rmd(remote = TRUE)Alternatively, If you want to use any other GitHub template just once
in a while, provides a url directly.
pkgtemp::use_pkgbuild_rmd(url = "https://github.com/OWNER/REPO/blob/REF/path/to/one-off-template.Rmd")Last updated: 2022-05-03