| 1 |
`%|%` <- function(x, y) ifelse(is.na(x), y, x) |
| 2 |
|
| 3 |
`%empty%` <- function(x, y) if(length(x)==0) y else x |
| 4 |
|
| 5 |
pcc <- function(...) paste0(..., collapse = ", ") |
| 6 |
|
| 7 |
#' Get information about valid units |
| 8 |
#' |
| 9 |
#' These functions require the \pkg{xml2} package, and return data frames with |
| 10 |
#' complete information about pre-defined units from UDUNITS2. Inspect this data |
| 11 |
#' frames to determine what inputs are accepted by \code{as_units} (and the |
| 12 |
#' other functions it powers: \code{as_units}, \code{set_units} , \code{units<-}). |
| 13 |
#' |
| 14 |
#' Any entry listed under \code{symbol} , \code{symbol_aliases} , \code{ |
| 15 |
#' name_singular} , \code{name_singular_aliases} , \code{name_plural} , or |
| 16 |
#' \code{name_plural_aliases} is valid. Additionally, any entry under |
| 17 |
#' \code{symbol} or \code{symbol_aliases} may can also contain a valid prefix, |
| 18 |
#' as specified by \code{valid_udunits_prefixes()} . |
| 19 |
#' |
| 20 |
#' Note, this is primarily intended for interactive use, the exact format of the |
| 21 |
#' returned data frames may change in the future. |
| 22 |
#' |
| 23 |
#' @param quiet logical, defaults \code{TRUE} to give a message about the location of |
| 24 |
#' the udunits database being read. |
| 25 |
#' |
| 26 |
#' @return a data frame with columns \code{symbol} , \code{symbol_aliases} , |
| 27 |
#' \code{name_singular} , \code{name_singular_aliases} , \code{name_plural} , |
| 28 |
#' or \code{name_plural_aliases} , \code{def} , \code{definition} , |
| 29 |
#' \code{comment} , \code{dimensionless} and \code{source_xml} |
| 30 |
#' |
| 31 |
#' @export |
| 32 |
#' |
| 33 |
#' @name valid_udunits |
| 34 |
#' @examples |
| 35 |
#' if (requireNamespace("xml2", quietly = TRUE)) { |
| 36 |
#' valid_udunits() |
| 37 |
#' valid_udunits_prefixes() |
| 38 |
#' if(interactive()) |
| 39 |
#' View(valid_udunits()) |
| 40 |
#' } |
| 41 |
valid_udunits <- function(quiet = FALSE) { |
| 42 |
db <- .read_ud_db(type="unit") |
| 43 |
if (!quiet) message(.startup_msg()) |
| 44 |
|
| 45 |
xml_nodes <- xml2::xml_children(db) |
| 46 |
|
| 47 |
l <- lapply(seq_len(xml2::xml_length(db)), function(i) { |
| 48 |
unit <- xml_nodes[[i]] |
| 49 |
|
| 50 |
source_xml <- xml2::xml_text(xml2::xml_find_first(unit, ".//source")) |
| 51 |
|
| 52 |
symbols <- xml2::xml_find_all(unit, ".//symbol") |
| 53 |
symbols <- xml2::xml_text(symbols) %empty% "" |
| 54 |
symbol <- symbols[ 1] |
| 55 |
symbol_aliases <- pcc(symbols[-1]) |
| 56 |
|
| 57 |
unit_names <- xml2::xml_find_all(unit, ".//name") |
| 58 |
|
| 59 |
all_names <- unlist(lapply(unit_names, function(.x) |
| 60 |
xml2::xml_text(xml2::xml_children(.x)))) |
| 61 |
|
| 62 |
singular <- xml2::xml_find_all(unit_names, ".//singular") |
| 63 |
singular <- xml2::xml_text(singular) |
| 64 |
|
| 65 |
plural <- xml2::xml_find_all(unit_names, ".//plural") |
| 66 |
plural <- xml2::xml_text(plural) |
| 67 |
|
| 68 |
name_singular <- singular[ 1] %|% "" |
| 69 |
name_singular_aliases <- pcc(singular[-1]) %|% "" |
| 70 |
|
| 71 |
name_plural <- plural[ 1] %|% "" |
| 72 |
name_plural_aliases <- pcc(plural[-1]) %|% "" |
| 73 |
|
| 74 |
def <- xml2::xml_find_all(unit, ".//def") |
| 75 |
def <- xml2::xml_text(def) %empty% "" |
| 76 |
|
| 77 |
definition <- xml2::xml_find_all(unit, ".//definition") |
| 78 |
definition <- xml2::xml_text(definition) %empty% "" |
| 79 |
|
| 80 |
comment <- xml2::xml_find_all(unit, ".//comment") |
| 81 |
comment <- xml2::xml_text(comment) %empty% "" |
| 82 |
|
| 83 |
dimensionless <- xml2::xml_find_all(unit, ".//dimensionless") |
| 84 |
dimensionless <- as.logical(length(dimensionless)) |
| 85 |
|
| 86 |
# all node names that might be in a unit node |
| 87 |
# db %>% xml_children() %>% map(~xml_children(.x) %>% xml_name()) %>% |
| 88 |
# unique() %>% unlist() %>% unique() |
| 89 |
# [1] "base" "name" "symbol" |
| 90 |
# [4] "aliases" "definition" "def" |
| 91 |
# [7] "comment" "dimensionless" |
| 92 |
|
| 93 |
# rest_xml <- unit %>% xml_children() |
| 94 |
# rest <- map(rest_xml, xml_text) |
| 95 |
# names(rest) <- rest_xml %>% xml_name() |
| 96 |
# rest <- list(rest) |
| 97 |
|
| 98 |
data.frame(symbol, symbol_aliases, |
| 99 |
name_singular, name_singular_aliases, |
| 100 |
name_plural, name_plural_aliases, |
| 101 |
def, definition, comment, dimensionless, source_xml, #, rest |
| 102 |
stringsAsFactors = FALSE) |
| 103 |
|
| 104 |
}) |
| 105 |
|
| 106 |
df <- do.call(rbind, c(l, stringsAsFactors=FALSE, make.row.names=FALSE)) |
| 107 |
class(df) <- c( "tbl_df", "tbl", "data.frame") |
| 108 |
df |
| 109 |
} |
| 110 |
|
| 111 |
#' @name valid_udunits |
| 112 |
#' @export |
| 113 |
valid_udunits_prefixes <- function(quiet = FALSE) { |
| 114 |
db <- .read_ud_db(type="prefix") |
| 115 |
if (!quiet) message(.startup_msg()) |
| 116 |
|
| 117 |
# all prefix valid names |
| 118 |
# db %>% xml_children() %>% map(~xml_children(.x) %>% xml_name()) %>% |
| 119 |
# unlist() %>% unique() |
| 120 |
# "value" "name" "symbol" |
| 121 |
|
| 122 |
l <- lapply( xml2::xml_children(db), function(prefix) { |
| 123 |
|
| 124 |
symbols <- xml2::xml_find_all(prefix, ".//symbol") |
| 125 |
symbols <- xml2::xml_text(symbols) |
| 126 |
symbol <- symbols[1] |
| 127 |
symbol_aliases <- pcc(symbols[-1]) |
| 128 |
|
| 129 |
name <- xml2::xml_find_all(prefix, ".//name") |
| 130 |
name <- xml2::xml_text(name) |
| 131 |
|
| 132 |
value <- xml2::xml_find_all(prefix, ".//value") |
| 133 |
value <- xml2::xml_double(value) |
| 134 |
|
| 135 |
data.frame(symbol, symbol_aliases, name, value) |
| 136 |
}) |
| 137 |
|
| 138 |
df <- do.call(rbind.data.frame, |
| 139 |
c(l, stringsAsFactors = FALSE, make.row.names = FALSE)) |
| 140 |
class(df) <- c( "tbl_df", "tbl", "data.frame") |
| 141 |
df |
| 142 |
} |