Bash+R: howto pass parameters from bash script to R
来源:互联网 发布:极客学院php就业班视频 编辑:程序博客网 时间:2024/05/16 17:46
From original post @ http://analyticsblog.mecglobal.it/analytics-tools/bashr/
In the world of data analysis, the term automation runs hand in hand with the term “scripting”. There’s not the best programming language, only the most suitable to perform the required function.
In our case, many data aggregation procedures are run from unix/linux servers, collecting API data in real time, so it becomes essential to make sure that data is formatted and correctly stored for the analysis/visualization needs.
In our case some automatic procedures run via cron at night, calling multiple R scripts with some parameters.
Our challenge was to ensure that R scripts could perform certain procedures or not, depending on the parameters passed via bash script. The question was: how to send parameters from bash script to R in real time?
The answer is very simple and two aspects needed to be considered: the bash script that invokes the R script passing the parameters, and the R script itself that must read the parameters.
In this example we will create a variable containing the date of yesterday (the variable “fileshort”) and we will pass this variable to R in order to save a file using the variable as filename.
Let’s start from the bash script:
#!/bin/bashdata=`date --date=-1day +%Y%m%d`fileshort=test_$data.csvRscript /home/file_repo/testfile.R $fileshort --save
As you can see a simple variable fileshort is created and then sent to R script. As for the syntax, to invoke R you can use either “Rscript” “R <“: the result will be identical.
Now it’s time to edir our R script. First we need tell our script to intercept the parameters/arguments passed by shell, checking them with the print method as you can see below:
args <- commandArgs()print(args)
on console R will print what follows:
[1] "/usr/lib/R/bin/exec/R" [2] "--slave"[3] "--no-restore" [4] "--file=/home/file_repo/testfile.R"[5] "--args" [6] "test_20150201.csv"[7] "--save"
In our case the required parameter is the filename, or “test_20150201.csv” which is the sixth element of the array [6].
At this point you just need to assign a variable with the element that interests us:
name <- args[6]
and use our variable as we prefer. In our example to write a file:
require(lubridate)write.table(db_final,paste0(name), append = FALSE, quote = FALSE, sep = ",", eol = "n", na = "NA", dec = ".", row.names = FALSE, col.names = FALSE, qmethod = c("escape", "double"), fileEncoding = "")
The generated file will have name “test_20150201.csv”
Enjoy!
- Bash+R: howto pass parameters from bash script to R
- Bash Script
- Bash Script: A way to wait_until
- bash script to change filename path
- How to answer 'yes' in bash Script
- Bash-Positional Parameters
- Bash Special Parameters
- read line from file with bash shell script
- shell script - BASH
- Shell Script(bash)简介
- Linux Bash Script
- Bash Self-Extracting Script
- linux bash script
- bash shell script
- Bash Shell Script Tutorials
- 学习心得之bash script
- create bash-shell-script
- Bash Script 格式化工具
- 线程中 this逃逸
- Spring WebSocket教程(一)
- Magento后台Grid表使用了Group By导致统计出错的解决办法
- Java与.NET DES加密解密互转[转载]
- Hbase入门之:原理、基本概念与架构
- Bash+R: howto pass parameters from bash script to R
- xxxHive-3-日常操作xxxHive总结
- Android App 性能优化(一)
- JS(四)DOM模型之属性样式
- java 设置代理网络
- hdoj 1021 Fibonacci Again
- 为什么一定要加final
- pat 1051
- 执行make menucofig发生错误“Unable to find the ncurses libraries or the required header files”的解决方法