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

10 comments:

  1. Hi. I am very keen to use the macro but I constantly get "No instance of R seems to be running".
    I have downloaded the macro from github and followed the instructions in the README.
    Starting 'R console' seems OK both from Menu or automatic when select "Send Source to Rgui". However, the command will not be sent to R console with output "No instance of R seems to be running".
    I saw similar issue in the github but wondering if this still happens in my environment.
    Thanks for your suggestions if there is a workaround.

    Windows 7 (64bit)
    R2.12.2 (I tried both i386 x64)

    ReplyDelete
  2. Hi again. This is self response on the issue below. I came to find that it works completely fine in the MDI window mode. I have been using SDI but will try to adjust to MDI. If it is easy one, fixing it would be highly appreciated in the future version. Thanks.
    By the way just a small question. Is there anyway we can assign a short cut key for "Send Source to R"?

    ReplyDelete
  3. Hi, I've just pushed a patch to github which should fix this issue (SDI).
    https://github.com/datadebrief/TheRMacro

    ReplyDelete
  4. Thank you so much for the prompt response. Now works perfect for SDI.
    For another question, I have assigned CTRL+DOWN to the R Macro so that I can send the command just one more type of DOWN.
    Thanks for the great tool!!

    ReplyDelete
  5. Hi, no problem!
    Did you still want to know how to assign a shortcut key?
    1. for All or just R configuration, select tools menu, properties for ... configuration.
    2. Select "keyboard" tab
    3. Select Category "My Macros", then choose Commands "thermacro.jsee"
    4. To assign hotkey, click in "Press new shortcut key" textbox and then press your key combination (for you ctrl+down).
    5. Click "Assign"
    6. Click ok

    You should now have it working for ctrl+down

    ReplyDelete
  6. oh thank you for the posts. I have a question on using emeditor as an R editor: I've imported the syntax file and configured the "external tool" as you posted before. everything works fine, except for plots. emeditor seems not be able to execute these commands and no plots are popped open. is it possible to work this out? thanks in advance.

    ReplyDelete
  7. Hi xxxEno, yes, i can confirm i can reproduce your issue, i'm not sure if it is an R issue (i thin it is) or EmEditor problem (used to work R < v13). I will look in to it (I use thermacro instead, which works, though there's a bug in that to i will fix this week)

    ReplyDelete
  8. Update, this seems to be expected behaviour of Rterm now as i've seen a few posts on it. This post on stackoverflow has some workarounds http://stackoverflow.com/questions/8167668/stopping-exit-after-rscript-run
    Basically, as a line before plot, call "windows()" and after the plot, call "locator()" and that will keep the plot device open until you click it. If you don't like this behaviour, I would encourage you to try the thermacro which does not have this problem.

    ReplyDelete
  9. oh great, it works ! thanks a lot ! i appreciate your help.

    ReplyDelete
  10. Quick update to say i have pushed a proper fix for reliably sending to R windows to the "thermacro" on github. Combined with a keyboard shortcut, I find the macro easier to use, you can even remove all of the menu items for the macro except "send to R" and it is an excellent replacement for the external program comand, as you can highlight text and send only that, amongst other things (such as displaying plots)

    ReplyDelete