Skip to content

Commit 20e7d56

Browse files
committed
keep R file convention with test file
1 parent 295fe84 commit 20e7d56

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

R/na-validation.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
check_params_for_na <- function(params) {
2+
# Recursively check for NA values
3+
check_na_recursive <- function(x, path = "") {
4+
if (is.list(x)) {
5+
for (i in seq_along(x)) {
6+
name <- names(x)[i] %||% as.character(i)
7+
new_path <- if (path == "") name else paste0(path, "$", name)
8+
check_na_recursive(x[[i]], new_path)
9+
}
10+
} else if (any(is.na(x) & !is.nan(x))) {
11+
# Found NA values (excluding NaN which is mathematically valid)
12+
na_positions <- which(is.na(x) & !is.nan(x))
13+
n_na <- length(na_positions)
14+
15+
cli::cli_abort(c(
16+
"{.code NA} values detected in parameter {.field {path}}",
17+
"x" = "Found NA at position{if (n_na > 1) 's' else ''}: {.val {na_positions}}",
18+
"i" = "Quarto CLI uses YAML 1.2 spec which cannot process R's {.code NA} values",
19+
"i" = "R's {.code NA} gets converted to YAML strings (like {.code .na.real}) that Quarto doesn't recognize as missing values",
20+
" " = "Consider these alternatives:",
21+
"*" = "Remove NA values from your data before passing to Quarto",
22+
"*" = "Use {.code NULL} instead of {.code NA} for missing optional parameters",
23+
"*" = "Handle missing values within your document code using conditional logic"
24+
))
25+
}
26+
}
27+
28+
check_na_recursive(params)
29+
}

R/utils.R

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,6 @@ yaml_handlers <- list(
1414
}
1515
)
1616

17-
check_params_for_na <- function(params) {
18-
# Recursively check for NA values
19-
check_na_recursive <- function(x, path = "") {
20-
if (is.list(x)) {
21-
for (i in seq_along(x)) {
22-
name <- names(x)[i] %||% as.character(i)
23-
new_path <- if (path == "") name else paste0(path, "$", name)
24-
check_na_recursive(x[[i]], new_path)
25-
}
26-
} else if (any(is.na(x) & !is.nan(x))) {
27-
# Found NA values (excluding NaN which is mathematically valid)
28-
na_positions <- which(is.na(x) & !is.nan(x))
29-
n_na <- length(na_positions)
30-
31-
cli::cli_abort(c(
32-
"{.code NA} values detected in parameter {.field {path}}",
33-
"x" = "Found NA at position{if (n_na > 1) 's' else ''}: {.val {na_positions}}",
34-
"i" = "Quarto parameters cannot contain NA values",
35-
" " = "Consider these alternatives:",
36-
"*" = "Remove NA values from your data before passing to Quarto",
37-
"*" = "Use {.code NULL} instead of {.code NA} for missing optional parameters",
38-
"*" = "Handle missing values within your document code using conditional logic"
39-
))
40-
}
41-
}
42-
43-
check_na_recursive(params)
44-
}
45-
4617
#' @importFrom yaml as.yaml
4718
as_yaml <- function(x) {
4819
yaml::as.yaml(x, handlers = yaml_handlers)

tests/testthat/_snaps/na-validation.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
Error in `check_na_recursive()`:
77
! `NA` values detected in parameter values
88
x Found NA at position: 2
9-
i Quarto parameters cannot contain NA values
9+
i Quarto CLI uses YAML 1.2 spec which cannot process R's `NA` values
10+
i R's `NA` gets converted to YAML strings (like `.na.real`) that Quarto doesn't recognize as missing values
1011
Consider these alternatives:
1112
* Remove NA values from your data before passing to Quarto
1213
* Use `NULL` instead of `NA` for missing optional parameters
@@ -20,7 +21,8 @@
2021
Error in `check_na_recursive()`:
2122
! `NA` values detected in parameter data$subset
2223
x Found NA at position: 2
23-
i Quarto parameters cannot contain NA values
24+
i Quarto CLI uses YAML 1.2 spec which cannot process R's `NA` values
25+
i R's `NA` gets converted to YAML strings (like `.na.real`) that Quarto doesn't recognize as missing values
2426
Consider these alternatives:
2527
* Remove NA values from your data before passing to Quarto
2628
* Use `NULL` instead of `NA` for missing optional parameters
@@ -34,7 +36,8 @@
3436
Error in `check_na_recursive()`:
3537
! `NA` values detected in parameter x
3638
x Found NA at positions: 2 and 4
37-
i Quarto parameters cannot contain NA values
39+
i Quarto CLI uses YAML 1.2 spec which cannot process R's `NA` values
40+
i R's `NA` gets converted to YAML strings (like `.na.real`) that Quarto doesn't recognize as missing values
3841
Consider these alternatives:
3942
* Remove NA values from your data before passing to Quarto
4043
* Use `NULL` instead of `NA` for missing optional parameters

0 commit comments

Comments
 (0)