https://github.com/simonpcouch/ensure
LLM-assisted unit testing for R
https://github.com/simonpcouch/ensure
Last synced: 2 months ago
JSON representation
LLM-assisted unit testing for R
- Host: GitHub
- URL: https://github.com/simonpcouch/ensure
- Owner: simonpcouch
- License: other
- Created: 2024-11-13T20:14:48.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-01-24T22:04:00.000Z (3 months ago)
- Last Synced: 2025-01-24T22:24:12.003Z (3 months ago)
- Language: R
- Homepage: https://simonpcouch.github.io/ensure/
- Size: 3.58 MB
- Stars: 27
- Watchers: 2
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.Rmd
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Support: .github/SUPPORT.md
Awesome Lists containing this project
- jimsghstars - simonpcouch/ensure - LLM-assisted unit testing for R (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# ensure
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=ensure)
[](https://github.com/simonpcouch/ensure/actions/workflows/R-CMD-check.yaml)The ensure package provides an addin for drafting testthat unit testing code using LLMs. Triggering the addin will open a corresponding test file and begin writing tests into it. The _ensurer_ is familiar with testthat 3e as well as tidy style, and incorporates context from the rest of your R package to write concise and relevant tests.
## Installation
You can install ensure like so:
``` r
pak::pak("simonpcouch/ensure")
```Then, make sure that you have an [`ANTHROPIC_API_KEY`](https://console.anthropic.com/) environment variable set, and you're ready to go. If you'd like to use an LLM other than Anthropic's Claude 3.5 Sonnet—like OpenAI's ChatGPT or a local ollama model—to power the ensurer, set the `.ensure_fn` and `.ensure_args` options in your `.Rprofile`, like `options(.ensure_fn = "chat_openai")` to use OpenAI’s GPT-4o or `options(.ensure_fn = "chat_openai", .ensure_args = list(model = "gpt-4o-mini"))` to use their GPT-4o-mini model.
The ensurer is interfaced with the via the RStudio addin "ensure: Test R code." For easiest access, we recommend registering the ensure addin to a keyboard shortcut. **In RStudio**, navigate to `Tools > Modify Keyboard Shortcuts > Search "ensure"`—we suggest `Ctrl+Alt+T` (or `Ctrl+Cmd+T` on macOS). The ensurer is currently not available in Positron as Positron has yet to implement document `id`s that the package needs to toggle between source and test files.
Once those steps are completed, you're ready to use the ensure addin with a keyboard shortcut.
## Example
To use the ensurer, just trigger the addin (optionally selecting some code to only write tests for a certain portion of the file) and watch your testing code be written.
```{r}
#| label: ensure-gif
#| echo: false
#| fig-alt: "A screencast of an RStudio session, where the user has a source file open. Several functions in the source file are selected and a keyboard shortcut is pressed, at which point a corresponding test file is created and drafts of unit tests are streamed into the file."
knitr::include_graphics("https://raw.githubusercontent.com/simonpcouch/ensure/refs/heads/main/inst/figs/ensure.gif")
```