[ACCEPTED]-How to get parameters from config file in R script-unix

Accepted answer
Score: 44

I'd go for YAML. Designed for human read-writability 11 unlike XML. R package "yaml" exists 10 on CRAN, I'm sure perl and java packages 9 exist too.

http://ftp.heanet.ie/mirrors/cran.r-project.org/web/packages/yaml/index.html

You can't get more cross-platform 8 than this:

http://yaml.org/

at least until I write a YAML 7 FORTRAN package...

[edit]

Example. Suppose 6 you have config.yml:

db:
 host : foo.example.com
 name : Foo Base
 user : user453
 pass : zoom

Then yaml.load_file("config.yml") returns:

$db
$db$pass
[1] "zoom"

$db$user
[1] "user453"

$db$name
[1] "Foo Base"

$db$host
[1] "foo.example.com"

So 5 you do:

library(yaml)
config = yaml.load_file("config.yml")
dbConnect(PgSQL(), host=config$db$host, dbname=config$db$name, user=config$db$user, password=config$db$pass)

Add as many sections and parameters 4 as you need. Sweeeeyit.

The yaml.load_file 3 returns your configuration as an R list, and 2 you can access named elements of lists using 1 $-notation.

Score: 12

You can source() an R script in at the top of your 9 main script that reads in your config file 8 parameters. Depending on who you are sharing 7 your scripts with, there may be issues with 6 database security and having the login information 5 in an unencrypted format. There is a recent 4 question here on SO about that, I'll see 3 if I can find it in a minute.

Anyways, store 2 all of your db parameters in a file named 1 config.R and then on your main script, run:

source("config.R") #Will create four objects named "db_host, db_name, db_user, db_pass"

dbConnect(PgSQL(), host=db_host, dbname=db_name, user=db_user, password=db_pass)
Score: 3

The built-in R function read.table handles 6 this INI format well if you suggest that 5 it split columns using the equals sign.

key.val<-read.table(filename, sep="=", col.names=c("key","value"), as.is=c(1,2))

If 4 you want a more traditional INI behavior, so 3 that you can use multiple config files, try 2 assigning the key-value pairs to an R environment. For 1 example:

read.config<-function(filename) {
  conf.vars<-new.env()
  for (f in filename) {
    if (file.exists(f)) {
      header<-1
      key.val<-read.table(f, sep="=", col.names=c("key","value"), skip=header,
          as.is=c(1,2))
      for (kidx in seq(length(key.val$key))) {
        assign(key.val[["key"]][kidx], key.val[["value"]][kidx], envir=conf.vars)
      }
    }
  }
  return(conf.vars)
}

get.config<-function(name) {
  kv.env=read.config(c("project.cfg","project_local.cfg"))
  return(kv.env[[name]])
}
Score: 1

What you describe here is a desire for common configuration 12 across systems, platforms, languages, ...

That 11 is a big topic and much ink has been spilled 10 on this. Some people see XML as the answer, other 9 prefer simpler related formats like JSON. You 8 could also try Apache-style config files 7 as most languages have libraries for it (but 6 R may be an exception).

I happen to like 5 Google ProtocolBuffers which are fast, efficient, cross-platform, multi-language, forward-compatible 4 ... but have the one downside of not being 3 ascii files (though you can read ascii files 2 first and then create proto files). For 1 R, there is the RProtoBuf package.

Score: 0

If you use an INI configuration file, you 1 can use the ini package. For example:

library(ini)
config <- read.ini('/config/database.ini')

https://cran.r-project.org/web/packages/ini/ini.pdf

More Related questions