This chapter is being developed. Thank you for your patience.
TLDR
The chores package was developed by Simon Couch1 to “help you complete repetitive, hard-to-automate tasks quickly”. We previously covered the ellmer package (see Chapter 27), which we’ll use to configure our chosen LLM.
install.packages(c('ellmer', 'chores'))# or the dev versionspak::pak('tidyverse/ellmer')pak::pak("simonpcouch/chores")
28.1 Navbar App
In this branch we’ll use chores to add roxygen2 documentation to an updated version of the movie review application. The application still contains the original scatter plot with inputs for variables, aesthetics, and a plot title. However, the UI has been built around the page_navbar layout from bslib, with nav_panel()s containing graphs and tables for Counts, Distributions, and Awards.
28.1.1 Modules
The modules in the application have the following naming convention:
mod_<tab>_<type>_ui/server
<tab> is the name of the tab (Compare, Counts, Distributions, Awards)
<type>
vars: ‘variable’ collection modules to return inputs
tbl: table display modules
type of display in module (i.e., point, bar, vbox, etc.)
The modules for each tab are organized into abstract syntax trees2 in the sections below.
28.1.1.1 Compare
The Compare tab uses two modules:
mod_compare_vars collects the variable and aesthetic inputs.
mod_compare_point takes the inputs and builds the plotly point plot.
Three new utility functions have been added to the application. The display_type() (Section 8.3.2) and test_logger() (Section 16.3) functions from previous sections are also included.
28.1.2.1 Name case
The name_case() function converts the column names to title case (and it’s used in most of the module functions):
name_case(x ="critics_rating", case ="title")## [1] "Critics Rating"
name_case(x ="Critics Rating", case ="lower")## [1] "critics rating"
28.1.2.2 Variable summary
The dist_var_summary() function creates the summary statistics table in the Distributions tab using the group and numerical variables.
dist_var_summary(data = movies, grp_var ='critics_rating', num_var ='audience_score')## # A tibble: 3 × 7## Group N Mean SD Min Max IQR## <fct> <int> <dbl> <dbl> <dbl> <dbl> <dbl>## 1 Rotten 307 49.7 17.9 11 95 28 ## 2 Certified F… 135 79.4 11.2 35 97 16.5## 3 Fresh 209 70.0 15.6 29 94 25
28.1.2.3 Movie awards
When provided an award type (Films or Stars) and start and end theater release dates, the create_movie_awards converts the movies data into the movie awards data for the graph and table the Awards tab:
create_movie_awards(df = movies,award ="Films", start_year =1990, end_year =1995)## # A tibble: 8 × 3## title award year## <chr> <chr> <dbl>## 1 The Age of Innocence Best Director 1993## 2 The Doors Best Director 1991## 3 The Man Without a Face Best Director 1993## 4 Heaven & Earth Best Director 1994## 5 The Wedding Banquet (Xi yan) Best Director 1993## 6 Jade Best Director 1995## 7 Point Break Best Director 1991## 8 Mighty Aphrodite Best Director 1995
28.2 Setting up chores
We’ll use the Anthropic’s Claude model for the chores add-in. This can be configured following the instructions on the package website.
chores comes with an add-in (an interface to write prompts) which I’ll be demonstrating in Positron 4
After adding the JSON to keybindings.json, the chores add-in will pop-up in the Viewer:
Positron chores add-in
28.3 Addin
chores comes with three helpers: cli, roxygen, testthat. Below is an example of the roxygen helper in Positron with our name_case() function:
Click to enlarge
chores doesn’t write comprehensive documentation, but it will
“generate a subset of a complete documentation entry, to be then completed by a developer:”
Specifically, it will write the @title, @description, any arguments (i.e., @param), and what the function @returns. The chores helper will also export the function by default.
The chores helper returned the following roxygen2 documentation:
#' Change the case of text#'#' @param x A character vector.#' @param case One of `"title"` or `"lower"`. Optional.#'#' @returns#' A character vector with the case changed according to the specified format.#' Will error if input is not a character vector or if an unsupported case is specified.#'#' @export
The mod_compare_vars_ui() function is also demonstrated below:
TIP: Padding
I recommend placing about 10 lines of extra padding above the function you’re using chores to document. I’ve found this ensures the first few lines of the function aren’t overwritten by the helper response.
Click to enlarge
The roxygen2 documentation for the mod_compare_vars_ui() function is below:
#' UI module for comparing movie variables#'#' @param id A string. The module ID.#'#' @returns A UI element containing inputs for selecting variables to compare #' in a scatter plot, including X and Y variables, color variable, and#' customization controls for alpha, size, and plot title.#'#' @export
So far, all of the documentation I’ve generated using chores has been correct, and it puts me in a better starting place that the standard roxygen2 skeleton produced by RStudio. You can read more about the helper prompts in the chores package documentation.
Below we’ll cover how to add custom helper prompts that can be used with the chores addin.
28.4 Custom helpers
chores gives users the ability to add custom helpers (prompts). These prompts can be written in markdown and must be named either replace, prefix, or suffix.
replace: will remove the highlighted code and replace it with a response.
prefix: will place the response above the highlighted text.
suffix: will place the response below the highlighted text.
You are an expert Shiny developer who loves providing detailed explanations of complex topics to non-technical audiences.
Follow the tidyverse style guide:
Limit code to 80 characters per line
Place a space before and after =
Only use a single empty line when needed to separate sections
Always use double quotes for strings
Always use backticks for inline code
Use double quotes, not single quotes, for quoting text
Use base pipe |> (not %>%)
Reference UI/server functions using brackets
Use the following documentation for the aes() function from ggplot2 as an example:
#' Construct aesthetic mappings#'#' Aesthetic mappings describe how variables in the data are mapped to visual#' properties (aesthetics) of geoms. Aesthetic mappings can be set in#' [ggplot()] and in individual layers.#'#' This function also standardises aesthetic names by converting `color` to #' `colour` (also in substrings, e.g., `point_color` to `point_colour`) and#' translating old style R names to ggplot names (e.g., `pch` to `shape` #' and `cex` to `size`).
When documenting a UI function, reference the corresponding server function (and vice versa).
For example, mod_vars_ui() should reference mod_vars_server().
#' UI for count variables module#'#' Creates inputs for selecting a grouping variable. This function is designed#' to work together with [mod_vars_server()].#'#' @param id A character string used to identify the namespace for the module.#'#' @return A `tagList` containing UI elements:#' * A variable select input for the grouping variable#'#' @seealso [mod_vars_server()] for the server-side logic#'#' @examples#' # UI implementation#' ui <- fluidPage(#' mod_vars_ui('vars1')#' )#'#' # Server implementation#' server <- function(input, output, session) {#' vars <- mod_vars_server('vars1')#' }#'
Indicate if returned values are reactive.
Return responses in roxygen2 comments (no R code blocks)
Include 5 blank lines of ‘padding’ after all responses
To add a custom helper to the chores addin, you can provide the URL to a raw markdown file on GitHub using chores::prompt_new():
If you have a collection of repetitive tasks you’d like to include in the chores addin, you can create and install a small chores extension package. For example, I’ve created the chorrrin extension package.5
As you can see from the image below, I can highlight the module function I’d like to add log messages to, click Ctrl + Cmd + C, select the helper, and click Enter:
Example modlog helper in Positron
Recap
chores delivers on it’s intention to ’complete repetitive, hard-to-automate tasks quickly.” I used it to quickly generate documentation, log messages, and tests for the modules in this branch’s application. The generated responses still required revision/edits, but being able use use a keyboard shortcut and start with a first draft saves time (and neurons).
Simon Couch also the author of the ensure and gander packages.↩︎