'configr' 整合了 'JSON', 'INI', 'YAML', 'TOML' 解析器,能夠方便的來讀取和生成配置文件html
{ "default":{ "debug":true }, "comments":{ "version":"0.0.4" } }
更多: json.org, JSON Example , JSON-wikipedia.git
[default] debug=TRUE [comments] version=0.0.4
更多: INI-wikipedia.github
default: debug: true comments: version: 0.0.4
更多: yaml.org, YAML-wikipedia.json
title = "TOML Example" [default] debug = true [comments] version = "0.0.4"
更多: toml-lang/toml. TOML-wikipedia.app
#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") devtools::install_github("Miachol/configr")
R CMD INSTALL pkg
#Get filepath of example configuration files config.json <- system.file('extdata', 'config.json', package='configr') config.ini <- system.file('extdata', 'config.ini', package='configr') config.yaml <- system.file('extdata', 'config.yaml', package='configr') config.toml <- system.file('extdata', 'config.toml', package='configr')
is.json.file/is.ini.file/is.yaml.file/is.toml.file函數
is.json.file/is.ini.file/is.yaml.file/is.toml.file 可以測試某個文件是否爲標準的某種配置文件格式(目前支持 JSON/INI/YAML/TOML),返回TRUE or FALSE測試
#Test file type of config file (No warning message) is.json <- is.json.file(file = config.json) is.ini <- is.ini.file(file = config.ini) is.yaml <- is.yaml.file(file = config.yaml) is.toml <- is.toml.file(file = config.toml) #Test file type of config file (Debug, print warning message) is.json <- is.json.file(file = config.yaml, json.file.debug = T) is.ini <- is.ini.file(file = config.json, ini.file.debug = T) is.yaml <- is.yaml.file(file = config.toml, yaml.file.debug = T) is.toml <- is.toml.file(file = config.yaml, toml.file.debug = T)
get.config.typeui
get.config.type 能夠獲得配置文件的類型,返回json/ini/yaml/toml 或者 FALSE (其餘類型或非配置文件)this
json <- get.config.type(file = config.json) ini <- get.config.type(file = config.ini) yaml <- get.config.type(file = config.yaml) toml <- get.config.type(file = config.toml)
read.configdebug
read.config 能夠讀取配置文件全部部分並做爲一個list對象
#Read in R as a list (JSON/INI/YAML/TOML be suported) #fromJSON/read.ini/readLines/yaml.load parameters can be automatch by parameter name (encoding .etc.) json.list <- read.config(file = config.json) ini.list <- read.config(file = config.ini) yaml.list <- read.config(file = config.yaml) toml.list <- read.config(file = config.toml)
eval.config
eval.config 解析函數,獲得一個list 而且包含 file path, config group, filetype屬性。
#Get the same obj with config package, only get the 'default or R_CONFIG_ACTIVE config sets' in config.cfg or R_CONFIGFILE_ACTIVE config.json.obj <- eval.config(file = config.json) config.ini.obj <- eval.config(file = config.ini) config.yaml.obj <- eval.config(file = config.yaml) config.toml.obj <- eval.config(file = config.toml)
eval.config.groups
eval.config.groups 能夠獲得配置文件第一層的名字.
json.groups <- eval.config.groups(file = config.json) ini.groups <- eval.config.groups(file = config.ini) yaml.groups <- eval.config.groups(file = config.yaml) toml.groups <- eval.config.groups(file = config.toml)
eval.config.merge
eval.config.merge 能夠讀取配置文件中指定的部分
json.config.all <- eval.config.merge(file = config.json) ini.config.all <- eval.config.merge(file = config.ini) yaml.config.all <- eval.config.merge(file = config.yaml) toml.config.all <- eval.config.merge(file = config.toml) json.config.all <- eval.config.merge(file = config.json, groups = c("comments"))
write.config
write.config 目前在R中可使用list對象生成JSON/INI/YAML格式的配置文件
list.test <- list(a=c(123,456)) out.fn <- sprintf("%s/test.json", tempdir()) write.config(config.dat = list.test, file.path = out.fn, write.type = "json") write.config(config.dat = list.test, file.path = out.fn, write.type = "json", indent = 2) out.fn <- sprintf("%s/test.yaml", tempdir()) write.config(config.dat = list.test, file.path = out.fn, write.type = "yaml") write.config(config.dat = list.test, file.path = out.fn, write.type = "yaml", indent = 4) out.fn <- sprintf("%s/test.ini", tempdir()) write.config(config.dat = list.test, file.path = out.fn, write.type = "ini")