‘configr’ 整合了 ‘JSON‘, ‘INI‘, ‘YAML‘, ‘TOML’ 解析器,可以方便的来读取和生成配置文件
更多: json.org, JSON Example , JSON-wikipedia.
更多: INI-wikipedia.
更多: yaml.org, YAML-wikipedia.
更多: toml-lang/toml. TOML-wikipedia.
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.type
get.config.type 可以得到配置文件的类型,返回json/ini/yaml/toml 或者 FALSE (其他类型或非配置文件)
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.config
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")