How to avoid hardcoding in R? How to automatize R scripts?
2
1
Entering edit mode
4.5 years ago

Hello,

This is not directly related to any particular analysis. I have been doing my own analysis for rna seq data for the last one year, completely learning from here and there. Until now, I am confident and can do my own analysis. I always use Rstudio to write and run the scripts for my data analysis. But one thing I feel lacking is, not being able to make the r script automatized. Like, just to be able to run Rscript my_script.R in bash to execute and get the results. For every data sets, I have to manually change i.e. some variable names, or change some simple data table subsetting parameters or change the figure output names or change the working directory path etc. Etc. I am kind of frustrated and it drives the motivation down that everytime I have to make a copy of my old script, manually change stuffs inside, then run it. Is this always the case everywhere? I am sure it's not! The place where I come from, there's nobody to help me or teach me effective R programming to make it really automatized. I would really appreciate if you can give me some tips and tricks or some resources online to follow through to make Automatized scripts that will run seamlessly.

R r programming RNA-Seq ChIP-Seq • 2.5k views
ADD COMMENT
3
Entering edit mode

In addition to the answer by Yean, you might consider looking into the optparse and argparse packages. These make handling command line arguments easier. I have a template R-script that uses optparse here: rscript-template gist

ADD REPLY
2
Entering edit mode

You can also add docopt to that list: https://github.com/docopt/docopt.R

ADD REPLY
6
Entering edit mode
4.5 years ago
Yean ▴ 140

I don't know if my approach is the best one but I usually just use the argument in my script, and whenever I got new datasets in different path , new variables or whatever. I just simply change the path of file or variable via argument.

Something like

Rscript myscript.R arg1 arg2 arg3

Check this how to add argument into Rscript https://www.r-bloggers.com/passing-arguments-to-an-r-script-from-command-lines/

ADD COMMENT
4
Entering edit mode

You can also use configuration files. Segregate the input variables into their own file named something like project1.config.R then simply read if from your script with source("project 1.config.R"). You can also write config files in various formats e.g. JSON, XML... and read them with the appropriate parsers. The advantage of config files is that you can save them with the output so that you can remember how a particular output was generated and avoid questions like: did I use arg1=0.1 with arg2="B" or was it arg1=0.2 with arg2="A"?

ADD REPLY
0
Entering edit mode

Thank you very much! That's very helpful!

ADD REPLY
3
Entering edit mode
4.5 years ago

The first step could be to write functions. This will help to identify the steps that can easily be automated and find the variables that may be a bit trickier to standardize. It will also reduce the amount of code that you have to wade through every time you do need to change something as the functions can easily be kept in a separate .R file, which you then source() in your main document.

If you need a primer on functions in R, this seems like a useful starting point.

ADD COMMENT
0
Entering edit mode

Thank you! I will look into it more!

ADD REPLY

Login before adding your answer.

Traffic: 2500 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6