R语言读取/生成配置文件 (configr package)
2017-02-04 23:34
337 查看
R语言读取/生成配置文件 (configr package)
configr package
简介
‘configr’ 整合了 ‘JSON‘, ‘INI‘, ‘YAML‘, ‘TOML’ 解析器,可以方便的来读取和生成配置文件配置文件格式
JSON
{ "default":{ "debug":true }, "comments":{ "version":"0.0.4" } }
更多: json.org, JSON Example , JSON-wikipedia.
INI
[default] debug=TRUE [comments] version=0.0.4
更多: INI-wikipedia.
YAML
default: debug: true comments: version: 0.0.4
更多: yaml.org, YAML-wikipedia.
TOML
title = "TOML Example" [default] debug = true [comments] version = "0.0.4"
4000
更多: toml-lang/toml. TOML-wikipedia.
安装
CRAN
#You can install this package directly from CRAN by running (from within R): install.packages('configr')
Github
# Install the cutting edge development version from GitHub: # install.packages("devtools") devtools::install_github("Miachol/configr")
Zip/Tarball
Download the appropriate zip file or tar.gz file from GithubUnzip the file and change directories into the configr directory
Run
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.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")
相关文章推荐
- 读取和修改App.config配置文件
- 配置文件的读取(.config)
- All about JAVA 读取配置文件的方法 (这里是config.properties)
- C#指定config配置文件读取信息
- C# 读取保存App.config配置文件的完整源码参考
- 使用python的ConfigParser模块方便的读取配置文件
- winform程序读取和改写配置文件App.config元素的值
- C# 读取保存App.config配置文件的完整源码参考(转)
- (转)C++编写Config类读取配置文件
- C#读取app.config配置文件中的connectionString节点
- 如何配置及读取App.config文件
- 记录下vs2008 读取 App.config 配置文件
- Web.config文件中节点的配置数值读取的2种方法,哪种更快?
- java Config配置文件读取#应用反射、工场模式
- 对config文件中的配置进行新增修改删除读取操作
- 一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- Silverlight4-RIAServices开发记事1-自动生成的web.config配置文件
- C# 注册表 网络检测 读取配置文件 config
- 假设一个网站系统存在两个配置文件web.config和config.config,那么我如何读取config.config的配置节呢?System.Configuration名字空间下的接口好象只能读
- C# 读取保存App.config配置文件的完整源码参考