Edit a text file using an external editor. Possibly wait for the end of the program and care about creating the file (from a template) if it does not exists yet.
path to one or more files to edit.
the title of the editor window (not honoured by all editors, most external editors only display the file name or path).
editor to use. Either the name of the program, or a string containing the command to run, using %s as replacement tag where to place the filename in the command, or a function with 'file', 'title' and 'wait' arguments to delegate process of the files.
encoding of the files. If
one or more files to use as template if files must be created.
force replacement of files if
wait for edition to complete. If more than one file is edited, the program waits sequentially for each file to be edited in turn (with a message in the R console).
The function returns
TRUE if it was able to edit the files or
FALSE otherwise, invisibly. Encountered errors are reported as warnings.
The default editor program, or the command to run is in the
getOption("fileEditor") to retrieve it, and
options(fileEditor = "<myowneditor>") to change it). Default values are
On Unixes, "gedit", "kate" and "vi" are looked for in that order. Note that
there is a gedit plugin to submit code directly to R:
http://rgedit.sourceforge.net/. Since, gedit natively supports a lot
of different syntax highlighting, including R, and is lightweight but feature
rich, it is recommended as default text editor for
Unixes. If JGR is run and the editor is "vi" or "internal", then the internal
JGR editor is used, otherwise, the provided editor is chosen.
On Mac OS X, if the "edit" program exists, it is used (it is the command line
program installed by TextWrangler or BBEdit, see
http://www.barebones.com/products/, much more capables text editors
than the default TextEdit program), otherwise, the default text editor used
by OS X is choosen (default usually to TextEdit). TextWrangler can be
installed freely. It can be configured to highlight and submit R code. see
instructions. It features also several tools that makes it a much better
choice than TextEdit for
fileEdit() on Mac OS X. Specify "textwrangler"
or "bbedit" to force using these programs. The default value is "textedit",
the Mac default text editor, but on R.app, and with
wait = FALSE, the
internal R.app editor is used instead in that case. If JGR is run, and the
editor is "textedit", "internal" or "vi", then, the internal JGR editor is
On Windows, if Notepad++ is installed in its default location, it is used,
otherwise, the default "notepad" is used in Rterm and the internal editors are
chosen for Rgui. Notepad++ is a free text editor that is much better suited to
edit code or text files that the default Windows' notepad application, in
particular because it can handle various line end types (Unix, Mac or Windows)
and encodings. It also supports syntax highlighting, code completion and much
more. So, it is strongly recommended to install it (see
http://notepad-plus-plus.org/) and use it with
is also a plugin to submit code to R directly from Notepad++:
Of course, you can use your own text editor, just indicate it in the
fileEditor option. Note, however, that you should use only lighweight
and fast starting programs. Also, for the
wait = TRUE argument of
fileEdit(), you must check that R waits for the editor to be closed
before further processing code. In some cases, a little command line program
is used to start a larger application (like for Komodo Edit/IDE), or the
program delegates to an existing instances and exits immediatelly, even if the
file is still edited. Such editors are not recommended at all for
If you want to use files that are compatibles between all platforms supported by R itself, you should think about using ASCII encoding as much as possible and the Windows style of line-ending. That way, you ensure that all the default editors will handle those files correctly, including the broken default editor on Windows, notepad, which does not understand at all Mac or Unix line ends!
Philippe Grosjean <[email protected]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## Not run: ## Create a template file in the tempdir... tpl <- tempfile("template", fileext = ".txt") cat("Example template file\nto be used with fileEdit()\n", file = tpl) ## ... and edit a new file, starting from that template: newf <- tempfile("test", fileext = ".txt") fileEdit(newf, template = tpl, wait = TRUE) ## Make sure the content ends with \n, and read it cat("\n", file = newf, append = TRUE) cat("Your file contains:\n") readLines(newf) ## Eliminate both the file and template unlink(newf) unlink(tpl) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.