From 52642ccc4b0882ef8e4c5d9dd1ccbb23040c193e Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sat, 15 Feb 2025 16:13:52 -0500 Subject: [PATCH 1/4] more helpful error messages when yaml-writing with non-lazy tbl --- R/utils.R | 5 +++++ R/yaml_write.R | 33 +++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/R/utils.R b/R/utils.R index e47881f74..047f61e0c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1732,3 +1732,8 @@ deparse_expr <- function(expr, collapse = " ", ...) { paste("", deparsed) } } + +string_is_valid_symbol <- function(x) { + rlang::is_scalar_character(x) && + identical(make.names(x), x) +} diff --git a/R/yaml_write.R b/R/yaml_write.R index ac62e42af..3a95adfc8 100644 --- a/R/yaml_write.R +++ b/R/yaml_write.R @@ -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() @@ -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) @@ -1664,3 +1654,22 @@ 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?", + "{.code create_{type}(tbl = ~ {tbl_name})}" + ) + } else { + "See {.code ?yaml_write} for details." + } + ), call = NULL) + } +} + From b5faf30cc166cacf8a2e15bd9f50a32dc76d431f Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sat, 15 Feb 2025 16:21:09 -0500 Subject: [PATCH 2/4] tweak msg --- R/yaml_write.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/yaml_write.R b/R/yaml_write.R index 3a95adfc8..ef7d308ba 100644 --- a/R/yaml_write.R +++ b/R/yaml_write.R @@ -1664,7 +1664,7 @@ check_lazy_tbl <- function(x, type = c("agent", "informant")) { "i" = if (string_is_valid_symbol(tbl_name)) { paste( "Did you mean to pass the table lazily?", - "{.code create_{type}(tbl = ~ {tbl_name})}" + "Ex: {.code create_{type}(tbl = ~ {tbl_name})}" ) } else { "See {.code ?yaml_write} for details." From d08b6e8807c6d1954cc1d8f639e756480c8bdff0 Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sat, 15 Feb 2025 21:26:56 -0500 Subject: [PATCH 3/4] fix lint --- R/yaml_write.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/yaml_write.R b/R/yaml_write.R index ef7d308ba..a1167be57 100644 --- a/R/yaml_write.R +++ b/R/yaml_write.R @@ -1672,4 +1672,3 @@ check_lazy_tbl <- function(x, type = c("agent", "informant")) { ), call = NULL) } } - From 110bf0412956b5f6c71349425deaed487639f6c2 Mon Sep 17 00:00:00 2001 From: yjunechoe Date: Sat, 15 Feb 2025 21:27:48 -0500 Subject: [PATCH 4/4] add news item --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 69d7dc6c4..1b1e69e76 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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)