[R] easier way to update packages i'm developing?

Henrik Bengtsson hb at maths.lth.se
Fri Jan 18 23:55:04 CET 2002


For the reasons you mention I wrote a 'relibrary()' function, which detaches
and reloads a library. It is basically a modification of the (R v1.3.1)
library() function. The reason for the cut-n-paste from the library() source
instead of a function that calls library() internally is that library makes
use of substitute() which is a very odd creature. Anyway, the code (and the
Rd source) for relibrary() is below.

Cheers

Henrik Bengtsson

Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
Office: P316, +46 46 222 9611 (phone), +46 46 222 4623 (fax)
h b @ m a t h s . l t h . s e
http://www.maths.lth.se/matstat/staff/hb/

#########################################################################/**
# \name{relibrary}
# \alias{relibrary}
#
# \title{Reloads a package}
#
# \usage{
#   relibrary(package, character.only=FALSE, warn.conflicts=FALSE, ...)
# }
#
# \description{
#  Reloads a package. This function works exactly the same as
#  \code{\link[base]{library}}, \emph{except} that it is reloads the package
#  if it already loaded. This is useful for developers.
#  For more information see \code{\link[base]{library}}.
# }
#
# \arguments{
#   \item{package}{Name or character string giving the name of a package.}
#   \item{character.only}{A logical indicating whether \code{package} can
#      be assumed to be character strings. Default value is \code{FALSE}.}
#   \item{warn.conflicts}{If \code{TRUE}, warnings are printed about
#      conflicts from reattaching of the package, unless that package
#      contains an object \code{.conflicts.OK}. Default value is
#      \code{FALSE}.}
#   \item{...}{Any other arguments that \code{\link[base]{library}} takes.}
# }
#
# \details{
#   While relibrary is reloading a package the option \code{relibrary} will
#   be set to name of the package currently reloaded. This can be useful if
#   the package to be reloaded would like save away data until it is loaded
#   again.
# }
#
# \author{
#   Henrik Bengtsson, \email{henrikb at braju.com},
#   \url{http://www.braju.com/R/}
# }
#
# \seealso{
#   See \code{\link[base]{library}}.
# }
#
# @visibility public
#*/#########################################################################
t
relibrary <- function(package, character.only=FALSE, warn.conflicts=FALSE,
...) {
  if (!character.only)
    package <- as.character(substitute(package));

  options.relibrary <- options(relibrary=package)[[1]];

  # If package is already attached, then detach it first.
  pkgName <- paste(sep="", "package:", package);
  pos <- match(pkgName, search());
  if (!is.na(pos)) {
    # If there exists a function .Last.lib() in the package call it first!
    if (exists(".Last.lib", where=pos, inherits=FALSE)) {
      .Last.lib <- get(".Last.lib", pos=pos, inherits=FALSE);
      if (is.function(.Last.lib)) {
        libpath <- attr(pos.to.env(pos), "path");
        if (!is.null(libpath))
          try(.Last.lib(libpath));
      }
    }
    # ...then remove the package.
    .Internal(detach(pos));
  }

  library(package=package, character.only=TRUE,
                           warn.conflicts=warn.conflicts, ...);
  options(relibrary=options.relibrary);
} # relibrary



############################################################################
# HISTORY:
# 2001-08-06
# * Now relibrary will set the option "relibrary" to the name of the package
#   it is currently reloading. This option can be used by the package itself
#   in its .First.lib and .Last.lib for instance in case it will store
#   something until it is loaded again.
# 2001-08-03
# * Made relibrary by default not warning for conflicts.
# 2001-07-05
# * It is NOT possible to use UseMethod("relibrary", obj, ...).
# 2001-05-03
# * Created.
############################################################################


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Jeff D. Hamann
> Sent: Friday, January 18, 2002 7:41 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] easier way to update packages i'm developing?
>
>
> I'm frightfully new at developing r packages. I'm developing an
> econometrics
> package, and each time I update my beta package, I close R, delete the
> package dir in the library dir, rebuild the zip file, rerun R,
> reinstall the
> package and run the functions. Is there a more efficient way?
>
> Jeff.
>
> Jeff D. Hamann
> Hamann, Donald & Associates, Inc.
> PO Box 1421
> Corvallis, Oregon USA 97339-1421
> Bus. 541-753-7333
> Cell. 541-740-5988
> jeff_hamann at hamanndonald.com
> www.hamanndonald.com
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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