EmEditor Professional as an R script editor

September 11, 2010
By

(This article was first published on DataDebrief, and kindly contributed to R-bloggers)

R is not supported "out of the box" by EmEditor, so here's a few tips I've found for using it as a great editor for R.
  • Code Syntax highlighting
  • Executing R scripts and capturing output
  • Use ctags symbols to navigate files.
  • If you create packages for CRAN or like neat code, you might like to use a macro to tidy it up. 

    Getting the R syntax file
    This is on the EmEditor website, in the Library > Syntax Files section. Thank you to whomever created and uploaded it. While you are in the properties, it's also a good opportunity to change tabs to spaces.

    Creating an "External Tool" to run R scripts
    The following settings (see also screenshot) in the create/properties screen for external tools should get things running:
    Command: C:\Program Files\R\R-2.11.0\bin\Rterm.exe
    Arguments: --vanilla --quiet --slave --file="$(Path)"
    Initial Directory: $(Dir)
    Icon Path: C:\Program Files\R\R-2.11.0\bin\R.exe
    Save file: Checked
    Use Output bar: Checked
    Close one exit: Checked
    Input: None
    Output: Create new document
    Standard Error: Create new document

    Using a macro to run R scripts
    Edit 18th Jan 2011: The below text refers to the old version of the macro and is no longer relevant, a new post will  describe the new macro, and it is also documented on the github site. Get the new macro now hosted on github

    Edit 10th Jan 2011 - This macro has been updated and there is now a specific post regarding it.
    See emeditor-r-code-macro-almost.html

    This offers more flexibility than the external tool choice, particularly if you want to run only a portion of code (more than passing an "expression"), or if the file has not been saved yet. The Rrun.jsee macro is one I constructed to allow passing a saved file, unsaved file or snippet of code to rterm - feel free to use it and improve it.

    Generating and using ctags symbols for R
    The simplest way of using ctags for an unsupported language is to use the regular expression support, and include it in a file .ctags in your home directory (c:\users\xxxx\ for vista). This will then automatically get picked up by ctags and you won't need to do any further config regarding it in EmEditor (or other editor for that matter). It's also O/S transportable.
    My .ctags has the following, you may want to modify it to suit your particular needs though (you probably don't want the apply or plot, or even variable).
    Note: ctags does not seem to support multiline, so if you have a function assigned on a different line (like when it is tidied by R (see below)) it won't find it.

    --langdef=R
    --langmap=R:.r
    --regex-R=/([[:alnum:]\._]+)([[:space:]]*)<-([[:space:][:cntrl:]]*function)/\1/f,function/
    --regex-R=/([[:alnum:]]+)[[:space:]]*<-/\1/v,variable/
    --regex-R=/[^[:alnum:]]plot[[:space:]]*\(([[:alnum:],=" ]+)\)/\1/p,plot/
    --regex-R=/([[:alpha:]]apply[[:space:]]*\([[:alnum:],=" ]+\))/\1/a,apply/i



    To double check the file is ok, you can run "C:\Program Files\EmEditor\PlugIns\ctags.exe" --list-kinds  it will show R listed. When you update the symbols in EmEditor, you should see something similar to this:



    Tidying up R code with a macro in EmEditor
    Based on the "Writing R Extensions" manual, I created an EmEditor macro (jsee) to do basic R code tidying. It is based off the run R macro, so you can tidy sections of code as well as the whole file. It should be easily modified to run through cscript/wscript rather than EmEditor, just beware some objects are EmEditor only.
    Note: This method will remove any comments in the code, it also seems to force new lines for function assigns, eg, turns this:

    shorten.to.string <- function(line, token)

    to

    shorten.to.string <-
    function (line, token) 



    I'm not sure if that breaks suggested style rules or not, is a pain for picking up with ctags though (more a ctags failing).

    To leave a comment for the author, please follow the link and comment on his blog: DataDebrief.

    R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.