diff --git a/NAMESPACE b/NAMESPACE index 2234f78f..a8c70edc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method("$",glossary) S3method("[",glossary) S3method(as.data.frame,glossary) S3method(as.list,digits) diff --git a/R/glossary.R b/R/glossary.R index 3016dc95..84412933 100644 --- a/R/glossary.R +++ b/R/glossary.R @@ -365,6 +365,14 @@ c.glossary <- function(..., recursive = FALSE) { structure(NextMethod(), class = class(x)) } +#' @export +`$.glossary` <- function(x, name, ...) { + if(!exists(name, x)) { + return(NULL) + } + x[[name]] +} + #' Select entries from a glossary object #' #' @param x a glossary object. diff --git a/tests/testthat/test-glossary.R b/tests/testthat/test-glossary.R index 08d32e21..f9ca02c9 100644 --- a/tests/testthat/test-glossary.R +++ b/tests/testthat/test-glossary.R @@ -280,6 +280,15 @@ test_that("extract glossary entry", { expect_identical(names(g3), c("b", "c")) }) +test_that("no partial matching when extracting glossary entry", { + glo <- as_glossary(abc = "apple", xyz = "pear") + expect_null(glo$a) + expect_null(glo$xy) + expect_null(glo[["ab"]]) + expect_is(glo$abc, "glossary_entry") + expect_identical(capture.output(glo$abc), "apple (abc)") +}) + test_that("select glossary items", { g1 <- as_glossary(list(a = "apple", b = "banana", c = "cat", d = "dog")) g2 <- select_glossary(g1, b, d)