├── _pkgdown.yml ├── LICENSE ├── data ├── codata.rda └── codata.cor.rda ├── tests ├── testthat.R └── testthat │ └── test-constants.R ├── NAMESPACE ├── .github └── workflows │ ├── build.yml │ └── pkgdown.yml ├── .Rbuildignore ├── cran-comments.md ├── constants.Rproj ├── .gitignore ├── man ├── lookup.Rd ├── constants-package.Rd ├── codata.Rd └── syms.Rd ├── DESCRIPTION ├── NEWS.md ├── README.Rmd ├── wdir └── download.R ├── README.md └── R └── constants.R /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | template: 2 | bootstrap: 5 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2017-2020 2 | COPYRIGHT HOLDER: Iñaki Ucar 3 | -------------------------------------------------------------------------------- /data/codata.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-quantities/constants/HEAD/data/codata.rda -------------------------------------------------------------------------------- /data/codata.cor.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/r-quantities/constants/HEAD/data/codata.cor.rda -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(constants) 3 | test_check("constants") 4 | detach("package:constants", unload = TRUE) 5 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | ^CRAN-SUBMISSION$ 20 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## New release 2 | 3 | Major update to 2022 values. New versioning scheme to follow values' year. 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 | No issues found. 17 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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/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} 6 | \alias{constants-package} 7 | \title{\pkg{constants}: Reference on Constants, Units and Uncertainty} 8 | \description{ 9 | This package provides the 2022 version of the CODATA internationally recommended 10 | values of the fundamental physical constants for their use within the \R language. 11 | } 12 | \references{ 13 | Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2024). 14 | The 2022 CODATA Recommended Values of the Fundamental Physical Constants 15 | (Web Version 9.0). Database developed by J. Baker, M. Douma, and S. Kotochigova. 16 | Available at https://physics.nist.gov/cuu/Constants/, 17 | National Institute of Standards and Technology, Gaithersburg, MD 20899. 18 | } 19 | \seealso{ 20 | \code{\link{codata}}, \code{\link{syms}}, \code{\link{lookup}}. 21 | } 22 | \author{ 23 | Iñaki Ucar 24 | } 25 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: constants 2 | Type: Package 3 | Title: Reference on Constants, Units and Uncertainty 4 | Version: 2022.0 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 "2022 CODATA" 16 | version, published on May 2024: 17 | Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2024) 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.3.3 28 | -------------------------------------------------------------------------------- /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: 2022} 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 (2024). 18 | The 2022 CODATA Recommended Values of the Fundamental Physical Constants 19 | (Web Version 9.0). 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 "2022 CODATA" 33 | version, published on May 2024. 34 | } 35 | \seealso{ 36 | \code{\link{syms}}, \code{\link{lookup}}. 37 | } 38 | \keyword{datasets} 39 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # constants 2022.0 2 | 3 | Update to version 9.0, the 2022 CODATA recommended values. 4 | Package versioning has been aligned with the CODATA versioning scheme. 5 | 6 | # constants 1.0.1 7 | 8 | - Fix compatibility with `units` 0.7-0. 9 | 10 | # constants 1.0.0 11 | 12 | Update to version 8.1, the 2018 CODATA recommended values (#7 addressing #6). 13 | This version contains some breaking changes that are necessary to streamline 14 | future updates and provide a stable symbol table: 15 | 16 | - The `codata` table includes the absolute uncertainty instead of the relative 17 | one. Thus, the `rel_uncertainty` column has been dropped in favour of the new 18 | `uncertainty`. Also, columns have been slightly reordered. 19 | - Symbol names for constants have changed. The old ones were hand-crafted and 20 | thus unmanageable. This release adopts the ASCII symbols defined by NIST in 21 | their webpage, except for those that collide with some base R function. In 22 | particular, there are two cases: `c`, the speed of light, has been renamed as 23 | `c0`; `sigma`, the Stefan-Boltzmann constant, has been renamed as `sigma0`. 24 | - Constant types, or categories, (column `codata$type`) adopts the names defined 25 | by NIST in the webpage too. Some constants belong to more than one category 26 | (separated by comma); some others belong to no category (missing type). 27 | 28 | There are some new features too: 29 | 30 | - In addition to the `codata` data frame, this release includes `codata.cor`, a 31 | correlation matrix for all the constants. 32 | - In addition to `syms_with_errors` and `syms_with_units`, there is a new list 33 | of symbols called `syms_with_quantities` (available if the optional 34 | `quantities` package is installed), which provides constant values with 35 | uncertainty **and** units. 36 | - Experimental support for correlated values in `syms_with_errors` and 37 | `syms_with_quantities` is provided (disabled by default; see details in 38 | `help(syms)` for activation instructions). 39 | 40 | # constants 0.0.2 41 | 42 | - Use `units::as_units()` instead of the deprecated `units::parse_unit()` (#1). 43 | - Install the speed of light as a unit (#2). 44 | - Unitless constants now show a `1` instead of `unitless` as unit (#3). 45 | - Unit `Ω` has been replaced by `ohm` (#4). 46 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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://app.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 "2022 CODATA" version, published on May 2024. 25 | 26 | > Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2024). The 2022 CODATA Recommended Values of the Fundamental Physical Constants (Web Version 9.0). 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 | codata |> 85 | tidyr::separate_rows(type, sep=", ") |> 86 | dplyr::count(type, sort=TRUE) 87 | ``` 88 | -------------------------------------------------------------------------------- /wdir/download.R: -------------------------------------------------------------------------------- 1 | library(xml2) 2 | library(quantities) 3 | # some units depend on the speed of light 4 | remove_unit("c") 5 | install_unit("c", "299792458 m/s") 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 = drop_units(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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Reference on Constants, Units and Uncertainty 5 | 6 | 7 | 8 | [![Build 9 | Status](https://github.com/r-quantities/constants/workflows/build/badge.svg)](https://github.com/r-quantities/constants/actions) 10 | [![Coverage 11 | Status](https://codecov.io/gh/r-quantities/constants/branch/master/graph/badge.svg)](https://app.codecov.io/gh/r-quantities/constants) 12 | [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/constants)](https://cran.r-project.org/package=constants) 13 | [![Downloads](https://cranlogs.r-pkg.org/badges/constants)](https://cran.r-project.org/package=constants) 14 | 15 | 16 | The **constants** package provides the CODATA internationally 17 | recommended values of the fundamental physical constants, provided as 18 | symbols for direct use within the R language. Optionally, the values 19 | with uncertainties and/or units are also provided if the ‘errors’, 20 | ‘units’ and/or ‘quantities’ packages are installed. The Committee on 21 | Data for Science and Technology (CODATA) is an interdisciplinary 22 | committee of the International Council for Science which periodically 23 | provides the internationally accepted set of values of the fundamental 24 | physical constants. This package contains the “2022 CODATA” version, 25 | published on May 2024. 26 | 27 | > Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor 28 | > (2024). The 2022 CODATA Recommended Values of the Fundamental Physical 29 | > Constants (Web Version 9.0). Database developed by J. Baker, M. Douma, 30 | > and S. Kotochigova. Available at 31 | > , National Institute of 32 | > Standards and Technology, Gaithersburg, MD 20899. 33 | 34 | ## Installation 35 | 36 | Install the release version from CRAN: 37 | 38 | ``` r 39 | install.packages("constants") 40 | ``` 41 | 42 | The installation from GitHub requires the 43 | [remotes](https://cran.r-project.org/package=remotes) package. 44 | 45 | ``` r 46 | # install.packages("remotes") 47 | remotes::install_github("r-quantities/constants") 48 | ``` 49 | 50 | ## Example 51 | 52 | ``` r 53 | library(constants) 54 | 55 | # the speed of light 56 | syms$c0 57 | #> [1] 299792458 58 | # use the constants in a local environment 59 | with(syms, c0) 60 | #> [1] 299792458 61 | 62 | # explore which constants are available 63 | (lkp <- lookup("planck", ignore.case=TRUE)) 64 | #> symbol quantity 65 | #> 137 nah molar Planck constant 66 | #> 198 h Planck constant 67 | #> 199 hev Planck constant in eV/Hz 68 | #> 200 plkl Planck length 69 | #> 201 plkm Planck mass 70 | #> 202 plkmc2gev Planck mass energy equivalent in GeV 71 | #> 203 plktmp Planck temperature 72 | #> 204 plkt Planck time 73 | #> 230 hbar reduced Planck constant 74 | #> 231 hbarev reduced Planck constant in eV s 75 | #> 232 hbcmevf reduced Planck constant times c in MeV fm 76 | #> type value uncertainty unit 77 | #> 137 Physico-chemical 3.990313e-10 0.0e+00 J/Hz/mol 78 | #> 198 Universal, Adopted values 6.626070e-34 0.0e+00 J/Hz 79 | #> 199 Universal 4.135668e-15 0.0e+00 eV/Hz 80 | #> 200 Universal 1.616255e-35 1.8e-40 m 81 | #> 201 Universal 2.176434e-08 2.4e-13 kg 82 | #> 202 Universal 1.220890e+19 1.4e+14 GeV 83 | #> 203 Universal 1.416784e+32 1.6e+27 K 84 | #> 204 Universal 5.391247e-44 6.0e-49 s 85 | #> 230 Universal, Adopted values 1.054572e-34 0.0e+00 J*s 86 | #> 231 Universal 6.582120e-16 0.0e+00 eV*s 87 | #> 232 Universal, Non-SI units 1.973270e+02 0.0e+00 fm*MeV 88 | idx <- as.integer(rownames(lkp)) 89 | 90 | # attach the symbols to the search path and use them explicitly 91 | attach(syms[idx]) 92 | h 93 | #> [1] 6.62607e-34 94 | plkl 95 | #> [1] 1.616255e-35 96 | 97 | # the same with uncertainty 98 | detach(syms[idx]) 99 | attach(syms_with_errors[idx]) 100 | h 101 | #> 6.62607(0)e-34 102 | plkl 103 | #> 1.61626(2)e-35 104 | 105 | # the same with units 106 | detach(syms_with_errors[idx]) 107 | attach(syms_with_units[idx]) 108 | h 109 | #> 6.62607e-34 [J/Hz] 110 | plkl 111 | #> 1.616255e-35 [m] 112 | 113 | # the same with everything 114 | detach(syms_with_units[idx]) 115 | attach(syms_with_quantities[idx]) 116 | h 117 | #> 6.62607(0)e-34 [J/Hz] 118 | plkl 119 | #> 1.61626(2)e-35 [m] 120 | 121 | # the whole dataset 122 | head(codata) 123 | #> symbol quantity type 124 | #> 1 mal alpha particle mass Atomic and nuclear 125 | #> 2 malc2 alpha particle mass energy equivalent Atomic and nuclear 126 | #> 3 malc2mev alpha particle mass energy equivalent in MeV Atomic and nuclear 127 | #> 4 malu alpha particle mass in u Atomic and nuclear 128 | #> 5 mmal alpha particle molar mass Atomic and nuclear 129 | #> 6 malsme alpha particle-electron mass ratio Atomic and nuclear 130 | #> value uncertainty unit 131 | #> 1 6.644657e-27 2.0e-36 kg 132 | #> 2 5.971920e-10 1.8e-19 J 133 | #> 3 3.727379e+03 1.1e-06 MeV 134 | #> 4 4.001506e+00 6.3e-11 u 135 | #> 5 4.001506e-03 1.2e-12 kg/mol 136 | #> 6 7.294300e+03 2.4e-07 1 137 | 138 | # number of constants per type 139 | codata |> 140 | tidyr::separate_rows(type, sep=", ") |> 141 | dplyr::count(type, sort=TRUE) 142 | #> # A tibble: 8 × 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 | -------------------------------------------------------------------------------- /R/constants.R: -------------------------------------------------------------------------------- 1 | #' \pkg{constants}: Reference on Constants, Units and Uncertainty 2 | #' 3 | #' This package provides the 2022 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 (2024). 10 | #' The 2022 CODATA Recommended Values of the Fundamental Physical Constants 11 | #' (Web Version 9.0). 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 | #' @name constants-package 18 | "_PACKAGE" 19 | 20 | #' CODATA Recommended Values of the Fundamental Physical Constants: 2022 21 | #' 22 | #' The Committee on Data for Science and Technology (CODATA) is an interdisciplinary 23 | #' committee of the International Council for Science. The Task Group on Fundamental 24 | #' Constants periodically provides the internationally accepted set of values of 25 | #' the fundamental physical constants. This dataset contains the "2022 CODATA" 26 | #' version, published on May 2024. 27 | #' 28 | #' @format An object of class \code{data.frame} with the following information 29 | #' for each physical constant: 30 | #' ASCII \code{symbol}, \code{quantity} description, \code{type}, 31 | #' \code{value}, \code{uncertainty}, \code{unit}. 32 | #' 33 | #' @source 34 | #' Eite Tiesinga, Peter J. Mohr, David B. Newell, and Barry N. Taylor (2024). 35 | #' The 2022 CODATA Recommended Values of the Fundamental Physical Constants 36 | #' (Web Version 9.0). Database developed by J. Baker, M. Douma, and S. Kotochigova. 37 | #' Available at https://physics.nist.gov/cuu/Constants/, 38 | #' National Institute of Standards and Technology, Gaithersburg, MD 20899. 39 | #' 40 | #' @seealso \code{\link{syms}}, \code{\link{lookup}}. 41 | "codata" 42 | 43 | #' @name codata 44 | #' @format A \code{matrix} of correlations between physical constants. 45 | "codata.cor" 46 | 47 | #' Lists of Constants 48 | #' 49 | #' These named lists contain ready-to-use values for all the fundamental 50 | #' physical constants. 51 | #' 52 | #' Experimental support for correlations between constants is provided via the 53 | #' \pkg{errors} package, but it is disabled by default. To enable it, the 54 | #' following option must be set before loading the package: 55 | #' 56 | #' \code{options(constants.correlations=TRUE)} 57 | #' 58 | #' Alternatively, \code{constants:::set_correlations()} may be used 59 | #' interactively, but scripts should not rely on this non-exported function, 60 | #' as it may disappear in future versions. 61 | #' 62 | #' @format 63 | #' A \code{list}, where names correspond to symbols in \code{codata$symbol}. 64 | #' \itemize{ 65 | #' \item \code{syms} contains plain numeric values. 66 | #' \item \code{syms_with_errors} contains objects of type \code{errors}, which 67 | #' enables automatic uncertainty propagation. 68 | #' \item \code{syms_with_units} contains objects of type \code{units}, which 69 | #' enables automatic conversion, derivation and simplification. 70 | #' \item \code{syms_with_quantities} contains objects of type \code{quantities}, 71 | #' which combines \code{errors} and \code{units}. 72 | #' } 73 | #' The enriched versions of \code{syms} are available only if the corresponding 74 | #' optional packages, \pkg{errors}, \pkg{units} and/or \pkg{quantities} are 75 | #' installed. See the documentation of these packages for further information. 76 | #' 77 | #' @seealso \code{\link{codata}}, \code{\link{lookup}}. 78 | #' 79 | #' @examples 80 | #' # the speed of light 81 | #' syms$c0 82 | #' # use the constants in a local environment 83 | #' with(syms, c0) 84 | #' 85 | #' # attach only Planck-related constants 86 | #' (lkp <- lookup("planck", ignore.case=TRUE)) 87 | #' idx <- as.integer(rownames(lkp)) 88 | #' attach(syms[idx]) 89 | #' h 90 | #' plkl 91 | #' 92 | #' # the same with uncertainty 93 | #' detach(syms[idx]) 94 | #' attach(syms_with_errors[idx]) 95 | #' h 96 | #' plkl 97 | #' 98 | #' # the same with units 99 | #' detach(syms_with_errors[idx]) 100 | #' attach(syms_with_units[idx]) 101 | #' h 102 | #' plkl 103 | #' 104 | #' # the same with everything 105 | #' detach(syms_with_units[idx]) 106 | #' attach(syms_with_quantities[idx]) 107 | #' h 108 | #' plkl 109 | #' 110 | #' @export 111 | syms <- NULL 112 | 113 | #' @name syms 114 | #' @format NULL 115 | #' @export 116 | syms_with_errors <- NULL 117 | 118 | #' @name syms 119 | #' @format NULL 120 | #' @export 121 | syms_with_units <- NULL 122 | 123 | #' @name syms 124 | #' @format NULL 125 | #' @export 126 | syms_with_quantities <- NULL 127 | 128 | set_correlations <- function() { 129 | stopifnot(requireNamespace("errors", quietly = TRUE)) 130 | 131 | n <- length(syms_with_errors) 132 | e.diag <- diag(constants::codata$uncertainty) 133 | codata.cov <- e.diag %*% constants::codata.cor %*% e.diag 134 | for (i in seq_len(n-1)) for (j in (i+1):n) { 135 | ijcov <- codata.cov[i, j] 136 | if (!ijcov) next 137 | errors::covar(syms_with_errors[[i]], syms_with_errors[[j]]) <- ijcov 138 | } 139 | } 140 | 141 | .onLoad <- function(libname, pkgname) { 142 | syms <<- as.list(constants::codata$value) 143 | names(syms) <<- as.vector(constants::codata$symbol) 144 | 145 | if (requireNamespace("errors", quietly = TRUE)) { 146 | syms_with_errors <<- Map( 147 | errors::set_errors, syms, constants::codata$uncertainty) 148 | 149 | if (getOption("constants.correlations", FALSE)) 150 | set_correlations() 151 | } 152 | 153 | if (requireNamespace("units", quietly = TRUE)) { 154 | # define the speed of light 155 | if (utils::packageVersion("units") < "0.7-0") { 156 | try(getExportedValue("units", "remove_symbolic_unit")("c"), silent=TRUE) 157 | getExportedValue("units", "install_conversion_constant")("c", "m/s", syms$c0) 158 | } else { 159 | getExportedValue("units", "remove_unit")("c") 160 | getExportedValue("units", "install_unit")("c", paste(syms$c0, "m/s")) 161 | } 162 | 163 | syms_with_units <<- Map( 164 | units::set_units, syms, constants::codata$unit, mode="standard") 165 | } 166 | 167 | if (requireNamespace("quantities", quietly = TRUE)) { 168 | syms_with_quantities <<- Map( 169 | units::set_units, syms_with_errors, constants::codata$unit, mode="standard") 170 | } 171 | } 172 | 173 | .onAttach <- function(libname, pkgname) { 174 | if (!requireNamespace("errors", quietly = TRUE)) 175 | packageStartupMessage(paste( 176 | "Package 'errors' not found.", 177 | "Constants with uncertainty ('syms_with_errors') not available.")) 178 | if (!requireNamespace("units", quietly = TRUE)) 179 | packageStartupMessage(paste( 180 | "Package 'units' not found.", 181 | "Constants with units ('syms_with_units') not available.")) 182 | if (!requireNamespace("quantities", quietly = TRUE)) 183 | packageStartupMessage(paste( 184 | "Package 'quantities' not found.", 185 | "Constants with uncertainty+units ('syms_with_quantities') not available.")) 186 | } 187 | 188 | #' Lookup for Fundamental Physical Constants 189 | #' 190 | #' A simple wrapper around \code{\link{grep}} for exploring the CODATA dataset. 191 | #' 192 | #' @param pattern character string containing a regular expression to be matched 193 | #' (see \code{\link{grep}}). 194 | #' @param cols columns to perform pattern matching (see \code{\link{codata}}). 195 | #' @param ... additional arguments for \code{\link{grep}}. 196 | #' 197 | #' @seealso \code{\link{codata}}, \code{\link{syms}}. 198 | #' 199 | #' @examples 200 | #' lookup("planck", ignore.case=TRUE) 201 | #' 202 | #' @export 203 | lookup <- function(pattern, cols=c("symbol", "quantity", "type"), ...) { 204 | cols <- match.arg(cols, several.ok = TRUE) 205 | ind <- do.call(c, lapply( 206 | cols, function(col) grep(pattern, constants::codata[[col]], ...))) 207 | constants::codata[sort(unique(ind)),] 208 | } 209 | --------------------------------------------------------------------------------