The R package ‘configr’ implements the ‘JSON’, ‘INI’, ‘YAML’, and ‘TOML’ parser for R setting and writing of configuration file.
The configuration file is necessary for many projects that will help us to manage and set project environment variables easily.
Configuration files, from INI/XML/JSON/YAML to TOML, readability and maneuverability have been improved too much in the past few years, and there are several parsers be created in R and other programming languages. That has made us becomes more efficient, but, we need to remember the different functions for different format configuration file that sometimes we only just want to read it and regardless of that format. So, using a single function to read or/and write most of the configuration file are a good way to reduce memory burden.
configr have done some work to relax us on configuration files that can be used to parse and generate JSON/INI/YAML/TOML format configuration file. The functionality of this package is similar to that of package ‘config’. Vignettes can be found in usage-of-configr and configuration-filetypes.
#You can install this package directly from CRAN by running (from within R):
install.packages('configr')
# Install the cutting edge development version from GitHub:
# install.packages("devtools")
::install_github("Miachol/configr") devtools
R CMD INSTALL pkg
library(configr)
# Get demo configuration files in configr
<- system.file('extdata', 'config.json', package='configr')
config.json <- system.file('extdata', 'config.ini', package='configr')
config.ini <- system.file('extdata', 'config.yaml', package='configr')
config.yaml <- system.file('extdata', 'config.toml', package='configr')
config.toml <- system.file('extdata', 'config.global.toml', package='configr')
config.glob
# Check specific configuration file type
<- is.json.file(file = config.json)
is.json <- is.ini.file(file = config.ini)
is.ini <- is.yaml.file(file = config.yaml)
is.yaml <- is.toml.file(file = config.toml)
is.toml
# Check specific configuration file types with debug information
<- is.json.file(file = config.yaml, json.file.debug = T)
is.json <- is.ini.file(file = config.json, ini.file.debug = T)
is.ini <- is.yaml.file(file = config.toml, yaml.file.debug = T)
is.yaml <- is.toml.file(file = config.yaml, toml.file.debug = T)
is.toml
# Get configuration file type
<- get.config.type(file = config.json)
json <- get.config.type(file = config.ini)
ini <- get.config.type(file = config.yaml)
yaml <- get.config.type(file = config.toml)
toml
# Read whole configuration file
<- read.config(file = config.json)
json.list <- read.config(file = config.ini)
ini.list <- read.config(file = config.yaml)
yaml.list <- read.config(file = config.toml)
toml.list
# Read specific section of configuration file (default is 'default' section)
# Configuration type, section, and path will be added to this object
<- eval.config(file = config.json)
config.json.obj <- eval.config(file = config.ini)
config.ini.obj <- eval.config(file = config.yaml)
config.yaml.obj <- eval.config(file = config.toml)
config.toml.obj
# Get all configuration section names
<- eval.config.sections(file = config.json)
json.sections <- eval.config.sections(file = config.ini)
ini.sections <- eval.config.sections(file = config.yaml)
yaml.sections <- eval.config.sections(file = config.toml)
toml.sections
# Merge multiple sections, default is all sections
# You can use this to reduce one layer in configuration file
<- eval.config.merge(file = config.json)
json.config.all <- eval.config.merge(file = config.ini)
ini.config.all <- eval.config.merge(file = config.yaml)
yaml.config.all <- eval.config.merge(file = config.toml)
toml.config.all
# Convert string to configuration list object
<- '{"city" : "Z\\u00FCrich"}\n'
json_string <- 'foo: 123\n'
yaml_string <- str2config(json_string)
json_config <- str2config(yaml_string)
yaml_config
# Read configuration files from remote sits
<- c('https://raw.githubusercontent.com/JhuangLab/BioInstaller/master/inst/extdata/config/db/db_annovar.toml',
links 'https://raw.githubusercontent.com/JhuangLab/BioInstaller/master/inst/extdata/config/db/db_main.toml')
fetch.config(links)
# Convert YAML format configuration file to JSON format
convert.config(file = config.yaml, out.file = tempfile(, fileext = ".json"),
convert.to = "JSON")
# Write JSON format configuration file
<- list(a=c(123,456))
list.test <- sprintf("%s/test.json", tempdir())
out.fn
write.config(config.dat = list.test, file.path = out.fn,
write.type = "json")
# Write JSON format configuration file with 2 indent
write.config(config.dat = list.test, file.path = out.fn,
write.type = "json", indent = 2)
# Write YAML format configuration file
<- sprintf("%s/test.yaml", tempdir())
out.fn write.config(config.dat = list.test, file.path = out.fn,
write.type = "yaml")
# Write YAML format configuration file with 4 indent
write.config(config.dat = list.test, file.path = out.fn,
write.type = "yaml", indent = 4)
# Write TOML format configuration file
# !! You need install python and toml package
# pip install toml
<- sprintf("%s/test.toml", tempdir())
out.fn write.config(config.dat = list.test, file.path = out.fn,
write.type = "toml")
<- sprintf("%s/test.ini", tempdir())
out.fn
# Write INI format configuration file
write.config(config.dat = list.test, file.path = out.fn,
write.type = "ini")
# Write INI format configuration file (only including pointed sections)
write.config(config.dat = list.test, file.path = out.fn, sections = "a",
write.type = "ini")
# Some of demo of extend parse
#
# Read raw configuration file that were parsed by jsonlite, yaml, INI and RcppTOML
.1 <- read.config(file = config.json)
config<- system.file('extdata', 'config.other.yaml', package='configr')
other.config
# Replace any items contained string "{{debug}}" to "self" and "debug2" to "self2"
.2 <- read.config(file = config.json,
configextra.list = list(debug = "self", debug2 = "self2"))
# Replace any items contained string "{{debug}}" to "self" and "debug2" to "self2"
# Then replace {{key:yes_flag}} to value in other.config key's yes_flag and no_flag
.3 <- read.config(file = config.json,
configextra.list = list(debug = "self", debug2 = "self2"),
other.config = other.config)
# Replace any items contained string "{{debug}}" to "self" and "debug2" to "self2"
# Then replace {{key:yes_flag}} to value in other.config key's yes_flag and no_flag
# Then replace @>@ Sys.Date() @<@ to R command result ...
.4 <- read.config(file = config.json,
configextra.list = list(debug = "self", debug2 = "self2"),
other.config = other.config, rcmd.parse = T)
.5 <- parse.extra(config.1,
configextra.list = list(debug = "self", debug2 = "self2"),
other.config = other.config, rcmd.parse = T)
# Replace any items contained string "{{debug}}" to "self" and "debug2" to "self2"
# Then replace {{key:yes_flag}} to value in other.config key's yes_flag and no_flag
# Then replace @>@ Sys.Date() @<@ to R command executed result ...
# Then replace #>#echo bash#<# to system() executed result
.6 <- parse.extra(config.1,
configextra.list = list(debug = "self", debug2 = "self2", yes = "1", no = "0"),
other.config = other.config, rcmd.parse = T, bash.parse = T)
# Use glue() to parse "{value or R CMD}"
# It will change the the length of a vector if returned value is a vector
<- c("a", "!!glue{1:5}", "c")
raw <- list(glue = raw, nochange = 1:10)
list.raw <- parse.extra(list.raw, glue.parse = TRUE, glue.flag = "!!glue")
parsed .1 <- c("a", "1", "2", "3", "4", "5", "c")
expect.parsed.2 <- list(glue = expect.parsed.1, nochange = 1:10)
expect.parsed
# Global vars parse (new feature in v0.3.4)
#
config_no_parsed_global <- read.config(config.glob, global.vars.field = NULL) config_parsed <- read.config(config.glob)
# Delete a section in a configuration object
config.partial <- config.sections.del(config.1, "default")
If you want to access external helps about configurations format or
other related information, You can use
configr::config.help()
do this. It will return a dataframe
contains all helps or open a browser, such as Chrome, to access the
corresponding URL.
# Show all help urls stored in configr
config.help()
# Open the url in browser
config.help('toml_stackoverflow_search')
# Or use the row number to access
config.help(23)