These options are best set via chemunits_options()
and queried via get_chemunits_option()
. However, the base functions options()
and getOption()
work as well but require a chemunits.
prefix (the package name and a dot) for the option name. Setting an option to a value of NULL
means that the default is used.
Arguments
- ...
set package options, syntax identical to
options()
- x
name of the option to retrieve
Functions
chemunits_options()
: set option valuesget_chemunits_option()
: retrieve the current value of an option
Options for the chemunits package
preferred_units
(character vector of units): the default units that new units (i.e. resulting from calculations) should be converted to. Default:"1"
(i.e. units that cancel themselves and should be dimensionless are converted to unitless). The order does not matter except if units can be interconverted from each other (e.g."L"
and"m^3"
) in which case the first one will be used (with a warning).auto_scale_units
(character vector of units): the units whose best SI prefix (e.g. nano/n, micro/u, milli/m, kilo/k, etc) should be determined automatically based on the median value of a vector in this unit. Default:character(0)
(i.e. no units are automatically scaled).
Examples
# Default setting(s):
old <- chemunits_options()
old # original options
#> $preferred_units
#> [1] "mol" "g" "g/mol" "M/bar" "M" "L" "1"
#>
#> $auto_scale_units
#> [1] "mol" "g" "g/mol" "M/bar" "M" "L"
#>
get_chemunits_option("preferred_units")
#> [1] "mol" "g" "g/mol" "M/bar" "M" "L" "1"
# with this, there is no default unit defined for energy or volume
# so the units are not simplified
set_cu(100, "W") * set_cu(1, "s")
#> 100 [W*s]
set_cu(1, "m") * set_cu(1, "mm^2")
#> 1 [mL]
# however the default does simplify dimensionless units
set_cu(1, "W*s") / set_cu(1, "J")
#> 1 [1]
# Change for the duration of the session:
chemunits_options(preferred_units = c("J", "L"))
# now W*s is recognized as energy and cm mm^2 as a volume and both are
# automatically converted to the specified default units
set_cu(100, "W") * set_cu(1, "s")
#> 100 [J]
set_cu(1, "m") * set_cu(1, "mm^2")
#> 1 [mL]
# however since "1" was no longer included in the `preferred_units`,
# dimensionless units were no longer simplified (thus it is advisable
# to always include "1" in the `preferred_units` option)
set_cu(1, "W*s") / set_cu(1, "J")
#> 1 [W*s/J]
# Restore original values
chemunits_options(old)