R语言读入常见的配置文件(JSON, INI,YAML, TOML)


我们这里使用configr包

简介

‘configr’ 整合了 ‘JSON‘, ‘INI‘, ‘YAML‘, ‘TOML’ 解析器,可以方便的来读取和生成配置文件
配置文件格式
JSON


{   "default":{
        "debug":true
    },
    "comments":{
        "version":"0.0.4"
    }
}

INI


[default]
debug=TRUE
[comments]
version=0.0.4

YAML

default:
  debug: true
comments:
  version: 0.0.4

TOML

title = "TOML Example"

[default]
debug = true

[comments]
version = "0.0.4"

安装

CRAN

#You can install this package directly from CRAN by running (from within R):
install.packages('configr')

使用

#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.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")

原文链接

Miachol的CSDN博客: https://blog.csdn.net/u010808407/article/details/54868337


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM