knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(cli.num_colors = 1, crayon.enabled = FALSE)

Acknowledgement

Setting up an R package isn't really intuitive as the developer needs a basic understanding of some package conventions and good practice methods. There are multiple packages in the R landscape that help doing exactly this. personalr makes heavy usage of the most important package development helper package: usethis. It has a ton of useful functions but the developer has to know which of them is needed for the individual purpose.

personalr is more or less a wrapper around several usethis functions and completes the whole thing with some helper functions. The package setup that personalr creates is also based on the tidyverse package.

Everything you need to know about R package development is perfectly summarized in Hadley Wickham's book R Packages.

Usage

First setup

The main function of personalr is setup_package(). Handed a valid path, the desired package name and a vector or list of package names (the 'core' of the personal package that is about to be created) it does the following things:

The following example creates the new package "mypackage" with the core packages dplyr, glue and purrr in a temporary directory.

library(personalr)
temp_directory <- tempdir()
setup_package(path = temp_directory, packagename = "mypackage", core = c("dplyr", "glue", "purrr"))
library(personalr)
temp_directory <- tempdir()
setup_package(path = temp_directory, packagename = "mypackage", core = c("dplyr", "glue", "purrr"))

Once the setup is finished and the new package installed, the new package should be opened in a separate R session. It will show the following files and directories

library(fs)
fs::dir_tree(fs::path(temp_directory, "mypackage"))

Update your package

There are basically two reasons why you want to update your personal package:

  1. You want to update the list of core packages
  2. You want to add a helper function so you can call it when the package is loaded (i.e. 'export' a function)

Update your 'core' packages

There is a separate function in personalr to update your list of core packages: update_core(). It can either append new packages to the existing core or overwrite it completely (with the logical argument append).

Assuming you have run the above code and have created the personal package "mypackage" with the core packages "dplyr", "glue" and "purrr" you can add another package, e.g. "tibble" by running the following code (please note that the example uses temporary directories and if you run this on your machine it's easier to open the project and set path = ".").

update_core(path = temp_directory, packagename = "mypackage", core = "tibble", append = TRUE)
update_core(path = temp_directory, packagename = "mypackage", core = "tibble", append = TRUE)

Now "mypackage" attaches "dplyr", "glue", "purrr" and "tibble" when it is loaded. If you want to overwrite the core (i.e. only use "tibble" and drop the other packages) just set append = FALSE in the above code chunk.

Add your own exported helper function

One of the strengths of a package is that you are able to save your helper functions inside the package and make them available in your code without littering the Global Environment.

Let's say you often use the negated version of %in% to select values that are not in a vector. The typical way to do it is !value %in% vector. This is somewhat annoying as it's hard to read the code and see the exclamation mark at the beginning of the expression. So now we want to write and export our own little helper for the negated version of %in%, and we call it %nin%.

If you run this inside your project

usethis::use_r("nin")

it will create a new script "R/nin.R" and open it directly. Add the code of your helper function and don't forget to export it with #' @export (this means the next time you document and install your package the new function will be exported and available for usage).

#' @export
'%nin%' <- Negate(`%in%`)

To update your package you should consider changing the package version to make more clear something changed by running

usethis::use_version()

and afterwards documenting and installing the new version by running

devtools::document()
devtools::install()


mrcaseb/personalr documentation built on July 17, 2025, 4:22 a.m.