Showing posts with label EmEditor. Show all posts
Showing posts with label EmEditor. Show all posts

Monday, January 10, 2011

EmEditor R code macro - Almost interactive R development for Emeditor

Get the new macro now hosted on github


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.


As a follow up to the earlier post regarding Emeditor Professional as an editor for R development, I have updated the script based on user feedback. The script now sends selected text or the whole (if no text selected) file to a running RGui Console if set to, otherwise as per previous iteration, will send it to rterm.

So, there are now 2 ways to run the macro, based on setting the value for a variable called "
useSource" - the default is "true", which means any selected text (or file) will be sent to an already running instance of RGui; If useSource is set to "false", the macro will operate the "old" way, by invoking rterm each time.

This has a number of benefits over invoking rterm each time:
  • Keeps the same workspace loaded - No need to keep repeating tasks, such as loading libraries, performing lengthy calculations etc, if you only changed some other part of the code;
  • Plots now display;
  • You can see what has executed.
The macro relies on the R command "source", as sending text via sendkeys is unreliable. By default, the macro has echo=TRUE,keep.source=TRUE set. For further information on the source command, in R, run "help(source)".

Note
that when useSource=true, all R output is only shown in R, only errors with emeditor/macro will show in the Emeditor output window.

More features planned for the future:

  • Auto launch RGui if useSource=true but no rgui running;
  • Be closer in features to the vim r plugin; and
  • Move the macro code to github
  • Maybe create an actual emeditor plugin and run R in a hidden shell for more integration.
Get the new macro now hosted on github
As always, any issues please leave a comment, or log an issue

Tuesday, September 21, 2010

Auto-complete for R in EmEditor

This will work for any language you have a syntax file for, but I'll use R as the example.

1. Open an R file, and on the plugins menu, click "Word Complete"
2. Type a couple of letters e.grn and then press ctrl+space (default word suggest keyboard combination) and hopefully an auto-suggest popup menu will appear.


3. If nothing seems to happen, be sure you have configured R with the R syntax file (see previous post), otherwise you may need to do some further configuration. 
3.1 Go to Tools > Plugins > Customize Plugins > Word Complete (be sure it has a check) > R and double check the various settings, also verify the keyboard short cut assigned under the keyboard tab.




This post was inspired by Tal Galili's discussion of wordpress themes, and Yihui Xie's Notepad++ autocomplete efforts.

Sunday, September 12, 2010

EmEditor Professional as an R script editor

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).