[R] calling an editor on MAC and Linux

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jun 14 11:16:16 CEST 2002

On Fri, 14 Jun 2002, Ott Toomet wrote:

> On Thu, 13 Jun 2002, Niels Waller wrote:
>   |I have written an R function that writes output to an external file using
>   |the sink function.  I assume that `sink' is not operating system dependent.
>   |At the end of the function I have additional code that opens the newly
>   |created file using notepad on a Windows system.  Obviously, this part of the
>   |code will not work on a Mac or Linux box.  Since I do not have a machine
>   |with Linux or the Mac OS available I have no idea what to call the default
>   |editors.  Could somebody provide a suggestion?  Thanks in advance.
>   |
>   |
>   |   win3 <- function(...) {
>   |        system(...)
>   |    }
>   |
>   | notepad.file <- paste("notepad", outFile, sep = " ")
>   | win3(notepad.file)

R has an edit command that you could use!

edit(file=outFile) is simple and system-independent.  'Nuff said?

> If there is something like default editor, it is "vi" on UNIX (but there may
> be 10-20 different editors with a full linux installation).  It is a general
> tradition to let the user to define his own favourite using EDITOR
> environment variable.  So, to be polite, you should check the presence of
> the variable first, and thereafter run either vi or whatever was defined as
> This can be done pretty easily as (ignoring paste):
> system("${EDITOR:-vi} outFile")
> Unfortunately, this is not the whole story.  The previous example works with
> bash shell (which is default command interpreter on Linux).  With tcsh,
> another quite popular shell, I am afraid you need a different approach (this

What's wrong with

  editor <- Sys.getenv("EDITOR")
  if(editor == "") editor <-  "vi"

?  That is a lot cheaper and avoids any shell issues by not using a shell.

> again is controlled with environment variable SHELL).  In addition, many
> editors in Unix are terminal-based (like vi) i.e., they do not open a new
> window.  This may cause problems when R is runned from a GUI or from Emacs
> (Emacs cannot interpret vi diplay-control strings correctly).  If runned as
> usual process on a terimal, it is just fine.
> Perhaps it is the best to try the example above, but warn the user and
> suggest to customise the editor command.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list