Bash+R: howto pass parameters from bash script to R(转)

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:

1 #!/bin/bash
2 data=`date --date=-1day +%Y%m%d`
3 fileshort=test_$data.csv
4
5 Rscript /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:

1 args &lt;- commandArgs()
2 print(args)

on console R will print what follows:

1 [1] "/usr/lib/R/bin/exec/R"               [2] "--slave"
2 [3] "--no-restore"                        [4] "--file=/home/file_repo/testfile.R"
3 [5] "--args"                              [6] "test_20150201.csv"
4 [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!

时间: 2024-07-28 14:04:08

Bash+R: howto pass parameters from bash script to R(转)的相关文章

doris: shell invoke .sql script for doris and passing values for parameters in sql script.

1. background in most cases, we want to execute sql script  in doris  routinely. using azkaban, to load data,etc.And we want to pass parameters to the sql script file. we can easily handle such situation in hive. 1.1 hive usage: using -hiveconf:  or

学习bash第二版 学习笔记 1 —— bash的简介、检测和安装

shell 区别于内核 是一款命令解析程序. Bourne Again shell (BASH), 创建用于GNU项目. GNU项目由Free Software Foundation(FSF)的Richard Stallman 始创,用于创建一个UNIX兼容的操作系统, 并使用该免费的发布版本替代所有的商业UNIX版本. ?? 你可能不能马上用bash.你的系统管理员可能将你的账号设置为使用系统上的 "标准" shell,你甚至不知道有没有其他shell可用. ?? 键入 echo $

F?l?e?x?4?+?s?p?r?i?n?g?+?h?i?b?e?r?n?a?t?e?+?B?l?a?z?e?D?S?整合案例

http://wenku.baidu.com/link?url=7v5xAyD2nvChQRT60QewpvAASFHMZNvD0OxX11OASYvae8jbVNsr5I000TwwYOlChzq05c50utxcLVb6mRL_r1pSFM1QZ2p9hJvRnpeT-T7 前提是系统安装好flex插件,Myeclipse,jdk,tomcat,并将flex插件集成到Myeclipse中 http://hi.baidu.com/xzwangyu/item/5bdc91c3c3439d3108

R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数

遇到了小bug: R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数 看到网上别人的做法,发现了用class(A)和class(B)之后才发现,是因为读入的时候数据的类型不对,A.B的类型并不是matrix,才导致了这个问题. 用as.matrix来变型一下,就OK了. R语言学习中的小bug:R中矩阵相乘错误于A %*% B: 需要数值/复数矩阵/矢量参数,布布扣,bubuko.com

你绝对想不到R文件找不到(cannot resolve symbol R)的原因

你绝对想不到R文件找不到(cannot resolve symbol R)的原因 最近在项目开发中 Android Studio 的 R 文件突然找不到了.IDE 中出现了以下提示 cannot resolve symbol R Clean And Rebuild 一般来说看到这个错误随便 Google baidu 回来的答案无非就是 clean -> rebuild 工程 而我的本能反应也是如此然而一点用都没有! 资源id冲突 虽然整个工程涉及到 R 文件都无法找到,但是跑起来却是正常,完全没

R?e?q?u?e?s?t?、?R?e?q?u?e?s?t?.?F?o?r?m?和?R?e?q?u?e?s?t?.?Q?u?e?r?y?S?t?r?i?n?g?的?区?别

Request . Request.Form 和 Request.QueryString 的区别 request 本身是一个系统的静态对象,本身也可以作为数组调用,比如 request("abc") 就是获取 request 数组中键值 "abc" 对应的值,而这个数组又不是 固定的,而是多个数组的集合,其中包含了 (QueryString ,Form ,Cookies ,ClientCertificate,ServerVariables ) 这几个服务器 变量数组

m?y?e?c?l?i?p?s?e? ?环?境?下?整?合?开?发? ?s?t?r?u?t?s?2?+?s?p?r?i?n?g?+?h?i?b?e?r?n?a?t?e? ?常?见?问?题?及?解?答

myeclipse 环境下整合开发 struts2+spring+hibernate 常见问题及解答 1. org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): 异常原因: <id> 元素配置不正确, <id> 元素缺少其子元素 <generator></generator> 的配

AndroidStudio中R文件引用失败(Cannot resolve symbol &#39;R&#39;)的解决方法

常见的两种情况参见:http://xichen.pub/2018/04/08/2018-04-08-Android-TimeCat-解决cant-resolve-symbol-R/ 我的情况是:AndroidManifest.xml文件中的package="com.example.activitytest_5"语句出错.如图: 我的项目的实际包名为com.example.activitytest_6,而这里标记为了com.example.activitytest_5,所以导致AS找不到

[HOWTO] Install Sphinx for A Script Pro

Hi, Here's a small howto on installing Sphinx Search (http://sphinxsearch.com/) and configuring it to work with Adult Script Pro. By using sphinxyou will be able to allow users to search for video on your adult tube site by title, description and tag