Re: [R] what is best for scripting?

From: paul sorenson <>
Date: Sat 03 Dec 2005 - 09:09:55 EST

As I get more familiar with R I tend to find the need for massaging data with other scripts decreasing.

I still use python as a front end to some R tasks and it is a great language to have in your personal arsenal (as is R).

The kind of decision making process for me goes something like:

        o If R can read the data directly then use R. I am pleasantly surprised at R in this regard.

        o If Python (or other scripting tool) already has bindings for dataset that you want, consider using Python. Eg I extract software development metrics from Perforce with python for plotting in R.

        o If the data set has a complex grammar, choose a tool with support for grammar compilers (I use and recommend pyparsing but there are dozens of choices). Actually I didn't check if there is a grammar compiler for R.   Someone mentioned BeautifulSoup not long ago for extracting stuff from broken HTML. I have used this also with some success for extracting deeply nested tables in poorly written HTML.

I usually dump data from Python to R in CSV format. I call R scripts from Python and about the only trick I use here is to read in an R script template and perform string variable expansion (interpolation in Perl) before sending it to an R process. See attached for example. For various reasons I have not used the R-Python bindings.


Molins, Jordi wrote:
> I am using R in Windows. I see that I will have to use batch processes with
> R. I will have to read and write text files, and run some R code; probably
> some external code too. I have never done scripting. Is there any document
> that explains simple steps with examples? I also have heard that Python is a
> good scripting language. Is it worth the effort? (I do not have too much
> free time, so if I could do without, much better ...).
> Has anybody strong opinions on that? Past experiences?
> Thank you!
> Jordi
> --------------------------------------------------------------------------------
> The information contained herein is confidential and is inte...{{dropped}}
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!

#!/usr/bin/env python
# Executes an R script
# $Revision: 1.1 $
# paul sorenson oct 2003

CMD = '''"/Program Files/R/rw2001/bin/rterm.exe" --vanilla --slave'''

import logging
log = logging.getLogger('TT')

def RExec(RFile, params = None):

    Execute an R script - optionally interpolating dict style params.

    Warning, R also accepts %0d style interpolation, if you want to pass     this to R then your original script must add an additional %.     Eg:
    png(filename="c:/sosman/testtrack/web/tt%%02d.png", width=640)     

    This will get passed to R as:
    png(filename="c:/sosman/testtrack/web/tt%02d.png", width=640)     if you pass in non null params to the function.     

    If params is not set then no interpolation will take place so raw     R scripts will pass through unchanged.     '''
    import popen2
    (r, w, e) = popen2.popen3(CMD)
    rsrc = % params
    err =
    print err

def usage():

    print 'usage: python --r-script=<R_script> [--params=<params>]'     sys.exit(2)     

if __name__ == "__main__":

    import sys, getopt

        opts, args = getopt.getopt(sys.argv[1:], "p:r:", ["params=", "r-script="])     except getopt.GetoptError:

    scriptname = None
    params = None
    for opt, arg in opts:

        if opt in ('-p', '--params'):
            params = eval(arg)
        elif opt in ('-r', '--r-script'):
            scriptname = arg
    if not scriptname:

    print '# script: "%s" params: "%s"' % (scriptname, params)     r = file(scriptname, 'r')
    RExec(r, params)
    r.close() mailing list PLEASE do read the posting guide! Received on Sat Dec 03 19:31:15 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:28 EST