zoukankan      html  css  js  c++  java
  • 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:

    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:

    on console R will print what follows:

    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:

    The generated file will have name “test_20150201.csv”

    Enjoy!

    ---------------------------------------------------------------------------------- 数据和特征决定了效果上限,模型和算法决定了逼近这个上限的程度 ----------------------------------------------------------------------------------
  • 相关阅读:
    关于源码编译每次提示有错误 要make update-api
    如何在Android中添加系统服务
    git查看每个版本间的差异
    achartengine画出动态折线图
    java中基本数据类型和C语言中基本数据类型转换
    获取图片的真实宽高
    hdu-2066-一个人的旅行
    Linux内核模块编程与内核模块LICENSE -《具体解释(第3版)》预读
    Android平台Camera实时滤镜实现方法探讨(十一)--实时美颜滤镜
    ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方式
  • 原文地址:https://www.cnblogs.com/payton/p/4287548.html
Copyright © 2011-2022 走看看