Skip to content

Commit

Permalink
Merge pull request #597 from yjunechoe/yaml-write-tbl-name-error-msg
Browse files Browse the repository at this point in the history
More helpful error messages when YAML-writing agents/informants with a non-lazy tbl
  • Loading branch information
yjunechoe authored Feb 16, 2025
2 parents 5f499d3 + 110bf04 commit a822d10
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- `info_columns()` warn more informatively when no columns are selected (#589).

- `write_yaml()` errors more informatively when `tbl` value is incompatible for yaml-writing (#597)

- Data extracts for `rows_distinct()`/`rows_complete()` preserves all columns, not just the ones tested (#588, #591)

- The `brief` argument of validation functions now also supports `{glue}` syntax (#587)
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,8 @@ deparse_expr <- function(expr, collapse = " ", ...) {
paste("<expr>", deparsed)
}
}

string_is_valid_symbol <- function(x) {
rlang::is_scalar_character(x) &&
identical(make.names(x), x)
}
32 changes: 20 additions & 12 deletions R/yaml_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -966,12 +966,7 @@ prune_lst_step <- function(lst_step) {

as_agent_yaml_list <- function(agent, expanded) {

if (is.null(agent$read_fn)) {
stop(
"The agent must have a `tbl` value that can be put into YAML.",
call. = FALSE
)
}
check_lazy_tbl(agent, "agent")

action_levels_default <- as_action_levels(agent$actions)
end_fns <- agent$end_fns %>% unlist()
Expand Down Expand Up @@ -1574,12 +1569,7 @@ get_column_text <- function(step_list, expanded) {

as_informant_yaml_list <- function(informant) {

if (is.null(informant$read_fn)) {
stop(
"The informant must have a `tbl` value that can be put into YAML.",
call. = FALSE
)
}
check_lazy_tbl(informant, "informant")

lst_tbl_name <- to_list_tbl_name(informant$tbl_name)
lst_read_fn <- to_list_read_fn(informant$read_fn)
Expand Down Expand Up @@ -1664,3 +1654,21 @@ as_tbl_store_yaml_list <- function(tbl_store) {
lst_init # initialization statement
)
}

check_lazy_tbl <- function(x, type = c("agent", "informant")) {
type <- match.arg(type)
tbl_name <- x$tbl_name
if (is.null(x$read_fn)) {
cli::cli_abort(c(
"x" = "The {type} must have a `tbl` value that can be put into YAML.",
"i" = if (string_is_valid_symbol(tbl_name)) {
paste(
"Did you mean to pass the table lazily?",
"Ex: {.code create_{type}(tbl = ~ {tbl_name})}"
)
} else {
"See {.code ?yaml_write} for details."
}
), call = NULL)
}
}

0 comments on commit a822d10

Please sign in to comment.