knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file()) knitr::opts_chunk$set(eval = FALSE) here::i_am("dev/build.Rmd") library(here)
Essential Manuals and Packages:
I recommend initial setup by running the code that is not comment. Then, run the other commented code when you're ready.
usethis::use_git()
Tell Git not to track theses:
usethis::use_git_ignore("/dev/") # (Optional) ignore `dev` folder at PKG root # usethis::use_git_ignore("file")
Tell R not to build theses into R package:
usethis::use_build_ignore("dev") # (Require) if you use this template # usethis::use_build_ignore("file")
usethis::use_description()
Use licence file by use_*_license() family of functions
usethis::use_mit_license() # MIT
# usethis::use_readme_rmd()
Lifecycle badges
# usethis::use_lifecycle_badge("experimental")
# usethis::use_vignette("PKG") ## Articles # usethis::use_article()
# usethis::use_news_md()
A recommended approach
# usethis::use_pipe() # If package use `%>%` # usethis::use_tibble() # If package use `tbl_df` class # usethis::use_package("pkg")
## Developmental Package (not on CRAN) # usethis::use_dev_package("pkg", remote = "OWNER/REPO")
Importing a function from another package allows you to refer to it without a namespace (e.g., fun() instead of package::fun()).
# usethis::use_import_from("rlang", ":=") # Must have if use `:=`
Use suggested packages for example datasets, to run tests, build vignettes, or maybe there’s only one function that needs the package.
# usethis::use_package("pkg", type = "Suggests")
Write some R functions!
# usethis::use_r("fun1") # usethis::use_test("fun1")
# usethis::use_testthat()
Helper function for testing can be place in tests/testthat/helper-tests.R
# fs::file_create(here("tests/testthat/helper-tests.R"))
See covr
# covr::package_coverage()
for interactive use in viewer pane:
# covr::report()
usethis::use_data(): To store exported data in data/.
Each file in this directory should be .rda file containing a single object.
usethis::use_data_raw(): To store code that used for data preparation in data-raw/
# usethis::use_data(data1) # usethis::use_data_raw("data1") # Code to prepare data
Documenting datasets
Document the name of the dataset in R/ as roxygen2:
#' Prices of 50,000 round cut diamonds. #' #' A dataset containing the prices and other attributes of almost 54,000 #' diamonds. #' #' @format A data frame with 53940 rows and 10 variables: #' \describe{ #' \item{price}{price, in US dollars} #' \item{carat}{weight of the diamond, in carats} #' ... #' } #' @source \url{http://www.diamondse.info/} "diamonds"
Save all of the objects for internal use in R/sysdata.rda.. They don’t need to be documented.
# usethis::use_data(int_data1, int_data2, internal = TRUE)
If you want to show examples of loading/parsing raw data, put the original files in inst/extdata.
# usethis::use_directory("inst/extdata")
## Write Raw Data # readr::write_csv(mtcars, here("inst/extdata/mtcars.csv"))
Function: Get Path to Example Data
This function allows user to access paths to raw data.
Inspired from readr example.
# usethis::use_r("example") # Create `example.R` to store `PKG_example()`
PKG_example() to example.R, replace PKG with your package name.#' Get path to PKG example #' #' PKG comes bundled with a number of sample files in its `inst/extdata` #' directory. This function make them easy to access #' #' @param file Name of file. If `NULL`, the example files will be listed. #' @export #' @examples #' library(PKG) #' PKG_example() #' PKG_example("file.csv") PKG_example <- function(file = NULL) { if (is.null(file)) { dir(system.file("extdata", package = "PKG")) } else { system.file("extdata", file, package = "PKG", mustWork = TRUE) } }
Add an R Markdown Template
# usethis::use_rmarkdown_template( # template_name = "Template Name", # template_dir = "dir-name", # template_description = "A description of the template", # template_create_dir = FALSE # )
See also:
If R CMD Check shows a message like:
Undefined global functions or variables: var1 var2
Put this:
# Declare global variables if(getRversion() >= "2.15.1") utils::globalVariables(c("var1", "var2"))
in the same file as where you have package-level documentation.
# usethis::use_package_doc()
See this Discussion
Github action to automate R Package workflow.
Github action to run R CMD Check in the cloud.
This will
Create a YAML file: .github/workflows/R-CMD-check.yaml
Adding R-CMD-check badge to 'README.Rmd'
## Quickstart CI workflow # usethis::use_github_action_check_release()
Ref: Quickstart WF
usethis::use_coverage()# usethis::use_coverage()
test-coverage This will connect your package to codecov.
# usethis::use_github_action("test-coverage")
Ref: test coverage WF
# covr::codecov(token = "your-token")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.