Skip to content

Commit

Permalink
forefront addin interface (closes #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Oct 10, 2024
1 parent 2c0f58b commit 1dee2be
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 340 deletions.
103 changes: 39 additions & 64 deletions R/doc-pal-cli.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
#'
#' The cli pal helps you convert your R package to use cli for error messages.
#'
#' @section Creating a cli pal:
#'
#' Create a cli pal with:
#'
#' ```r
#' pal("cli")
#' ```
#'
#' @section Cost:
#'
# TODO: make this a template that takes in the token counts and prices as input
Expand All @@ -49,18 +41,10 @@
#' ["from the wild"](https://github.com/tidymodels/tune/blob/f8d734ac0fa981fae3a87ed2871a46e9c40d509d/R/checks.R)
#' and are generated with the default model, Claude Sonnet 3.5.
#'
#' ```r
#' library(pal)
#'
#' cli_pal <- pal("cli")
#' ```
#'
#' At its simplest, a one-line message with a little bit of markup:
#'
#' ```r
#' cli_pal$chat({
#' rlang::abort("`save_pred` can only be used if the initial results saved predictions.")
#' })
#' rlang::abort("`save_pred` can only be used if the initial results saved predictions.")
#' ```
#'
#' Returns:
Expand All @@ -72,17 +56,15 @@
#' Some strange vector collapsing and funky line breaking:
#'
#' ```r
#' cli_pal$chat({
#' extra_grid_params <- glue::single_quote(extra_grid_params)
#' extra_grid_params <- glue::glue_collapse(extra_grid_params, sep = ", ")
#' extra_grid_params <- glue::single_quote(extra_grid_params)
#' extra_grid_params <- glue::glue_collapse(extra_grid_params, sep = ", ")
#'
#' msg <- glue::glue(
#' "The provided `grid` has the following parameter columns that have ",
#' "not been marked for tuning by `tune()`: {extra_grid_params}."
#' )
#' msg <- glue::glue(
#' "The provided `grid` has the following parameter columns that have ",
#' "not been marked for tuning by `tune()`: {extra_grid_params}."
#' )
#'
#' rlang::abort(msg)
#' })
#' rlang::abort(msg)
#' ```
#'
#' Returns:
Expand All @@ -97,16 +79,14 @@
#' A message that probably best lives as two separate elements:
#'
#' ```r
#' cli_pal$chat({
#' rlang::abort(
#' paste(
#' "Some model parameters require finalization but there are recipe",
#' "parameters that require tuning. Please use ",
#' "`extract_parameter_set_dials()` to set parameter ranges ",
#' "manually and supply the output to the `param_info` argument."
#' )
#' rlang::abort(
#' paste(
#' "Some model parameters require finalization but there are recipe",
#' "parameters that require tuning. Please use ",
#' "`extract_parameter_set_dials()` to set parameter ranges ",
#' "manually and supply the output to the `param_info` argument."
#' )
#' })
#' )
#' ```
#'
#' Returns:
Expand All @@ -126,16 +106,14 @@
#' Gnarly ad-hoc pluralization:
#'
#' ```r
#' cli_pal$chat({
#' msg <- "Creating pre-processing data to finalize unknown parameter"
#' unk_names <- pset$id[unk]
#' if (length(unk_names) == 1) {
#' msg <- paste0(msg, ": ", unk_names)
#' } else {
#' msg <- paste0(msg, "s: ", paste0("'", unk_names, "'", collapse = ", "))
#' }
#' rlang::inform(msg)
#' })
#' msg <- "Creating pre-processing data to finalize unknown parameter"
#' unk_names <- pset$id[unk]
#' if (length(unk_names) == 1) {
#' msg <- paste0(msg, ": ", unk_names)
#' } else {
#' msg <- paste0(msg, "s: ", paste0("'", unk_names, "'", collapse = ", "))
#' }
#' rlang::inform(msg)
#' ```
#'
#' Returns:
Expand All @@ -149,13 +127,11 @@
#' Some `paste0()` wonk:
#'
#' ```r
#' cli_pal$chat({
#' rlang::abort(paste0(
#' "The workflow has arguments to be tuned that are missing some ",
#' "parameter objects: ",
#' paste0("'", pset$id[!params], "'", collapse = ", ")
#' ))
#' })
#' rlang::abort(paste0(
#' "The workflow has arguments to be tuned that are missing some ",
#' "parameter objects: ",
#' paste0("'", pset$id[!params], "'", collapse = ", ")
#' ))
#' ```
#'
#' Returns:
Expand All @@ -171,16 +147,14 @@
#' erroring code that's run conditionally can get borked:
#'
#' ```r
#' cli_pal$chat({
#' cls <- paste(cls, collapse = " or ")
#' if (!fine) {
#' msg <- glue::glue("Argument '{deparse(cl$x)}' should be a {cls} or NULL")
#' if (!is.null(where)) {
#' msg <- glue::glue(msg, " in `{where}`")
#' }
#' rlang::abort(msg)
#' cls <- paste(cls, collapse = " or ")
#' if (!fine) {
#' msg <- glue::glue("Argument '{deparse(cl$x)}' should be a {cls} or NULL")
#' if (!is.null(where)) {
#' msg <- glue::glue(msg, " in `{where}`")
#' }
#' })
#' rlang::abort(msg)
#' }
#' ```
#'
#' Returns:
Expand All @@ -196,9 +170,7 @@
#' Sprintf-style statements aren't an issue:
#'
#' ```r
#' cli_pal$chat({
#' abort(sprintf("No such '%s' function: `%s()`.", package, name))
#' })
#' abort(sprintf("No such '%s' function: `%s()`.", package, name))
#' ```
#'
#' Returns:
Expand All @@ -207,5 +179,8 @@
#' cli::cli_abort("No such {.pkg {package}} function: {.fn {name}}.")
#' ```
#'
#' @templateVar role cli
#' @template manual-interface
#'
#' @name pal_cli
NULL
101 changes: 48 additions & 53 deletions R/doc-pal-roxygen.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
#' errors and warnings users might encounter.
#'
#'
#' @section Creating a roxygen pal:
#'
#' Create a roxygen pal with:
#'
#' ```r
#' pal("roxygen")
#' ```
#'
#' @section Cost:
#'
#' The system prompt from a roxygen pal includes something like 1,000 tokens.
Expand All @@ -43,35 +35,31 @@
#' "[from the wild](https://github.com/hadley/elmer/tree/e497d627e7be01206df6f1420ca36235141dc22a/R)"
#' and are generated with the default model, Claude Sonnet 3.5.
#'
#' ```r
#' library(pal)
#' Documenting a function factory:
#'
#' roxygen_pal <- pal("roxygen")
#' ```r
#' deferred_method_transform <- function(lambda_expr, transformer, eval_env) {
#' transformer <- enexpr(transformer)
#' force(eval_env)
#'
#' unique_id <- new_id()
#' env_bind_lazy(
#' generators,
#' !!unique_id := inject((!!transformer)(!!lambda_expr)),
#' eval.env = eval_env
#' )
#'
#' inject(
#' function(...) {
#' (!!generators)[[!!unique_id]](self, private, ...)
#' }
#' )
#' }
#' ```
#'
#' Documenting a function factory:
#' Returns:
#'
#' ```r
#' roxygen_pal$chat({
#' deferred_method_transform <- function(lambda_expr, transformer, eval_env) {
#' transformer <- enexpr(transformer)
#' force(eval_env)
#'
#' unique_id <- new_id()
#' env_bind_lazy(
#' generators,
#' !!unique_id := inject((!!transformer)(!!lambda_expr)),
#' eval.env = eval_env
#' )
#'
#' inject(
#' function(...) {
#' (!!generators)[[!!unique_id]](self, private, ...)
#' }
#' )
#' }
#' })
#'
#' #' #' Transform a deferred method
#' #'
#' #' @description
Expand All @@ -91,19 +79,21 @@
#' A function that may raise a condition:
#'
#' ```r
#' roxygen_pal$chat({
#' set_default <- function(value, default, arg = caller_arg(value)) {
#' if (is.null(value)) {
#' if (!is_testing() || is_snapshot()) {
#' cli::cli_inform("Using {.field {arg}} = {.val {default}}.")
#' }
#' default
#' } else {
#' value
#' set_default <- function(value, default, arg = caller_arg(value)) {
#' if (is.null(value)) {
#' if (!is_testing() || is_snapshot()) {
#' cli::cli_inform("Using {.field {arg}} = {.val {default}}.")
#' }
#' default
#' } else {
#' value
#' }
#' })
#' }
#' ```
#'
#' Returns:
#'
#' ```r
#' #' Set default value
#' #'
#' #' @description
Expand All @@ -123,20 +113,22 @@
#' A function with some tricky indexing:
#'
#' ```r
#' roxygen_pal$chat({
#' find_index <- function(left, e_right) {
#' if (!is.list(e_right) || !has_name(e_right, "index") || !is.numeric(e_right$index)) {
#' return(NA)
#' }
#' find_index <- function(left, e_right) {
#' if (!is.list(e_right) || !has_name(e_right, "index") || !is.numeric(e_right$index)) {
#' return(NA)
#' }
#'
#' matches_idx <- map_lgl(left, function(e_left) e_left$index == e_right$index)
#' if (sum(matches_idx) != 1) {
#' return(NA)
#' }
#' which(matches_idx)[[1]]
#' matches_idx <- map_lgl(left, function(e_left) e_left$index == e_right$index)
#' if (sum(matches_idx) != 1) {
#' return(NA)
#' }
#' })
#' which(matches_idx)[[1]]
#' }
#' ```
#'
#' Returns:
#'
#' ```
#' #' Find matching index
#' #'
#' #' @description
Expand All @@ -152,5 +144,8 @@
#' #' @export
#' ```
#'
#' @templateVar role roxygen
#' @template manual-interface
#'
#' @name pal_roxygen
NULL
Loading

0 comments on commit 1dee2be

Please sign in to comment.