Learn R Programming

mvbutils (version 2.5.0)

mvbutils.utils: Miscellaneous utilities

Description

Miscellaneous utilities.

Usage

as.cat( x)
clip( x, n=1)
cq( ...)
deparse.names.parsably( x)
empty.data.frame( ...)
exists.mvb( x, pos = -1, envir = pos.to.env(pos), frame, mode = "any", inherits = FALSE)
expanded.call( nlocal=sys.parent())
find.funs(pos=1, ..., exclude.mcache = TRUE, mode="function")
index( lvector)
is.dir( dir)
legal.filename( name)
lsall( ...)
lib.pos() # used only to hack 'library'
masked( pos)
masking( pos=1)
mkdir( dirlist)
my.all.equal( x, y)
named( x)
option.or.default( opt.name, default=NULL)
pos( substrs, mainstrs, any.case = FALSE, names.for.output)
## S3 method for class 'cat':
print( x, ...) # print.cat
## S3 method for class 'nullprint':
print( x, ...) # print.nullprint
put.in.session( ...)
returnList( ...)
safe.rbind( df1, df2)
to.regexpr( x)
yes.no( prompt, default)

Arguments

x, y, n, ..., exclude.mcache, nlocal, lvector, dir, name, pos, envir, frame, mode, inherits, dirlist, opt.name, default, substrs, mainstrs, any.case, names.for.output, df1, df2, prompt
see "Arguments by function"

Value

  • as.catcharacter vector of class cat
  • clipvector of the same mode as x
  • cqcharacter vector
  • empty.data.framedata.frame
  • expanded.calla call object
  • find.funsa character vector of function names
  • is.dirlogical vector
  • is.nonzeroTRUE or FALSE
  • legal.filenamecharacter( 1)
  • maskedcharacter vector
  • maskingcharacter vector
  • mkdirlogical vector of success/failure
  • namedvector of the same mode as x
  • option.or.defaultoption's value
  • posnumeric matrix, one column per match found plus one; at least one column guaranteed
  • returnListlist or single object
  • safe.rbinddata.frame
  • to.regexprcharacter
  • yes.noTRUE or FALSE

Details

as.cat makes a character vector print as if it was catted rather than printed. clip removes the last n elements of x. cq is handy for typing cq( alpha, beta, gamma) instead of cq( "alpha", "beta", "gamma"). Certain strings DO still require quotes around them, e.g. cq( "NULL", "1-2")). deparse.names.parsably is like deparse except that name objects get wrapped in a call to as.name, so that they won't be evaluated accidentally. empty.data.frame creates a template data frame with 0 rows but with all columns of the appropriate type. Useful for rbinding to later-- see also safe.rbind. exists.mvb is pretty much like exists but has a pos rather than a where argument. find.funs finds "function" objects (or objects of other modes, via the "mode" arg) in one or more environments, optionally matching a pattern. expanded.call returns the full argument list available to its caller, including defaults where arguments were not set explicitly. The arguments may not be those originally passed, if they were modified before the invocation of expanded.call. Default arguments which depend on calculations after the invocation of expanded.call will lead to an error. index returns the position(s) of TRUE elements. Unlike which: attributes are lost; NA elements map to NAs; index(<>) is numeric(0); index( <>) is NA. is.dir tests for directoriness. legal.filename coerces its character argument into a similar-looking string that is a legal filename on any (?) system. lib.pos is used by mvbutils in hacking library, to make sure packages get attached below ROOT task. You'll probably never need to call this directly. lsall is like ls but coerces all.names=TRUE. masked checks which objects in search()[pos] are masked by identically-named objects higher in the search path. masking checks for objects mask identically-named objects lower in the search path. Namespaces may make the results irrelevant. mkdir makes directories; unlike dir.create, it can do several levels at once. my.all.equal is like all.equal, except that it returns FALSE in cases where all.equal returns a non-logical-mode result. named(x) is just names(x) <- as.character( x); x; useful for lapply etc. option.or.default returns the named option value if it exists, otherwise the supplied default. Obsolete in R2.10; use getOption( ..., default=...) instead. pos is probably to be eschewed in new code, in favour of gregexpr with fixed=TRUE, which is likely faster. (And I should rewrite it to use gregexpr.) It's one of a few legacy functions in mvbutils that pre-date improvements in base R. pos will either search for several literal patterns in a single target, or vice versa- not both. It returns a matrix showing the positions of the matching substrings, with as many columns as the maximum number of matches. 0 signifies "no match"; there is always at least one column even if there are no matches at all. print: if class(x)=="cat", the character vector x will be printed by cat(x,sep="\n"). If class(x)=="nullprint", then print(x) will not print anything. returnList returns a list corresponding to old-style (pre-R 1.8) return syntax. Briefly: a single argument is returned as itself. Multiple arguments are returned in a list. The names of that list are the argument names if provided; or, for any unnamed argument that is just a symbolic name, that symbolic name; or no name at all, for other unnamed arguments. You can duplicate pre-1.8 behaviour of return(...) via return(returnList(...)). safe.rbind mimics rbind, but works round an R bug (I reckon) where a column appears to be a numeric in one data.frame but a factor in the other. to.regexpr converts literal strings to their equivalent regexps, e.g. by doubling backslashes. Useful if you want "fixed=TRUE" to apply only to a portion of your regexp. yes.no cats its "prompt" argument and waits for user input. if the user input pmatches "yes" or "YES", then yes.no returns TRUE; if the input pmatches no or NO then yes.no returns FALSE; if the input is '' and default is set, then yes.no returns default; otherwise it repeats the question. You probably want to put a space at the end of prompt.

Examples

Run this code
clip( 1:5, 2) # 1:3
cq( alpha, beta) # c( "alpha", "beta")
empty.data.frame( a=1, b="yes")
# data.frame with 0 rows of columns "a" (numeric) and
# "b" (a factor with levels ""yes"")
f <- function( a=9, b) expanded.call(); f( 3, 4) # list( a=3, b=4)
find.funs( "package:base", patt="an") # "transform" etc.
is.dir( getwd()) # TRUE
legal.filename( "a:b\\c/d&f") # "a.b.c.d&f"
sapply( named( cq( alpha, beta)), nchar)  # c( alpha=5, beta=4)
option.or.default( "my.option", 5) # probably 5
pos( cq( quick, lazy), "the quick brown fox jumped over the lazy dog")
# matrix( c( 5, 37), nrow=2)
pos( "quick", c( "first quick", "second quick quick", "third"))
# matrix( c( 7,8,0, 0,14,0), nrow=3)
pos( "quick", "slow") # matrix( 0)
f <- function() { a <- 9; return( returnList( a, a*a, a2=a+a)) }
f() # list( a=9, 81, a2=18)
levels( rbind( data.frame( x=1), data.frame( x="cat"))$x)
# NULL, because "x" acquires mode "character"; a bug, I think
levels( safe.rbind( data.frame( x=1), data.frame( x="cat"))$x)
# c( "1", "cat")
to.regexpr( "a{{") # "a\\\\\{\\\\\{"
mkdir( "subdirectory.of.getwd")
yes.no( "OK (Y/N)? ")
masking( 1)
masked( 5)

Run the code above in your browser using DataLab