MaraudingLily
MaraudingLily

Reputation: 25

Setting options in R by reading a config file - evaluating options() dynamically

I am trying to set a few options (globally) before running an R script that calls a bunch of functions in custom packages. These options are to be read from a text file (config) that looks like

param1=value1

I read in this file like so

configs_df<-read.csv("configfile", sep="=", strip.white=TRUE, header=FALSE, comment.char="#", stringsAsFactor=FALSE, blank.lines.skip=TRUE)

I've attempted using a combination of eval and sprintf, but to no avail

options("eval(configs_df$V1[1])"="eval(configs_df$V2[1])")
do.call(options, list(configs_df$V1[1], configs_df$V2[1]))
params <- c(configs_df$V1[1], configs_df$V2[1])
exp <- "options(%s=%s)"
toeval <- splat(sprintf)(c(exp, params))
eval(toeval)

I would really appreciate a few pointers.

Upvotes: 1

Views: 160

Answers (1)

r2evans
r2evans

Reputation: 160607

After you read in your data, try:

options(as.list(setNames(configs_df$V2, nm=configs_df$V1)))

Upvotes: 1

Related Questions