How to get parameters from config file in R script

前端 未结 4 2026
一整个雨季
一整个雨季 2020-12-08 09:59

Is there a way to read parameters from a file in an R script?

I want to create a config file that has

db_host=xxxx
db_name=xxxx
db_user=xxxx
db_pass=         


        
4条回答
  •  广开言路
    2020-12-08 10:33

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

    Anyways, store all of your db parameters in a file named 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)
    

提交回复
热议问题