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/bash
data=`date --date=-1day +%Y%m%d`
fileshort=test_$data.csv 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:

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

on console R will print what follows:

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

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(转)的更多相关文章

  1. [Bash] Understand and Use Functions in Bash

    n this lesson, we'll go over how bash functions work. Bash functions work like mini bash scripts--yo ...

  2. 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 l ...

  3. How-to: Do Statistical Analysis with Impala and R

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  4. -bash: /etc/init.d/nginx: /bin/bash^M: bad interpreter: No such file or directory

    -bash: /etc/init.d/nginx: /bin/bash^M:bad interpreter: No such file or directory 这个使为了弄nginx自启的,然后在官 ...

  5. R用户的福音︱TensorFlow:TensorFlow的R接口

    ------------------------------------------------------------ Matt︱R语言调用深度学习架构系列引文 R语言︱H2o深度学习的一些R语言实 ...

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

    你绝对想不到R文件找不到(cannot resolve symbol R)的原因 最近在项目开发中 Android Studio 的 R 文件突然找不到了.IDE 中出现了以下提示 cannot re ...

  7. 《R语言入门与实践》第一章:R基础

    前言 本章介绍了 R 语言的基础知识 界面: 使用命令 “ R “进行命令行的实时编译 对象 定义: 用于储存数据的,设定一个名称 格式: a <- 1:6 命名规则: 规则1:不能以数字开头规 ...

  8. 概率图模型 基于R语言 这本书中的第一个R语言程序

    概率图模型 基于R语言 这本书中的第一个R语言程序 prior <- c(working =0.99,broken =0.01) likelihood <- rbind(working = ...

  9. 《R语言实战》读书笔记--第一章 R语言介绍

    1.典型的数据分析过程可以总结为一下图形: 注意,在模型建立和验证的过程中,可能需要重新进行数据清理和模型建立. 2.R语言一般用 <- 作为赋值运算符,一般不用 = ,原因待考证.用-> ...

随机推荐

  1. __init__.py

    python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的 ...

  2. 程序设计 之 C#实现《拼图游戏》

    功能描述: 1.用户自定义上传图片 2.游戏难度选择:简单(3*3).一般(5*5).困难(9*9)三个级别 3.纪录完成步数 模块: 1.拼图类 2.配置类 3.游戏菜单窗口 4.游戏运行窗口 -- ...

  3. 微信小程序入门学习

    前(che)言(dan): 近几天,微信小程序的内测引起了众多开发人员的热议,很多人都认为这将会成为一大热门,那么好吧,虽然我是一个小白,但这是个新玩意,花点时间稍稍钻研一下也是无妨的,谁让我没有女朋 ...

  4. 使用 Http 的 Get 方式读取网络数据

    作为移动平台的应用,一定避免不了与网络交换数据,不论是读取网页数据,还是调用API接口,都必须掌握Http通信技术 代码如下: package zw1; import java.io.Buffered ...

  5. STM32F0的flash读写

    flash大小64k Rom+8k Ram的大小,stm32f051有64k Rom,总的分为 64页,一页1024byte ,在flash的Rom里面写数据掉电保存,相当于W25q80 uint32 ...

  6. python自动化开发-[第一天]-基础数据类型与编码

    1.Python与其他语言对比 - C语言的解释方式  代码-->机器码-->计算机 - python,java,php等高级语言的解释方式  代码-->字节码-->机器码-- ...

  7. Arcgis Engine OMD

    在UML 图中有三种类型的类:抽象类(abstract class).组件类(COclass)与普通类(instantiable class). 抽象类:不能创建或实例化,从来没有一个抽象类的实例用于 ...

  8. iOS APP开发设置启动图片 Launch Image

    一.添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或点下面的+号创建Launch Image: 这里首先说明一下尺寸: ...

  9. Jfinal数据库操作语句中占位符的使用

    占位符的优点: 1.增加SQL代码可读性 2.占位符可以预先编译,提高执行效率 3.防止SQL注入 4.用占位符的目的是绑定变量,这样可以减少数据SQL的硬解析,所以执行效率会提高不少 假设要将id从 ...

  10. Ehcache 的简单实用 及配置

    Ehcache 与 spring 整合后的用法,下面是一个Ehcache.xml配置文件: 通用的缓存策略 可以用一个 cache: <?xml version="1.0" ...