/
constants.R
87 lines (76 loc) · 2.38 KB
/
constants.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# all the constants used by this package
.onLoad <- function(libname, pkgname) {
bar_per_atm <- 1.01325
# constants
opts <- list(
# base units
base_units = c(
"quantity" = NA_character_,
"amount" = "mol",
"mass" = "g",
"molecular_weight" = "g/mol",
"molarity_concentration" = "M",
"mass_concentration" = "g/L",
"volume" = "L",
"pressure" = "bar",
"gas_solubility" = "M/bar",
"temperature" = "K"
),
# abbreviation
abbreviations = c(
"quantity" = "qty",
"amount" = "N",
"mass" = "m",
"molecular_weight" = "MW",
"molarity_concentration" = "C",
"mass_concentration" = "C",
"volume" = "V",
"pressure" = "P",
"gas_solubility" = "S",
"temperature" = "T"
),
# metric prefixes
metric_prefix =
rlang::set_names(
c(1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1, 1e3, 1e6, 1e9, 1e12),
c("f", "p", "n", stringi::stri_encode("\U00B5"), "m", "", "k", "M", "G", "T")
),
# pressure units
bar_per_pa = 1e-5,
bar_per_atm = bar_per_atm,
bar_per_psi = 1/14.50377,
bar_per_Torr = 1/760 * bar_per_atm,
# temperature units
celsius_kelvin_offset = -273.15,
fahrenheit_celsius_offset = 32,
fahrenheit_celsius_slope = 9/5,
# physical constants
R_in_L_bar_per_K_mol = 0.08314462 # ideal gas constant in the units used as base units by microbialkitchen (L bar K-1 mol-1)
)
names(opts) <- paste0("microbialkitchen_", names(opts))
options(opts)
}
#' Constants
#'
#' List and retrieve constants used in microbialkitchen.
#'
#' @name constants
NULL
#' @describeIn constants get the value of a constant
#' @param name name of the constant
#' @export
get_microbialkitchen_constant <- function(name) {
value <- getOption(paste0("microbialkitchen_", name))
if (is.null(value)) stop("constant ", name, " is not specified")
return(value)
}
#' @describeIn constants list all constants
#' @export
get_microbialkitchen_constants <- function() {
opts <- options() %>% {.[names(.) %>% stringr::str_detect("^microbialkitchen_")]}
tibble(
constant = names(opts) %>% stringr::str_replace("^microbialkitchen_", ""),
key = purrr::map(opts, names) %>% purrr::map( ~ if (is.null(.x)) { NA_character_ } else { .x }),
value = purrr::map(opts, identity)
) %>% tidyr::unnest(.data$key, .data$value)
}