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. JVM初探之类文件结构

    java 的 .class 文件字节码如图:Class 文件是一组以8字节为基础单位的二进制流,各个数据项目有着严格的结构.下面我们介绍下class文件的结构. 魔数 class文件的头4个字节是魔数 ...

  2. Timer,TimerTask通过程序计数器实现的定时任务

    1.程序计数器 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用可以看 做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型, 各种虚 ...

  3. ## Python中的Package和Jupyter中import包问题

    前言 关于python包的一些知识 Java中的package概念 我们知道在java中的import package概念, java中的包就是一个目录,里面包含着子目录,子目录套着子目录,当需要引入 ...

  4. Java中如何动态创建接口的实现

    有很多应用场景,用到了接口动态实现,下面举几个典型的应用: 1.mybatis / jpa 等orm框架,可以在接口上加注解进行开发,不需要编写实现类,运行时动态产生实现. 2.dubbo等分布式服务 ...

  5. iOS数据本地化

    本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...

  6. uc广告过滤你能更坑点不

    背景: 搞的手机站要上线,电脑测试木有问题,拿手机访问,有个页面始终不正常, 其他的 windows phone 的正常, ios 的也正常 就唯独 ,用的是安卓,uc的浏览器显示有问题 我勒个去,那 ...

  7. less的基本操作

    less的四大特性及示例 1.特性一(变量) less写法 @color:#ffffff: body{background-color:@color:} 生成的css body{background- ...

  8. 初识Kafka----------Centos上单机部署、服务启动、JAVA客户端调用

    作为Apach下一个优秀的开源消息队列框架,Kafka已经成为很多互联网厂商日志采集处理的第一选择.后面在实际应用场景中可能会应用到,因此就先了解了一下.经过两个晚上的努力,总算是能够基本使用. 操作 ...

  9. Java多线程的几个常用关键字

    一.同步(synchronized)和异步(asynchronized) 1.同步(synchronized)简单说可以理解为共享的意思,如果资源不是共享的,就没必要进行同步.设置共享资源为同步的话, ...

  10. 构建你人生里的第一个 Laravel 项目

    安装 Laravel 安装器 composer global require "laravel/installer" 创建项目 laravel new links 检查是否安装成功 ...