R/getName.environment.R

###########################################################################/**
# @class "environment"
# @RdocMethod getName
#
# @title "Gets the name of an environment"
#
# \description{
#  @get "title", e.g. \code{"R_GlobalEnv"} or \code{"0x01ddd060"}.
# }
#
# @synopsis
#
# \arguments{
#   \item{env}{An @environment.}
#   \item{...}{Not used.}
# }
#
# \value{
#   Returns a @character string.
# }
#
# \examples{
#   name <- getName(globalenv())
#   print(name)
#   stopifnot(identical(name, "R_GlobalEnv"))
#
#   getName(new.env())
# }
#
# @author
#
# \seealso{
#   \code{\link[base:environment]{environmentName}()}.
# }
#
# \keyword{programming}
#*/###########################################################################
setMethodS3("getName", "environment", function(env, ...) {
  # base::environmentName() was added to R v2.5.0
  if (exists("environmentName", mode="function")) {
    name <- environmentName(env)
  } else {
    name <- ""
  }

  if (name == "") {
    name <- capture.output(print.default(env))
    name <- name[1]; # Just in case
    name <- gsub("[<]*environment:[ ]*([^>]*)[>]", "\\1", name)
  }

  name
})

Try the R.oo package in your browser

Any scripts or data that you put into this service are public.

R.oo documentation built on Nov. 2, 2024, 1:07 a.m.