Using Geany for programming in R

I like Geany as a no-nonsense Integrated Development Environment (IDE). It is fast, elegant, intuitive, and lets you get your programming job done. (I certainly find it superior to the more popular Gedit.) You can also use it to program in R, and this page will show off some tips for doing that.

Execute commands in an R session

To send R commands from the editor to the integrated Virtual Terminal Emulator (VTE), you need to download Geany >= 0.19. Then you can set send_selection_unsafe=true in geany.conf and assign “Send selection to terminal” to a ctrl+r or ctrl+enter keybinding (or similar) via Edit > Prefs > Keybindings. As long as your Geany installation has support for the embedded VTE (and to my knowledge it is currently NOT supported on Windows), you’re good to go. Start R in the terminal, write some R code in Geany and send the line or selection to the terminal by using the assigned keybinding.

Be careful, though. The hidden option send_selection_unsafe is called that way and is disabled by default for good reason. If set to true, it does not strip trailing newline characters and even add one if not already present. When no R session is running, you are prone to send stupid commands (e.g. rm -rf your_preferred_folder) for execution to the console. Again, be careful with that.

Improving the R parser in Geany

The R lexer should consider the "." (dot) as part of an object name (as it does for the "_" underscore). By default it doesn’t. To change that, you can proceed as follows (assuming Linux, but it should be very similar on other platforms).

Copy /usr/share/geany/filetypes.r to ~/.config/geany/filedefs/. Then in ~/.config/geany/filedefs/filetypes.r uncomment the wordchars element and add a "." (dot) in that list. Your config file should thus contain the following line:
wordchars=_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

Now when you double-click or ctrl+left/right on an object name (say, make.names() or my_uber.cool_fun()), Geany should select or skip the entire word.

UPDATE: You need Geany >=1.23 for this to work. Otherwise, check the Comments section.

Combining Geany with RStudio

As good as an IDE as Geany is, it is not so well suited for working with R. It comes with no integrated graphics device, help system, or object browser. So Geany is a good choice if you’re OK with working in a bare bones terminal. In this sense RStudio is superior for actually performing statistical analyses in R, as it has many R-specific functionalities that make analyzing data that much easier. Yet again, RStudio is not a full-fledged IDE and lacks certain functionality for heavy code uplifting.

So one thing that you can do is to use both IDEs at the same time on a given file. My workflow is as follows:
- I open a_given_file.R in both Geany and RStudio and regularly save any modifications
- When I analyze data in RStudio, save a_given_file.R and switch to Geany, you will usually be greeted with a “The file ‘untitled.R’ on the disk is more recent than the current buffer. Do you want to reload it?” pop-up dialogue. Say Reload.
- When I do some heavy code uplifting in Geany, save a_given_file.R and switch to RStudio, you will usually automatically get the latest version of the file reloaded from disk.

Be careful: this approach works if you get used to it and try to avoid careless errors that could result in data loss. Otherwise, always save and close the file before switching to the other IDE.

Feedback

If readers have other useful tips on using Geany for R programming, please leave your thoughts in the Comments section.

About these ads

2 responses to “Using Geany for programming in R

  • Peter Wilson

    Thanks for a very helpful and timely post. I recently had to rebuild my main PC due to hardware failure and have had a great deal of trouble getting a stable version of Linux working on the new hardware combination (I found that Linux Mint 15 worked best). One of the things that has inexplicably broken is RStudio – a vital tool to my daily workflow! While I wait for the Rstudio developers to reply to my support request, I was wondering about using Geany to edit/run R-scripts (Geany is one of my favourite tools). Thanks to your post I can now keep the wheels turning!

    One thing cropped up in my experimenting with Geany as an R-script IDE. I’m using version 1.22 of Geany from the Ubuntu 13.04/Mint 15 repositories and the patched version of filetypes.r didn’t work as you suggested. According to a post on the Geany Sourceforge pages (sourceforge.net/p/geany/bugs/772) a fix was only recently put into the latest builds of Geany that would make your method work. To solve the issue for version 1.22 and earlier I found I needed to edit the file /usr/share/geany/filetypes.common. The line begining whitespace_chars= contains a string of characters that are regarded as whitespace and these chars over-ride any changes made for filetype-specific formatting. I removed “.” and also “$” and “@” so that editing scripts with references to the components of R lists and S3 and S4 objects will work too. This edit needs to be done with admin rights (eg in Debian-based Linux distros: sudo geany /usr/share/geany/filetypes.common).

    Thanks again for an inspiring post.

    Peter Wilson

    • landroni

      “a fix was only recently put into the latest builds of Geany that would make your method work.”

      Indeed that would be possible. I tested my fix with Geany 1.23 and to my surprise it worked, so I didn’t bother to go into more details. I use an up-to-date Geany from the Ubuntu PPA ( https://launchpad.net/~geany-dev/+archive/ppa ), but I don’t know if that works with Linux Mint.

      ” This edit needs to be done with admin rights (eg in Debian-based Linux distros: sudo geany /usr/share/geany/filetypes.common).”

      Actually I would suggest to go a different way. The proper way of doing such changes is to copy /usr/share/geany/filetypes.common to ~/.config/geany/filedefs/, and edit the file there. I think it should automagically work so (without admin rights) by going to Tools > Config files > filetypes.common, perform the modifications and save.

      I’m glad this post helped.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: