envvar
Make working with environment variables easier and more consistent
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (17.7%) to scientific vocabulary
Keywords
configuration
environment-variables
r
Last synced: 6 months ago
·
JSON representation
Repository
Make working with environment variables easier and more consistent
Basic Info
- Host: GitHub
- Owner: briandconnelly
- License: other
- Language: R
- Default Branch: main
- Homepage: https://briandconnelly.github.io/envvar/
- Size: 4.6 MB
Statistics
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 3
- Releases: 3
Topics
configuration
environment-variables
r
Created over 2 years ago
· Last pushed over 1 year ago
Metadata Files
Readme
Changelog
License
README.Rmd
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# envvar
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=envvar)
[](https://github.com/briandconnelly/envvar/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/briandconnelly/envvar?branch=main)
Environment variables are a powerful tool that enable your code to react to its environment.
However, two common design choices are a frequent source of friction.
First, unlike most other "getter"-type functions, those functions that retrieve values from environment variable typically fail silently.
Second, while programmers often use environment variables to store a wide variety of data types from numbers to timestamps to URLs, values are almost always returned as strings.
These choices necessitate additional code that checks whether an environment variable was actually set and to coerce its value into the intended format.
For frequent users of environment variables, writing all this extra code is unpleasant and time consuming.
## Failing Loudly
envvar takes a slightly opinionated perspective to make working with environment variables easier and more consistent.
Unless a default value is explicitly given, `envvar_get()` raises an error if an environment variable is not defined.
For example, let's say our code depends on an environment variable called `NUM_CPUS`.
In base R, we have to first get the value using `Sys.getenv()` and then see whether the result is the empty string (not `NA` like you might expect):
```{r base notset,error=TRUE}
num_cpus <- Sys.getenv("NUM_CPUS")
if (identical(num_cpus, "")) {
stop("I need `NUM_CPUS` to be set!")
}
```
envvar's `envvar_get()` will just fail if `NUM_CPUS` isn't set:
```{r envvar notset,error=TRUE}
library(envvar)
envvar_get("NUM_CPUS")
```
If a reasonable default is known, it can be supplied via the `default` argument.
envvar prints a message, though, so you know that it's using a default rather than a value specified in the environment.
```{r envvar notset default}
envvar_get("NUM_CPUS", default = 12)
```
Warnings can be disabled with the `warn_default` argument.
## Speaking Native Types
```{r include=FALSE}
Sys.setenv("NUM_CPUS" = 8)
```
Let's say our `NUM_CPUS` environment variable is set to 8.
Because `Sys.getenv()` returns strings, we can't immediately treat it like the integer that it is.
```{r base nocoerce,error=TRUE}
Sys.getenv("NUM_CPUS") / 2
```
envvar includes several helper functions that return commonly-used data types as their proper type.
Here, we'll use `envvar_get_integer()` to get `NUM_CPUS` and return it as an integer.
```{r envvar nocoerce,error=TRUE}
envvar_get_integer("NUM_CPUS") / 2
```
Returning to the theme of failing loudly, envvar's type-specific functions will also fail if a value cannot be coerced to the expected type.
For example, using `Sys.getenv()` and `as.integer` to load what _should be_ an integer value might not produce what you'd expect.
```{r base int conversion}
Sys.setenv("NUM_CPUS" = 12.345)
num_cpus <- as.integer(Sys.getenv("NUM_CPUS"))
num_cpus
```
Using `envvar_get_integer()`:
```{r envvar int conversion, error=TRUE}
envvar_get_integer("NUM_CPUS")
```
This extends to default values:
```{r envvar int conversion default, error=TRUE}
envvar_get_integer("SOME_UNSET_INTEGER", default = 12.345)
```
envvar can handle numbers, logical values, version numbers, URLs, timestamps, UUIDs, IP addresses, and more.
We'll work with dates in the next example.
## Validation
Sometimes being the right type isn't enough.
envvar's `envvar_get` functions can also apply validation logic.
For this example, let's set an environment variable called `LAUNCH_DATE` that stores a date that absolutely, positively must be in the future.
Let's first set it to a date in the past.
```{r set launch date past}
envvar_set("LAUNCH_DATE" = "1969-07-16")
```
To read `LAUNCH_DATE` and ensure that it is in the future, we can supply a `validate` function to `envvar_get_date()` that checks the value.
If this function returns `FALSE`, an error is raised.
```{r get launch date,error=TRUE}
envvar_get_date("LAUNCH_DATE", validate = \(x) x > Sys.Date())
```
Let's try that again:
```{r get good launch date,error=TRUE}
envvar_set("LAUNCH_DATE" = "2028-08-28")
envvar_get_date("LAUNCH_DATE", validate = \(x) x > Sys.Date())
```
Note that the `validate` argument supports one function.
If you're in need of complex validation, just use a function that encapsulates all of that fanciness.
## Installation
You can install the latest released version of envvar by running:
``` r
install.packages("envvar")
```
If you’d like to try out the development version, you can install directly from GitHub:
``` r
# install.packages("remotes")
remotes::install_github("briandconnelly/envvar")
```
## Related Packages
- [dotenv](https://github.com/gaborcsardi/dotenv) package for loading environment variables from `.env` files
- [config](https://rstudio.github.io/config/) package for defining and using multiple environments
- [options](https://dgkf.github.io/options/) package for defining and using R package options, another way of adding flexibility to your code
Owner
- Name: Brian Connelly
- Login: briandconnelly
- Kind: user
- Location: Seattle
- Company: Ookla
- Website: https://fosstodon.org/@briandconnelly
- Repositories: 17
- Profile: https://github.com/briandconnelly
GitHub Events
Total
- Issues event: 1
Last Year
- Issues event: 1
Committers
Last synced: about 2 years ago
Top Committers
| Name | Commits | |
|---|---|---|
| Brian Connelly | b****y@o****m | 61 |
Committer Domains (Top 20 + Academic)
ookla.com: 1
Issues and Pull Requests
Last synced: 7 months ago
All Time
- Total issues: 8
- Total pull requests: 6
- Average time to close issues: 3 months
- Average time to close pull requests: 17 days
- Total issue authors: 1
- Total pull request authors: 1
- Average comments per issue: 0.0
- Average comments per pull request: 0.0
- Merged pull requests: 5
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 3
- Pull requests: 1
- Average time to close issues: 11 days
- Average time to close pull requests: 11 days
- Issue authors: 1
- Pull request authors: 1
- Average comments per issue: 0.0
- Average comments per pull request: 0.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- briandconnelly (9)
Pull Request Authors
- briandconnelly (11)
Top Labels
Issue Labels
enhancement (1)
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- cran 215 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 3
- Total maintainers: 1
cran.r-project.org: envvar
Make Working with Environment Variables Easier and More Consistent
- Homepage: https://github.com/briandconnelly/envvar
- Documentation: http://cran.r-project.org/web/packages/envvar/envvar.pdf
- License: MIT + file LICENSE
-
Latest release: 0.1.2
published over 1 year ago
Rankings
Dependent packages count: 29.0%
Dependent repos count: 37.1%
Average: 51.0%
Downloads: 86.8%
Maintainers (1)
Last synced:
6 months ago
Dependencies
.github/workflows/R-CMD-check.yaml
actions
- actions/checkout v3 composite
- r-lib/actions/check-r-package v2 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml
actions
- JamesIves/github-pages-deploy-action v4.4.1 composite
- actions/checkout v3 composite
- r-lib/actions/setup-pandoc v2 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml
actions
- actions/checkout v3 composite
- actions/upload-artifact v3 composite
- r-lib/actions/setup-r v2 composite
- r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION
cran
- cli * imports
- fs * imports
- lubridate * imports
- rlang * imports
- httr2 * suggests
- ipaddress * suggests
- spelling * suggests
- testthat >= 3.0.0 suggests
- uuid * suggests
- withr * suggests