Learn R Programming

mvbutils (version 2.2.0)

fixr: Editing functions

Description

fixr opens a function (or text stored as a character vector) in your preferred text editor. Control returns immediately to the R command line, so you can keep working in R and can be editing several functions simultaneously (cf edit). A session-duration list of objects being edited is maintained, so that each object can be easily sourced back into its rightful workspace, either manually by calling FF() or automatically on file change (see autoedit below). There is an optional automatic text backup facility. readr also opens a file in your text editor, but in read-only mode, and doesn't update the backups or the list of objects being edited. fixtext is a shorthand form for forcing creation of a text object rather than the default of a function.

Usage

# Usually: fixr( x)
  fixr( x, new=FALSE, install=FALSE, what, fixing, pkg=NULL, character.only=FALSE)
  # fixtext really has exact same args as fixr, but technically it
  fixtext( x, ...)
  # Usually: readr( x) but exact same args as fixr
  readr( x, ...)
  FF()
  autoedit( do=TRUE)

Arguments

x
a quoted or unquoted name of a function or text object.
new
(rarely used) if TRUE, edit a blank function template rather than any existing copy in the search path. New edit will go into .GlobalEnv unless argument pkg is set.
install
(rarely used) logical indicating whether to go through the process of asking you about your editor
what
if no pre-existing x, then fixr creates an empty function template by default. Set what="" to create an empty character vector instead-- or just use fixtext.
fixing
FALSE for read-only (i.e. just opening editor to examine the object)
pkg
(string or environment) if non-NULL, then specifies in which package a specific maintained package (see maintain.packages) x should be looked for.
character.only
(boolean) if TRUE, x is treated as a string naming the object to be edited, rather than the unquoted object name.
do
TRUE => automatically update objects from altered files; FALSE => don't.
...
other arguments (except what in fixtext, and fixing in readr) are passed to fixr.

Details

When fixr is run for the first time (or if you set install=TRUE), it will ask you for some basic information about your text editor. In particular, you'll need to know what to type to invoke your text editor from the command shell (MS-DOS prompt on Windows) on a specific file. After supplying these details, fixr will launch the editor and print a message showing some options ("backup.fix", "edit.scratchdir" and "program.editor"), that will need to be set in your .First. function-- which you can do via fixr(.First). readr requires a similar installation process. To get the read-only feature, you'll need to add some kind of option/switch on the command line that invokes your text editor; not all text editors support this feature. Similarly to fixr, you'll need to set options( program.reader=<>) in your .First; the installation process will tell you what to use. fixr will also edit character vectors. It will "respect" class attributes on character vectors; for example, I often set the class to "cat" so that print invokes my print.cat method, which displays text more readably than the default. Any other attributes on character vectors are stripped. fixr creates a blank function template if none existed, or if new=TRUE is specified. If you want to create a new character vector as opposed to a new function, set what="" when you call fixr. If the function has attributes, it's wrapped in a structure(...) construct. If a doc attribute exists, it's printed as free-form text at the end of the file, and the call to structure will end with a line similar to: ,doc=flatdoc( EOF="<>")) When the file is read later on (see next paragraph, and source.mvb for technical details), that line will cause the rest of the file-- which should be free-format text, with no escape characters etc.-- to be read in as a doc attribute, which can be displayed by help. If you want to add "flat-format documentation", you can also add these lines yourself-- see flatdoc. To read functions back in again, call FF(), which checks the modification times of any function files. If any have been updated, FF sources in the code and modifies the appropriate functions. It tries to write functions back into the workspace they came from, which might not be .GlobalEnv. If not, you'll be asked whether you want to save.image that workspace (provided it's a "task"-- see cd). FF should still put the function in the right place, even if you've called cd after calling fixr (unless you've detached the original task) or if you moved it. To have FF run automatically whenever a valid command is executed (e.g. by typing 0; alone doesn't work), run autoedit(). If the function was being mtraced (see help(package=debug)), FF will re-apply mtrace after loading the edited version. If there is a problem with parsing, the source attribute of the function is updated to the new code, but the function body is invisibly replaced with a stop call, stating that parsing failed. FF also prints an immediate warning. The list of functions being edited by fixr is stored in the variable fix.list in the mvb.session.info environment. When you quit and restart R, the function files you have been using will stay open in the editor, but fix.list will be empty; hence, updating the file "myfun.r" will not update the corresponding R function. If this happens, just type fixr(myfun) in R and when your editor asks you if you want to replace the on-screen version, say no. Save the file again (some editors require a token modification, such as space-then-delete, first) and R will notice the update. An automatic text backup facility is available from fixr and FF: see ?get.backup. The backup system also allows you to sort functions by edit date; see ?fix.order. Backup only works for functions. fixtext returns a text vector with class cat; the print method for such objects is just cat( x, sep="\n") which avoids all the "[1]" and escape-character clutter.

See Also

.First, edit, cd, get.backup, fix.order, move, autoFF