Skip to content

Commit

Permalink
close #244, #243: new load_units_xml function
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Nov 7, 2020
1 parent 409b291 commit 9d7a87e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export(drop_units)
export(install_conversion_constant)
export(install_conversion_offset)
export(install_symbolic_unit)
export(load_units_xml)
export(make_unit)
export(make_unit_label)
export(make_units)
Expand Down
40 changes: 35 additions & 5 deletions R/database.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.startup_msg <- function(warn = FALSE) {
paste("udunits database from" , .get_ud_xml(warn))
paste("udunits database from" , default_units_xml(warn))
}

.get_ud_xml <- function(warn = FALSE) {
default_units_xml <- function(warn = FALSE) {
paths = c(
Sys.getenv("UDUNITS2_XML_PATH"),
"/usr/local/share/udunits/udunits2.xml",
Expand All @@ -24,17 +24,47 @@
p
}

.read_ud_db <- function(file = .get_ud_xml(), type) {
#' Load a unit system
#'
#' Load an XML database containing a unit system compatible with UDUNITS2.
#'
#' A unit system comprises a root \code{<unit-system>} and a number of children
#' defining prefixes (\code{<prefix>}) or units (\code{<unit>}).
#' See the contents of
#'
#' \code{system.file("share/udunits", package="units")}
#'
#' for examples.
#'
#' @param path a path to a valid unit system in XML format.
#'
#' @examples
#' # load a new unit system
#' load_units_xml(system.file("share/udunits/udunits2-base.xml", package="units"))
#' \dontrun{
#' set_units(1, rad) # doesn'twork
#' }
#'
#' # reload the default unit system
#' load_units_xml()
#' set_units(1, rad) # works again
#'
#' @export
load_units_xml <- function(path = default_units_xml()) {
udunits_init(path)
}

.read_ud_db <- function(path = default_units_xml(), type) {
if (! requireNamespace("xml2", quietly = TRUE))
stop("package 'xml2' is required for this functionality", call.=FALSE)

db <- xml2::read_xml(file)
db <- xml2::read_xml(path)

# resolve imports
imports <- xml2::xml_find_all(db, "import")
files <- sapply(imports, xml2::xml_text)
for (i in files) {
import <- xml2::read_xml(file.path(dirname(file), i))
import <- xml2::read_xml(file.path(dirname(path), i))
for (child in xml2::xml_children(import)) {
source <- gsub("udunits2-|\\.xml", "", i)
source <- paste0("<doc><source>", source, "</source></doc>")
Expand Down
3 changes: 2 additions & 1 deletion R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
NULL

.onLoad = function(libname, pkgname) {
udunits_init(.get_ud_xml())
load_units_xml()

if (ud_is_parseable("B"))
.default_options$define_bel <- FALSE
do.call(units_options, .default_options)
Expand Down
35 changes: 35 additions & 0 deletions man/load_units_xml.Rd

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

0 comments on commit 9d7a87e

Please sign in to comment.