Learn R Programming

mvbutils (version 2.2.0)

mvbutils-package: How to use the mvbutils package

Description

Package mvbutils is a collection of utilities offering the following main features:
  • Hierarchical organization of projects and sub-projects, allowing switching within a single R session, searching and moving objects through the hierarchy, objects in ancestor projects always visible from child (sub)projects, etc. Seecd.
  • Improved function & text-object editing facilities, interfacing with whichever text editor you prefer. The R command line is not frozen while editing, and you can have multiple edit windows open. There is also a complete automatic text-format backup system for functions. Seefixr.
  • "Lazy loading" for individual objects, allowing fast & efficient access to collections of biggish objects where only a few objects are used at a time. Seemlazy.
  • Support for flat-format (plain-text) documentation, stored along with a function and editable at the same time, and viewable through normalhelp(i.e. semi-literate programming). Automatic conversion to Rd format is available if certain rules are followed. Seeflatdoc.
  • Support for transparent "live" editing of your own loaded packages (even/especially with namespaces), and for conversion to formal R source package format; seemaintain.packages.
  • Support for nesting ofsourcecalls, and for interspersing R code and data in the same file. Seesource.mvb.
  • Support for easy macro-like functions which act in their caller's environment (see also RNews 1/3), and for "dynamically scoped" functions. Seemlocalanddo.in.envir.
  • Graphical display of which functions call which other functions. Seefoodweb.
There are also numerous lower-level utility functions and operators: see ?"mvbutils-utils" and ?"mvbutils-operators". To get the full features of the mvbutils package (in particular, the project organization), you need to start R in the same directory every time (your "ROOT task"), and then switch to whichever project from inside R; see cd. Various options always need to be set to make fixr and the debug package work the way you want, so one advantage of the start-in-the-same directory-approach is that you can keep all your project-independent options(), library loads, etc., in a single .First function, called automatically when you start R. However, most features (including support for the debug package) will probably work even if you don't follow this suggestion. On loading, the mvbutils package creates a new environment in the search path, called mvb.session.info, which stores some housekeeping information. mvb.session.info is never written to disk, and disappears when the R session finishes. [For Splus users: mvb.session.info is similar to frame 0.] You should never change anything in mvb.session.info by hand, but it is sometimes useful to look at some of the variables there:
  • .First.top.searchis the directory R started in (your ROOT task).
  • .Pathshows the currently-attached part of the task hierarchy.
  • base.xxxis the original copy of an overwritten system function, e.g.help
  • fix.listkeeps track of functions being edited viafixr
  • session.start.timeis the value ofSys.time()whenmvbutilswas loaded
  • source.listis used bysource.mvbto allow nesting of sources
  • r.window.handleis used by thehandypackage (Windows only)
  • things whose name starts with ".." are environments used in live-editing packages

Arguments

Note2

On loading, the present version of package mvbutils compulsorily overwrites a few system functions: library, rbind.data.frame, lockEnvironment. By default, it also overwrites help, savehistory, loadhistory, save.image, difftime, +.POSIXt, and -.POSIXt. (The original version of routine xxx can always be obtained via base.xxx if you really need it.) The modifications should have [almost] no side-effects, and/but I hope to be able to avoid them altogether in future versions of R. Briefly:
  • libraryis modified so that its defaultposargument becomes a call tolib.pos(). This means that packages get attached just below ROOT rather than always in position 2 (needed bycd).
  • lockEnvironmentgets an X-rated hack to allow editing of "live" packages (seemaintain.packages)-- no change to default behaviour.
  • rbind.data.frameis modified to work better (IMO) when the firstdata.framehas zero rows, to cope with a code-breaking change in R's behaviour several versions ago. Specifically, the modified version doesnotdrop zero-row data.frames, and their column attributes are taken account of whenrbinding to the other args. In other words,rbind( data.frame( x=1, y="a"), data.frame( x=2, y="b"))[-1,]$ynow has the same levels asrbind( data.frame( x=1, y="a")[-1,], data.frame( x=2, y="b"))$y. Experiment withbase.rbind.data.frameinstead, if you want to see the difference.
  • helpis modified so that, if systemhelpcan't find help for a simple object (not a method or package), it will look for thedocattribute of that object (if any) and display it in a pager usingdochelp.
  • savehistoryandloadhistoryare modified so that they use theR_HISTFILEenvironment variable if it set. This can be set dynamically during an R session usingSys.setenv. Standard R behaviour is to respectR_HISTFILEiff it is setbeforethe R session starts. Note that, by default,cdwill on first use setR_HISTFILEto what it thinks is the ROOT task, so that same the history file will be used throughout the session; seecdif you want to change this.
  • save.imageis modified to callSaveinstead; this will behave exactly the same for workspaces not usingmvbutilstask-hierarchy feature or thedebugpackage, but otherwise will prevent problems withmtraced functions andmlazyed functions.
  • difftime,+.POSIXt, and-.POSIXtare modified to behave more consistently and forgivingly. Results won't break code that doesn't make invalid assumptions.
If you are certain that you don't want the optional replacements, set options(mvbutils.replacements=FALSE) before loading mvbutils. However, this will prevent cd, fixr, and the flat-documentation help from working properly. You can undo the modification of an individual function called xxx with assign.to.base( "xxx", base.xxx). ESS.and.'mvbutils' For ESS users: I'm not an Emacs user and so haven't tried ESS with the mvbutils package myself, but a read-through of the ESS documentation suggests that a couple of ESS variables may need changing to get the two working optimally. Please check the ESS documentation for further details on these points. I will update this helpfile when/if I receive more feedback on what works.
  • cdchanges the search list, so you may need to alter "ess-change-sp-regex" in ESS.
  • cdalso changes the prompt, so you may need to alter "inferior-ess-prompt". Prompts have the form WORD1/WORD2/.../WORDn> where WORDx is a letter followed by one or more letters, underscores, periods, or digits.
  • movecan add/remove objects in workspaces other than the top one, so if ESS relies on stored internal summaries of "what's where", these may need updating.
Display bugs: if you have a buggy Unix display where readline() always returns the cursor to the start of the line, overwriting any prompt, then try options( cd.extra.CR=TRUE).

See Also

cd, fixr, mlazy, flatdoc, dochelp, maintain.packages, source.mvb, mlocal, do.in.envir, foodweb, mvbutils-operators, mvbutils-utils, package debug