Skip to content
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

add simplify argument to eml_get #304

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions R/eml_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#' If multiple occurrences are found, will extract all
#' @param from explicit type for the input format. Possible values:
#' "xml", "json", "list", or "guess" with "list" as the default.
#' @param simplify simplify return value into a list (`TRUE`)
#' or return an S3 `emld` typed object (`FALSE`).
#' @param ... additional arguments
#'
#' @examples
Expand All @@ -22,10 +24,17 @@
#' @importFrom jqr jq combine
#' @importFrom emld as_json as_emld
#' @importFrom jsonlite fromJSON
eml_get <- function(x, element, from = "list", ...) {
eml_get <- function(x, element, from = "list", simplify = FALSE, ...) {
doc <- as.character(emld::as_json(emld::as_emld(x, from = from)))
out <- jqr::jq(doc, paste0("..|.", element, "? // empty"))
json <- jqr::combine(out)
robj <- jsonlite::fromJSON(json, simplifyVector = FALSE)
emld::as_emld(robj)

if (simplify & !is.atomic(robj)){
robj$`@context` <- NULL
}
else if (!simplify){
robj <- emld::as_emld(robj)
}
return(robj)
}
5 changes: 4 additions & 1 deletion man/eml_get.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-eml_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ test_that("eml_get works on more calls", {
"attributeName"
)
})

test_that("eml_get can return a simple list output", {
my_eml <- read_eml(f)
y <- eml_get(my_eml, "attributeName", simplify = T)
expect_equal(class(y), "list")

y <- eml_get(
my_eml$dataset$dataTable$attributeList$attribute[[1]],
"attributeName",
simplify = T
)
})