├── .Rbuildignore ├── .github └── workflows │ ├── build.yml │ └── pkgdown.yml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS.md ├── R └── constants.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── constants.Rproj ├── cran-comments.md ├── data ├── codata.cor.rda └── codata.rda ├── man ├── codata.Rd ├── constants-package.Rd ├── lookup.Rd └── syms.Rd ├── tests ├── testthat.R └── testthat │ └── test-constants.R └── wdir └── download.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^\.travis\.yml$ 4 | ^_pkgdown\.yml$ 5 | ^README\.Rmd$ 6 | ^README-.*\.png$ 7 | ^cran-comments\.md$ 8 | ^.*\.gcda$ 9 | ^.*\.gcno$ 10 | ^.*\.gcov$ 11 | wdir 12 | .gitmodules 13 | .gitignore 14 | .git 15 | .lintr 16 | ^docs$ 17 | TODO 18 | ^CRAN-RELEASE$ 19 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: build 4 | 5 | jobs: 6 | build: 7 | name: Check package 8 | uses: r-quantities/workflows/.github/workflows/build.yml@main 9 | secrets: inherit 10 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yml: -------------------------------------------------------------------------------- 1 | on: 2 | workflow_dispatch: 3 | release: 4 | types: [published] 5 | 6 | name: pkgdown 7 | 8 | jobs: 9 | pkgdown: 10 | name: Build and deploy site 11 | uses: r-quantities/workflows/.github/workflows/pkgdown.yml@main 12 | secrets: inherit 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | wdir/tmp 5 | 6 | # Compiled Object files 7 | *.slo 8 | *.lo 9 | *.o 10 | *.obj 11 | *.gcda 12 | *.gcno 13 | *.gcov 14 | # Precompiled Headers 15 | *.gch 16 | *.pch 17 | # Compiled Dynamic libraries 18 | *.so 19 | *.dylib 20 | *.dll 21 | # Fortran module files 22 | *.mod 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | # OSX-specific 33 | .DS_Store 34 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: constants 2 | Type: Package 3 | Title: Reference on Constants, Units and Uncertainty 4 | Version: 1.0.1 5 | Authors@R: c( 6 | person("Iñaki", "Ucar", email="iucar@fedoraproject.org", 7 | role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550"))) 8 | Description: CODATA internationally recommended values of the fundamental 9 | physical constants, provided as symbols for direct use within the R language. 10 | Optionally, the values with uncertainties and/or units are also provided if 11 | the 'errors', 'units' and/or 'quantities' packages are installed. 12 | The Committee on Data for Science and Technology (CODATA) is an 13 | interdisciplinary committee of the International Council for Science which 14 | periodically provides the internationally accepted set of values of the 15 | fundamental physical constants. This package contains the "2018 CODATA" 16 | version, published on May 2019: 17 | Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020) 18 | . 19 | License: MIT + file LICENSE 20 | Encoding: UTF-8 21 | LazyData: true 22 | URL: https://github.com/r-quantities/constants 23 | BugReports: https://github.com/r-quantities/constants/issues 24 | Depends: R (>= 3.5.0) 25 | Suggests: errors (>= 0.3.6), units, quantities, testthat 26 | ByteCompile: yes 27 | RoxygenNote: 7.1.1 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2017-2020 2 | COPYRIGHT HOLDER: Iñaki Ucar 3 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(lookup) 4 | export(syms) 5 | export(syms_with_errors) 6 | export(syms_with_quantities) 7 | export(syms_with_units) 8 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # constants 1.0.1 2 | 3 | - Fix compatibility with `units` 0.7-0. 4 | 5 | # constants 1.0.0 6 | 7 | Update to version 8.1, the 2018 CODATA recommended values (#7 addressing #6). 8 | This version contains some breaking changes that are necessary to streamline 9 | future updates and provide a stable symbol table: 10 | 11 | - The `codata` table includes the absolute uncertainty instead of the relative 12 | one. Thus, the `rel_uncertainty` column has been dropped in favour of the new 13 | `uncertainty`. Also, columns have been slightly reordered. 14 | - Symbol names for constants have changed. The old ones were hand-crafted and 15 | thus unmanageable. This release adopts the ASCII symbols defined by NIST in 16 | their webpage, except for those that collide with some base R function. In 17 | particular, there are two cases: `c`, the speed of light, has been renamed as 18 | `c0`; `sigma`, the Stefan-Boltzmann constant, has been renamed as `sigma0`. 19 | - Constant types, or categories, (column `codata$type`) adopts the names defined 20 | by NIST in the webpage too. Some constants belong to more than one category 21 | (separated by comma); some others belong to no category (missing type). 22 | 23 | There are some new features too: 24 | 25 | - In addition to the `codata` data frame, this release includes `codata.cor`, a 26 | correlation matrix for all the constants. 27 | - In addition to `syms_with_errors` and `syms_with_units`, there is a new list 28 | of symbols called `syms_with_quantities` (available if the optional 29 | `quantities` package is installed), which provides constant values with 30 | uncertainty **and** units. 31 | - Experimental support for correlated values in `syms_with_errors` and 32 | `syms_with_quantities` is provided (disabled by default; see details in 33 | `help(syms)` for activation instructions). 34 | 35 | # constants 0.0.2 36 | 37 | - Use `units::as_units()` instead of the deprecated `units::parse_unit()` (#1). 38 | - Install the speed of light as a unit (#2). 39 | - Unitless constants now show a `1` instead of `unitless` as unit (#3). 40 | - Unit `Ω` has been replaced by `ohm` (#4). 41 | -------------------------------------------------------------------------------- /R/constants.R: -------------------------------------------------------------------------------- 1 | #' \pkg{constants}: Reference on Constants, Units and Uncertainty 2 | #' 3 | #' This package provides the 2018 version of the CODATA internationally recommended 4 | #' values of the fundamental physical constants for their use within the \R language. 5 | #' 6 | #' @author Iñaki Ucar 7 | #' 8 | #' @references 9 | #' Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020). 10 | #' The 2018 CODATA Recommended Values of the Fundamental Physical Constants 11 | #' (Web Version 8.1). Database developed by J. Baker, M. Douma, and S. Kotochigova. 12 | #' Available at https://physics.nist.gov/cuu/Constants/, 13 | #' National Institute of Standards and Technology, Gaithersburg, MD 20899. 14 | #' 15 | #' @seealso \code{\link{codata}}, \code{\link{syms}}, \code{\link{lookup}}. 16 | #' 17 | #' @docType package 18 | #' @name constants-package 19 | NULL 20 | 21 | #' CODATA Recommended Values of the Fundamental Physical Constants: 2018 22 | #' 23 | #' The Committee on Data for Science and Technology (CODATA) is an interdisciplinary 24 | #' committee of the International Council for Science. The Task Group on Fundamental 25 | #' Constants periodically provides the internationally accepted set of values of 26 | #' the fundamental physical constants. This dataset contains the "2018 CODATA" 27 | #' version, published on May 2019. 28 | #' 29 | #' @format An object of class \code{data.frame} with the following information 30 | #' for each physical constant: 31 | #' ASCII \code{symbol}, \code{quantity} description, \code{type}, 32 | #' \code{value}, \code{uncertainty}, \code{unit}. 33 | #' 34 | #' @source 35 | #' Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020). 36 | #' The 2018 CODATA Recommended Values of the Fundamental Physical Constants 37 | #' (Web Version 8.1). Database developed by J. Baker, M. Douma, and S. Kotochigova. 38 | #' Available at https://physics.nist.gov/cuu/Constants/, 39 | #' National Institute of Standards and Technology, Gaithersburg, MD 20899. 40 | #' 41 | #' @seealso \code{\link{syms}}, \code{\link{lookup}}. 42 | "codata" 43 | 44 | #' @name codata 45 | #' @format A \code{matrix} of correlations between physical constants. 46 | "codata.cor" 47 | 48 | #' Lists of Constants 49 | #' 50 | #' These named lists contain ready-to-use values for all the fundamental 51 | #' physical constants. 52 | #' 53 | #' Experimental support for correlations between constants is provided via the 54 | #' \pkg{errors} package, but it is disabled by default. To enable it, the 55 | #' following option must be set before loading the package: 56 | #' 57 | #' \code{options(constants.correlations=TRUE)} 58 | #' 59 | #' Alternatively, \code{constants:::set_correlations()} may be used 60 | #' interactively, but scripts should not rely on this non-exported function, 61 | #' as it may disappear in future versions. 62 | #' 63 | #' @format 64 | #' A \code{list}, where names correspond to symbols in \code{codata$symbol}. 65 | #' \itemize{ 66 | #' \item \code{syms} contains plain numeric values. 67 | #' \item \code{syms_with_errors} contains objects of type \code{errors}, which 68 | #' enables automatic uncertainty propagation. 69 | #' \item \code{syms_with_units} contains objects of type \code{units}, which 70 | #' enables automatic conversion, derivation and simplification. 71 | #' \item \code{syms_with_quantities} contains objects of type \code{quantities}, 72 | #' which combines \code{errors} and \code{units}. 73 | #' } 74 | #' The enriched versions of \code{syms} are available only if the corresponding 75 | #' optional packages, \pkg{errors}, \pkg{units} and/or \pkg{quantities} are 76 | #' installed. See the documentation of these packages for further information. 77 | #' 78 | #' @seealso \code{\link{codata}}, \code{\link{lookup}}. 79 | #' 80 | #' @examples 81 | #' # the speed of light 82 | #' syms$c0 83 | #' # use the constants in a local environment 84 | #' with(syms, c0) 85 | #' 86 | #' # attach only Planck-related constants 87 | #' (lkp <- lookup("planck", ignore.case=TRUE)) 88 | #' idx <- as.integer(rownames(lkp)) 89 | #' attach(syms[idx]) 90 | #' h 91 | #' plkl 92 | #' 93 | #' # the same with uncertainty 94 | #' detach(syms[idx]) 95 | #' attach(syms_with_errors[idx]) 96 | #' h 97 | #' plkl 98 | #' 99 | #' # the same with units 100 | #' detach(syms_with_errors[idx]) 101 | #' attach(syms_with_units[idx]) 102 | #' h 103 | #' plkl 104 | #' 105 | #' # the same with everything 106 | #' detach(syms_with_units[idx]) 107 | #' attach(syms_with_quantities[idx]) 108 | #' h 109 | #' plkl 110 | #' 111 | #' @export 112 | syms <- NULL 113 | 114 | #' @name syms 115 | #' @format NULL 116 | #' @export 117 | syms_with_errors <- NULL 118 | 119 | #' @name syms 120 | #' @format NULL 121 | #' @export 122 | syms_with_units <- NULL 123 | 124 | #' @name syms 125 | #' @format NULL 126 | #' @export 127 | syms_with_quantities <- NULL 128 | 129 | set_correlations <- function() { 130 | stopifnot(requireNamespace("errors", quietly = TRUE)) 131 | 132 | n <- length(syms_with_errors) 133 | e.diag <- diag(constants::codata$uncertainty) 134 | codata.cov <- e.diag %*% constants::codata.cor %*% e.diag 135 | for (i in seq_len(n-1)) for (j in (i+1):n) { 136 | ijcov <- codata.cov[i, j] 137 | if (!ijcov) next 138 | errors::covar(syms_with_errors[[i]], syms_with_errors[[j]]) <- ijcov 139 | } 140 | } 141 | 142 | .onLoad <- function(libname, pkgname) { 143 | syms <<- as.list(constants::codata$value) 144 | names(syms) <<- as.vector(constants::codata$symbol) 145 | 146 | if (requireNamespace("errors", quietly = TRUE)) { 147 | syms_with_errors <<- Map( 148 | errors::set_errors, syms, constants::codata$uncertainty) 149 | 150 | if (getOption("constants.correlations", FALSE)) 151 | set_correlations() 152 | } 153 | 154 | if (requireNamespace("units", quietly = TRUE)) { 155 | # define the speed of light 156 | if (utils::packageVersion("units") < "0.7-0") { 157 | try(getExportedValue("units", "remove_symbolic_unit")("c"), silent=TRUE) 158 | getExportedValue("units", "install_conversion_constant")("c", "m/s", syms$c0) 159 | } else { 160 | getExportedValue("units", "remove_unit")("c") 161 | getExportedValue("units", "install_unit")("c", paste(syms$c0, "m/s")) 162 | } 163 | 164 | syms_with_units <<- Map( 165 | units::set_units, syms, constants::codata$unit, mode="standard") 166 | } 167 | 168 | if (requireNamespace("quantities", quietly = TRUE)) { 169 | syms_with_quantities <<- Map( 170 | units::set_units, syms_with_errors, constants::codata$unit, mode="standard") 171 | } 172 | } 173 | 174 | .onAttach <- function(libname, pkgname) { 175 | if (!requireNamespace("errors", quietly = TRUE)) 176 | packageStartupMessage(paste( 177 | "Package 'errors' not found.", 178 | "Constants with uncertainty ('syms_with_errors') not available.")) 179 | if (!requireNamespace("units", quietly = TRUE)) 180 | packageStartupMessage(paste( 181 | "Package 'units' not found.", 182 | "Constants with units ('syms_with_units') not available.")) 183 | if (!requireNamespace("quantities", quietly = TRUE)) 184 | packageStartupMessage(paste( 185 | "Package 'quantities' not found.", 186 | "Constants with uncertainty+units ('syms_with_quantities') not available.")) 187 | } 188 | 189 | #' Lookup for Fundamental Physical Constants 190 | #' 191 | #' A simple wrapper around \code{\link{grep}} for exploring the CODATA dataset. 192 | #' 193 | #' @param pattern character string containing a regular expression to be matched 194 | #' (see \code{\link{grep}}). 195 | #' @param cols columns to perform pattern matching (see \code{\link{codata}}). 196 | #' @param ... additional arguments for \code{\link{grep}}. 197 | #' 198 | #' @seealso \code{\link{codata}}, \code{\link{syms}}. 199 | #' 200 | #' @examples 201 | #' lookup("planck", ignore.case=TRUE) 202 | #' 203 | #' @export 204 | lookup <- function(pattern, cols=c("symbol", "quantity", "type"), ...) { 205 | cols <- match.arg(cols, several.ok = TRUE) 206 | ind <- do.call(c, lapply( 207 | cols, function(col) grep(pattern, constants::codata[[col]], ...))) 208 | constants::codata[sort(unique(ind)),] 209 | } 210 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, echo = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "README-" 12 | ) 13 | ``` 14 | 15 | # Reference on Constants, Units and Uncertainty 16 | 17 | 18 | [![Build Status](https://github.com/r-quantities/constants/workflows/build/badge.svg)](https://github.com/r-quantities/constants/actions) 19 | [![Coverage Status](https://codecov.io/gh/r-quantities/constants/branch/master/graph/badge.svg)](https://codecov.io/gh/r-quantities/constants) 20 | [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/constants)](https://cran.r-project.org/package=constants) 21 | [![Downloads](https://cranlogs.r-pkg.org/badges/constants)](https://cran.r-project.org/package=constants) 22 | 23 | 24 | The **constants** package provides the CODATA internationally recommended values of the fundamental physical constants, provided as symbols for direct use within the R language. Optionally, the values with uncertainties and/or units are also provided if the 'errors', 'units' and/or 'quantities' packages are installed. The Committee on Data for Science and Technology (CODATA) is an interdisciplinary committee of the International Council for Science which periodically provides the internationally accepted set of values of the fundamental physical constants. This package contains the "2018 CODATA" version, published on May 2019. 25 | 26 | > Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020). The 2018 CODATA Recommended Values of the Fundamental Physical Constants (Web Version 8.1). Database developed by J. Baker, M. Douma, and S. Kotochigova. Available at https://physics.nist.gov/cuu/Constants/, National Institute of Standards and Technology, Gaithersburg, MD 20899. 27 | 28 | ## Installation 29 | 30 | Install the release version from CRAN: 31 | 32 | ```{r, eval=FALSE} 33 | install.packages("constants") 34 | ``` 35 | 36 | The installation from GitHub requires the [remotes](https://cran.r-project.org/package=remotes) package. 37 | 38 | ```{r, eval=FALSE} 39 | # install.packages("remotes") 40 | remotes::install_github("r-quantities/constants") 41 | ``` 42 | 43 | ## Example 44 | 45 | ```{r} 46 | library(constants) 47 | 48 | # the speed of light 49 | syms$c0 50 | # use the constants in a local environment 51 | with(syms, c0) 52 | 53 | # explore which constants are available 54 | (lkp <- lookup("planck", ignore.case=TRUE)) 55 | idx <- as.integer(rownames(lkp)) 56 | 57 | # attach the symbols to the search path and use them explicitly 58 | attach(syms[idx]) 59 | h 60 | plkl 61 | 62 | # the same with uncertainty 63 | detach(syms[idx]) 64 | attach(syms_with_errors[idx]) 65 | h 66 | plkl 67 | 68 | # the same with units 69 | detach(syms_with_errors[idx]) 70 | attach(syms_with_units[idx]) 71 | h 72 | plkl 73 | 74 | # the same with everything 75 | detach(syms_with_units[idx]) 76 | attach(syms_with_quantities[idx]) 77 | h 78 | plkl 79 | 80 | # the whole dataset 81 | head(codata) 82 | 83 | # number of constants per type 84 | library(magrittr) 85 | codata %>% 86 | tidyr::separate_rows(type, sep=", ") %>% 87 | dplyr::count(type, sort=TRUE) 88 | ``` 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Reference on Constants, Units and Uncertainty 5 | 6 | 7 | [![Build 8 | Status](https://github.com/r-quantities/constants/workflows/build/badge.svg)](https://github.com/r-quantities/constants/actions) 9 | [![Coverage 10 | Status](https://codecov.io/gh/r-quantities/constants/branch/master/graph/badge.svg)](https://codecov.io/gh/r-quantities/constants) 11 | [![CRAN\_Status\_Badge](https://www.r-pkg.org/badges/version/constants)](https://cran.r-project.org/package=constants) 12 | [![Downloads](https://cranlogs.r-pkg.org/badges/constants)](https://cran.r-project.org/package=constants) 13 | 14 | 15 | The **constants** package provides the CODATA internationally 16 | recommended values of the fundamental physical constants, provided as 17 | symbols for direct use within the R language. Optionally, the values 18 | with uncertainties and/or units are also provided if the ‘errors’, 19 | ‘units’ and/or ‘quantities’ packages are installed. The Committee on 20 | Data for Science and Technology (CODATA) is an interdisciplinary 21 | committee of the International Council for Science which periodically 22 | provides the internationally accepted set of values of the fundamental 23 | physical constants. This package contains the “2018 CODATA” version, 24 | published on May 2019. 25 | 26 | > Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor 27 | > (2020). The 2018 CODATA Recommended Values of the Fundamental Physical 28 | > Constants (Web Version 8.1). Database developed by J. Baker, M. Douma, 29 | > and S. Kotochigova. Available at 30 | > , National Institute of 31 | > Standards and Technology, Gaithersburg, MD 20899. 32 | 33 | ## Installation 34 | 35 | Install the release version from CRAN: 36 | 37 | ``` r 38 | install.packages("constants") 39 | ``` 40 | 41 | The installation from GitHub requires the 42 | [remotes](https://cran.r-project.org/package=remotes) package. 43 | 44 | ``` r 45 | # install.packages("remotes") 46 | remotes::install_github("r-quantities/constants") 47 | ``` 48 | 49 | ## Example 50 | 51 | ``` r 52 | library(constants) 53 | 54 | # the speed of light 55 | syms$c0 56 | #> [1] 299792458 57 | # use the constants in a local environment 58 | with(syms, c0) 59 | #> [1] 299792458 60 | 61 | # explore which constants are available 62 | (lkp <- lookup("planck", ignore.case=TRUE)) 63 | #> symbol quantity 64 | #> 137 nah molar Planck constant 65 | #> 198 h Planck constant 66 | #> 199 hev Planck constant in eV/Hz 67 | #> 200 plkl Planck length 68 | #> 201 plkm Planck mass 69 | #> 202 plkmc2gev Planck mass energy equivalent in GeV 70 | #> 203 plktmp Planck temperature 71 | #> 204 plkt Planck time 72 | #> 230 hbar reduced Planck constant 73 | #> 231 hbarev reduced Planck constant in eV s 74 | #> 232 hbcmevf reduced Planck constant times c in MeV fm 75 | #> type value uncertainty unit 76 | #> 137 Physico-chemical 3.990313e-10 0.0e+00 J/Hz/mol 77 | #> 198 Universal, Adopted values 6.626070e-34 0.0e+00 J/Hz 78 | #> 199 Universal 4.135668e-15 0.0e+00 eV/Hz 79 | #> 200 Universal 1.616255e-35 1.8e-40 m 80 | #> 201 Universal 2.176434e-08 2.4e-13 kg 81 | #> 202 Universal 1.220890e+19 1.4e+14 GeV 82 | #> 203 Universal 1.416784e+32 1.6e+27 K 83 | #> 204 Universal 5.391247e-44 6.0e-49 s 84 | #> 230 Universal, Adopted values 1.054572e-34 0.0e+00 J*s 85 | #> 231 Universal 6.582120e-16 0.0e+00 eV*s 86 | #> 232 Universal, Non-SI units 1.973270e+02 0.0e+00 fm*MeV 87 | idx <- as.integer(rownames(lkp)) 88 | 89 | # attach the symbols to the search path and use them explicitly 90 | attach(syms[idx]) 91 | h 92 | #> [1] 6.62607e-34 93 | plkl 94 | #> [1] 1.616255e-35 95 | 96 | # the same with uncertainty 97 | detach(syms[idx]) 98 | attach(syms_with_errors[idx]) 99 | h 100 | #> 6.62607(0)e-34 101 | plkl 102 | #> 1.61626(2)e-35 103 | 104 | # the same with units 105 | detach(syms_with_errors[idx]) 106 | attach(syms_with_units[idx]) 107 | h 108 | #> 6.62607e-34 [J/Hz] 109 | plkl 110 | #> 1.616255e-35 [m] 111 | 112 | # the same with everything 113 | detach(syms_with_units[idx]) 114 | attach(syms_with_quantities[idx]) 115 | h 116 | #> 6.62607(0)e-34 [J/Hz] 117 | plkl 118 | #> 1.61626(2)e-35 [m] 119 | 120 | # the whole dataset 121 | head(codata) 122 | #> symbol quantity type 123 | #> 1 mal alpha particle mass Atomic and nuclear 124 | #> 2 malc2 alpha particle mass energy equivalent Atomic and nuclear 125 | #> 3 malc2mev alpha particle mass energy equivalent in MeV Atomic and nuclear 126 | #> 4 malu alpha particle mass in u Atomic and nuclear 127 | #> 5 mmal alpha particle molar mass Atomic and nuclear 128 | #> 6 malsme alpha particle-electron mass ratio Atomic and nuclear 129 | #> value uncertainty unit 130 | #> 1 6.644657e-27 2.0e-36 kg 131 | #> 2 5.971920e-10 1.8e-19 J 132 | #> 3 3.727379e+03 1.1e-06 MeV 133 | #> 4 4.001506e+00 6.3e-11 u 134 | #> 5 4.001506e-03 1.2e-12 kg/mol 135 | #> 6 7.294300e+03 2.4e-07 1 136 | 137 | # number of constants per type 138 | library(magrittr) 139 | codata %>% 140 | tidyr::separate_rows(type, sep=", ") %>% 141 | dplyr::count(type, sort=TRUE) 142 | #> # A tibble: 8 x 2 143 | #> type n 144 | #> 145 | #> 1 Atomic and nuclear 173 146 | #> 2 68 147 | #> 3 Non-SI units 36 148 | #> 4 Electromagnetic 26 149 | #> 5 Physico-chemical 25 150 | #> 6 Universal 16 151 | #> 7 Adopted values 11 152 | #> 8 X-ray values 6 153 | ``` 154 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | template: 2 | bootstrap: 5 3 | -------------------------------------------------------------------------------- /constants.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Patch release (resubmission) 2 | 3 | Fixes compatibility with upcoming `units` 0.7-0. 4 | 5 | ## Test environments 6 | 7 | * Ubuntu, Windows, MacOS (on GHA), R oldrel, release, devel 8 | * win-builder, R devel 9 | 10 | ## R CMD check results 11 | 12 | There were no ERRORs, WARNINGs or NOTEs. 13 | 14 | ## Downstream dependencies 15 | 16 | There are no downstream dependencies for this package. 17 | -------------------------------------------------------------------------------- /data/codata.cor.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-quantities/constants/ed0e6f715f2d64414e203af98f66196f1cdcd800/data/codata.cor.rda -------------------------------------------------------------------------------- /data/codata.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-quantities/constants/ed0e6f715f2d64414e203af98f66196f1cdcd800/data/codata.rda -------------------------------------------------------------------------------- /man/codata.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/constants.R 3 | \docType{data} 4 | \name{codata} 5 | \alias{codata} 6 | \alias{codata.cor} 7 | \title{CODATA Recommended Values of the Fundamental Physical Constants: 2018} 8 | \format{ 9 | An object of class \code{data.frame} with the following information 10 | for each physical constant: 11 | ASCII \code{symbol}, \code{quantity} description, \code{type}, 12 | \code{value}, \code{uncertainty}, \code{unit}. 13 | 14 | A \code{matrix} of correlations between physical constants. 15 | } 16 | \source{ 17 | Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020). 18 | The 2018 CODATA Recommended Values of the Fundamental Physical Constants 19 | (Web Version 8.1). Database developed by J. Baker, M. Douma, and S. Kotochigova. 20 | Available at https://physics.nist.gov/cuu/Constants/, 21 | National Institute of Standards and Technology, Gaithersburg, MD 20899. 22 | } 23 | \usage{ 24 | codata 25 | 26 | codata.cor 27 | } 28 | \description{ 29 | The Committee on Data for Science and Technology (CODATA) is an interdisciplinary 30 | committee of the International Council for Science. The Task Group on Fundamental 31 | Constants periodically provides the internationally accepted set of values of 32 | the fundamental physical constants. This dataset contains the "2018 CODATA" 33 | version, published on May 2019. 34 | } 35 | \seealso{ 36 | \code{\link{syms}}, \code{\link{lookup}}. 37 | } 38 | \keyword{datasets} 39 | -------------------------------------------------------------------------------- /man/constants-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/constants.R 3 | \docType{package} 4 | \name{constants-package} 5 | \alias{constants-package} 6 | \title{\pkg{constants}: Reference on Constants, Units and Uncertainty} 7 | \description{ 8 | This package provides the 2018 version of the CODATA internationally recommended 9 | values of the fundamental physical constants for their use within the \R language. 10 | } 11 | \references{ 12 | Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2020). 13 | The 2018 CODATA Recommended Values of the Fundamental Physical Constants 14 | (Web Version 8.1). Database developed by J. Baker, M. Douma, and S. Kotochigova. 15 | Available at https://physics.nist.gov/cuu/Constants/, 16 | National Institute of Standards and Technology, Gaithersburg, MD 20899. 17 | } 18 | \seealso{ 19 | \code{\link{codata}}, \code{\link{syms}}, \code{\link{lookup}}. 20 | } 21 | \author{ 22 | Iñaki Ucar 23 | } 24 | -------------------------------------------------------------------------------- /man/lookup.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/constants.R 3 | \name{lookup} 4 | \alias{lookup} 5 | \title{Lookup for Fundamental Physical Constants} 6 | \usage{ 7 | lookup(pattern, cols = c("symbol", "quantity", "type"), ...) 8 | } 9 | \arguments{ 10 | \item{pattern}{character string containing a regular expression to be matched 11 | (see \code{\link{grep}}).} 12 | 13 | \item{cols}{columns to perform pattern matching (see \code{\link{codata}}).} 14 | 15 | \item{...}{additional arguments for \code{\link{grep}}.} 16 | } 17 | \description{ 18 | A simple wrapper around \code{\link{grep}} for exploring the CODATA dataset. 19 | } 20 | \examples{ 21 | lookup("planck", ignore.case=TRUE) 22 | 23 | } 24 | \seealso{ 25 | \code{\link{codata}}, \code{\link{syms}}. 26 | } 27 | -------------------------------------------------------------------------------- /man/syms.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/constants.R 3 | \docType{data} 4 | \name{syms} 5 | \alias{syms} 6 | \alias{syms_with_errors} 7 | \alias{syms_with_units} 8 | \alias{syms_with_quantities} 9 | \title{Lists of Constants} 10 | \format{ 11 | A \code{list}, where names correspond to symbols in \code{codata$symbol}. 12 | \itemize{ 13 | \item \code{syms} contains plain numeric values. 14 | \item \code{syms_with_errors} contains objects of type \code{errors}, which 15 | enables automatic uncertainty propagation. 16 | \item \code{syms_with_units} contains objects of type \code{units}, which 17 | enables automatic conversion, derivation and simplification. 18 | \item \code{syms_with_quantities} contains objects of type \code{quantities}, 19 | which combines \code{errors} and \code{units}. 20 | } 21 | The enriched versions of \code{syms} are available only if the corresponding 22 | optional packages, \pkg{errors}, \pkg{units} and/or \pkg{quantities} are 23 | installed. See the documentation of these packages for further information. 24 | } 25 | \usage{ 26 | syms 27 | 28 | syms_with_errors 29 | 30 | syms_with_units 31 | 32 | syms_with_quantities 33 | } 34 | \description{ 35 | These named lists contain ready-to-use values for all the fundamental 36 | physical constants. 37 | } 38 | \details{ 39 | Experimental support for correlations between constants is provided via the 40 | \pkg{errors} package, but it is disabled by default. To enable it, the 41 | following option must be set before loading the package: 42 | 43 | \code{options(constants.correlations=TRUE)} 44 | 45 | Alternatively, \code{constants:::set_correlations()} may be used 46 | interactively, but scripts should not rely on this non-exported function, 47 | as it may disappear in future versions. 48 | } 49 | \examples{ 50 | # the speed of light 51 | syms$c0 52 | # use the constants in a local environment 53 | with(syms, c0) 54 | 55 | # attach only Planck-related constants 56 | (lkp <- lookup("planck", ignore.case=TRUE)) 57 | idx <- as.integer(rownames(lkp)) 58 | attach(syms[idx]) 59 | h 60 | plkl 61 | 62 | # the same with uncertainty 63 | detach(syms[idx]) 64 | attach(syms_with_errors[idx]) 65 | h 66 | plkl 67 | 68 | # the same with units 69 | detach(syms_with_errors[idx]) 70 | attach(syms_with_units[idx]) 71 | h 72 | plkl 73 | 74 | # the same with everything 75 | detach(syms_with_units[idx]) 76 | attach(syms_with_quantities[idx]) 77 | h 78 | plkl 79 | 80 | } 81 | \seealso{ 82 | \code{\link{codata}}, \code{\link{lookup}}. 83 | } 84 | \keyword{datasets} 85 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(constants) 3 | test_check("constants") 4 | detach("package:constants", unload = TRUE) 5 | -------------------------------------------------------------------------------- /tests/testthat/test-constants.R: -------------------------------------------------------------------------------- 1 | context("constants") 2 | 3 | test_that("the dataset has the right number of columns", { 4 | expect_equal(names(codata), c("symbol", "quantity", "type", "value", "uncertainty", "unit")) 5 | }) 6 | 7 | test_that("the symbols are correctly set", { 8 | expect_true(all(do.call(c, lapply(syms, is.numeric)))) 9 | expect_equal(nrow(codata), length(syms)) 10 | expect_equal(as.vector(codata$symbol), names(syms)) 11 | 12 | if (requireNamespace("errors", quietly = TRUE)) { 13 | expect_true(all(do.call(c, lapply(syms_with_errors, inherits, "errors")))) 14 | expect_equal(nrow(codata), length(syms_with_errors)) 15 | expect_equal(as.vector(codata$symbol), names(syms_with_errors)) 16 | expect_equal(as.numeric(syms), as.numeric(syms_with_errors)) 17 | } else expect_null(syms_with_errors) 18 | 19 | if (requireNamespace("units", quietly = TRUE)) { 20 | expect_true(all(do.call(c, lapply(syms_with_units, inherits, "units")))) 21 | expect_equal(nrow(codata), length(syms_with_units)) 22 | expect_equal(as.vector(codata$symbol), names(syms_with_units)) 23 | expect_equal(as.numeric(syms), as.numeric(syms_with_units)) 24 | } else expect_null(syms_with_units) 25 | 26 | if (requireNamespace("quantities", quietly = TRUE)) { 27 | expect_true(all(do.call(c, lapply(syms_with_quantities, inherits, "quantities")))) 28 | expect_equal(nrow(codata), length(syms_with_quantities)) 29 | expect_equal(as.vector(codata$symbol), names(syms_with_quantities)) 30 | expect_equal(as.numeric(syms), as.numeric(syms_with_quantities)) 31 | } else expect_null(syms_with_quantities) 32 | }) 33 | -------------------------------------------------------------------------------- /wdir/download.R: -------------------------------------------------------------------------------- 1 | library(xml2) 2 | library(quantities) 3 | # some units depend on the speed of light 4 | try(units::remove_symbolic_unit("c"), silent=TRUE) 5 | install_conversion_constant("c", "m/s", 299792458) 6 | 7 | # URL parts #################################################################### 8 | 9 | base_url <- "https://physics.nist.gov/cgi-bin/cuu/" 10 | list_url <- "Category?view=html&%s" 11 | vals_url <- "Value?%s" 12 | corr_url <- "CCValue?%s|ShowSecond=Browse&First=%s" 13 | categories <- c( 14 | "Universal", "Electromagnetic", "Atomic+and+nuclear", "Physico-chemical", 15 | "Adopted+values", "Non-SI+units", "X-ray+values") 16 | 17 | # Helpers ###################################################################### 18 | 19 | read_html <- function(url, name, tmpdir="wdir/tmp") { 20 | dir.create(tmpdir, showWarnings=FALSE) 21 | dest <- file.path(tmpdir, name) 22 | if (!file.exists(dest)) 23 | download.file(url, dest) 24 | xml2::read_html(dest) 25 | } 26 | 27 | extract_symbols <- function(x) { 28 | index <- read_html(sprintf(paste0(base_url, list_url), x), x) 29 | nodes <- xml_find_all(index, "body/table/tr/td[2]//a") 30 | nodes <- nodes[-length(nodes)] # remove "new search" link 31 | 32 | # quantity in contents, symbol in href 33 | do.call(rbind, lapply(nodes, function(node) data.frame( 34 | quantity = trimws(xml_text(node)), 35 | symbol = sub(".*\\?(.*)\\|.*", "\\1", xml_attr(node, "href")) 36 | ))) 37 | } 38 | 39 | # Build codata table ########################################################### 40 | 41 | # * quantity, symbol, type ---- 42 | codata <- extract_symbols("All+values") 43 | types <- do.call(rbind, lapply(categories, function(x) data.frame( 44 | symbol = extract_symbols(x)$symbol, 45 | type = gsub("\\+", " ", x)) 46 | )) 47 | # some constants are listed in more than one category 48 | types <- aggregate(type ~ symbol, types, toString) 49 | codata <- merge(codata, types, all=TRUE, sort=FALSE) 50 | 51 | # * value, unit, uncertainty ---- 52 | values <- do.call(rbind, lapply(codata$symbol, function(symbol) { 53 | html <- read_html(sprintf(paste0(base_url, vals_url), symbol), symbol) 54 | if (!length(xml_children(html))) 55 | return(NULL) # broken link safe-guard 56 | 57 | # take "concise form" and clean it 58 | raw <- xml_text(xml_find_all(html, "body/table/tr/td[2]/table/tr/td[2]"))[4] 59 | raw <- gsub("\xc2\xa0", " ", raw) # convert nbsp 60 | raw <- gsub("(?<=\\d) +(?=\\d)", "", raw, perl=TRUE) # space between numbers 61 | raw <- sub(" \t ", " ", raw) # tab before unit 62 | raw <- sub("x 10", "e", raw) # use "e" notation 63 | raw <- sub("...", "", raw, fixed=TRUE) # remove dots 64 | raw <- gsub(")-", ")^-", raw, fixed=TRUE) # required by units 65 | 66 | # now we can parse it to capture everything reliably 67 | quantity <- parse_quantities(raw) 68 | data.frame( 69 | symbol = symbol, 70 | value = drop_quantities(quantity), 71 | uncertainty = errors(quantity), 72 | unit = as.character(units(quantity)) 73 | ) 74 | })) 75 | codata <- merge(codata, values, all=TRUE, sort=FALSE) 76 | codata <- subset(codata, !is.na(value)) # remove broken links 77 | rownames(codata) <- NULL 78 | 79 | # Build correlation matrix ##################################################### 80 | # WARNING: this takes a lot of time currently, because there many pairs 81 | # ~2/3 of these combinations have correlation 0 or 1, so this can be optimized 82 | 83 | pairs <- t(combn(codata$symbol, 2)) 84 | corr <- do.call(rbind, apply(pairs, 1, function(pair) { 85 | html <- read_html( 86 | sprintf(paste0(base_url, corr_url), pair[1], pair[2]), 87 | paste0(pair, collapse="-")) 88 | if (!length(xml_children(html))) 89 | return(NULL) # broken link safe-guard 90 | 91 | # take the "r = ..." line 92 | raw <- xml_text(xml_find_all(html, "body/table/tr/td[2]/table/tr/td[1]")) 93 | raw <- gsub("\xc2\xa0| ", "", raw) # remove spaces 94 | raw <- grep("r=", raw, value=TRUE) # take the desired field 95 | 96 | data.frame( 97 | symbol1 = pair[1], symbol2 = pair[2], 98 | r = as.numeric(strsplit(raw, "=")[[1]][2]) 99 | ) 100 | })) 101 | # add first and last 102 | fl <- lapply(codata$symbol[c(1, nrow(codata))], function(x) list(x, x, 1)) 103 | corr <- do.call(rbind, modifyList(fl, list(corr=corr))) 104 | # spread to matrix 105 | codata.cor <- tidyr::spread(corr, symbol2, r) 106 | rownames(codata.cor) <- codata.cor$symbol1 107 | codata.cor$symbol1 <- NULL 108 | codata.cor <- as.matrix(codata.cor) 109 | # reorder to match codata dataframe 110 | symbol_order <- rank(codata$symbol) 111 | codata.cor <- codata.cor[symbol_order, symbol_order] 112 | # fill in the gaps 113 | diag(codata.cor) <- 1 114 | codata.cor[lower.tri(codata.cor)] <- t(codata.cor)[lower.tri(codata.cor)] 115 | 116 | # Save data #################################################################### 117 | 118 | # save original symbols as an attribute 119 | attr(codata$symbol, "nist") <- codata$symbol 120 | # fix symbols that are functions in base R (by appending a zero) 121 | isfun <- sapply(codata$symbol, function(i) is.function(get0(i))) 122 | codata$symbol[isfun] <- paste0(codata$symbol[isfun], 0) 123 | colnames(codata.cor) <- codata$symbol 124 | rownames(codata.cor) <- codata$symbol 125 | 126 | #save(codata, file="data/codata.rda") 127 | #save(codata.cor, file="data/codata.cor.rda") 128 | #tools::resaveRdaFiles("data") 129 | --------------------------------------------------------------------------------