-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array of 1 not represented as list in R #69
Comments
The easiest way to accomplish what you want at this moment is to use a combination of a custom handler and setting the str(yaml.load("{ test: [123, 456] }", handlers = list(seq = function(x) x), as.named.list=FALSE))
#> List of 1
#> $ :List of 2
#> ..$ : int 123
#> ..$ : int 456
#> - attr(*, "keys")=List of 1
#> ..$ : chr "test" It might be a good idea to add a more user-friendly parameter, though. |
Check out the documentation for |
is a good solution for this issue (I indeed still need the list names), but as mentioned above it would be nice to have an option similar to jsonlite’s |
I don't like the idea of only implementing it in The above solution works by disabling coercion of YAML sequences from lists to vectors by way of a custom handler that does nothing. Having a user-friendly option just for disabling coercion of YAML sequences seems a bit too limited in scope to deserve its own parameter. I think having a more general way to disable default handlers would be more appropriate. Maybe something like: yaml.load("{ test: [123, 456] }", pristine = c("seq")) The |
I can't see a lot of anything going on in the default handlers except for "seq". I feel like I'm missing something reading the code. |
I think it would be important for the default settings to let a YAML input survive a Currently, length-1 YAML sequences get "simplified" during a round trip which is not what most people might expect: tmp_file <- tempfile(fileext = ".yml")
cat("key: value
array1:
- item
array2:
- item1
- item2
",
file = tmp_file)
# before yaml round trip
readLines(con = tmp_file) |> cat(sep = "\n")
#> key: value
#> array1:
#> - item
#> array2:
#> - item1
#> - item2
yaml::yaml.load_file(input = tmp_file) |> yaml::write_yaml(file = tmp_file)
# after yaml round trip
readLines(con = tmp_file) |> cat(sep = "\n")
#> key: value
#> array1: item
#> array2:
#> - item1
#> - item2 Created on 2023-06-18 with reprex v2.0.2 |
- avoid default YAML sequence simplification, cf. vubiostat/r-yaml#69 - use base R pipe and anonymous fns where possible - refactor code and improve readability - extend `process_pkg()` doc - other tweaks
When we parse below example with
yaml.load
we get
I would expect to have the option that makes sure
array1
is a list of 1 in this case, representing it was an array of length 1.Just as a reference when I do the same thing for
json
injsonlite
Note: when using
jsonlite::read_json
thesimplifyVector
argument is set toFALSE
by defaultCould we add a similar
simplifyVector = FALSE
option to force the R object to represent the original structure?(applies to rstudio/plumber#390 )
The text was updated successfully, but these errors were encountered: