diff --git a/NEWS.md b/NEWS.md index 926b6d2..b6062e8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # plume (development version) +* `PlumeQuarto` now properly handles authors with no roles (#81). + * `PlumeQuarto` now supports `.yml` and `.yaml` files (#82). # plume 0.2.4 diff --git a/R/plume-quarto.R b/R/plume-quarto.R index d4dbb2a..ae5bb44 100644 --- a/R/plume-quarto.R +++ b/R/plume-quarto.R @@ -157,12 +157,12 @@ PlumeQuarto <- R6Class( if (!private$has_col(col)) { return() } - out <- unnest_drop(private$plume, cols = all_of(col)) - out <- summarise( - out, - `_` = list(tolower(.data[[col]])), - .by = all_of(private$id) - ) + out <- unnest(private$plume, cols = all_of(col)) + out <- summarise(out, `_` = if_not_na( + .data[[col]], + as_role_list(.data[[col]]), + all = TRUE + ), .by = all_of(private$id)) out[["_"]] }, @@ -243,6 +243,10 @@ PlumeQuarto <- R6Class( ) ) +as_role_list <- function(x) { + list(tolower(vec_drop_na(x))) +} + affiliation_keys <- c( "number", "name", "department", "address", "city", "region", "state", "country", "postal-code", "url", "isni", "ringgold", "ror" diff --git a/tests/testthat/_snaps/to-yaml.md b/tests/testthat/_snaps/to-yaml.md index deb4f8c..3f5ebde 100644 --- a/tests/testthat/_snaps/to-yaml.md +++ b/tests/testthat/_snaps/to-yaml.md @@ -231,6 +231,26 @@ given: Zip family: Zap +# to_yaml() properly handles authors with no roles (#81) + + Code + read_test_file(tmp_file) + Output + title: foo + author: + - id: aut1 + name: + given: A + family: A + roles: + - formal analysis + - writing - original draft + - id: aut2 + name: + given: B + family: B + roles: {} + # to_yaml() errors if no YAML headers is found Code diff --git a/tests/testthat/test-to-yaml.R b/tests/testthat/test-to-yaml.R index 1cdb14d..d04f13b 100644 --- a/tests/testthat/test-to-yaml.R +++ b/tests/testthat/test-to-yaml.R @@ -128,6 +128,26 @@ test_that("to_yaml() can push data into YAML files", { expect_snapshot(read_test_file(tmp_file)) }) +test_that("to_yaml() properly handles authors with no roles (#81)", { + tmp_file <- withr::local_tempfile( + lines = "title: foo", + fileext = ".yaml" + ) + + aut <- PlumeQuarto$new( + data.frame( + given_name = c("A", "B"), + family_name = c("A", "B"), + writing = c(1, NA), + analysis = c(1, NA) + ), + tmp_file + ) + aut$to_yaml() + + expect_snapshot(read_test_file(tmp_file)) +}) + # Errors ---- test_that("to_yaml() errors if no YAML headers is found", {