| Type: | Package |
| Title: | File Abstraction Interface Layer (FAIL) |
| Description: | More comfortable interface to work with R data or source files in a key-value fashion. |
| Version: | 1.3 |
| Author: | Michel Lang <michellang@gmail.com> |
| Maintainer: | Michel Lang <michellang@gmail.com> |
| URL: | https://github.com/mllg/fail |
| License: | BSD_3_clause + file LICENSE |
| Imports: | stats, utils, BBmisc, checkmate |
| Suggests: | testthat |
| ByteCompile: | yes |
| NeedsCompilation: | no |
| Packaged: | 2015-09-30 11:01:23 UTC; lang |
| Repository: | CRAN |
| Date/Publication: | 2015-10-01 00:21:25 |
Create a file abstraction interface layer (FAIL) object.
Description
The general idea is to not bother about file path joining or file extensions. Instead, FAIL offers a key-value like interface to RData files in a specified directory. The filename (without extension) acts as the key while the stored R objects are the values. Fail provides an interface to the basic file system actions: listing, reading / loading, writing / saving, removing and applying functions on files. An implemented cache mechanism can be used to avoid repeated disk reads.
Usage
fail(path = getwd(), extension = "RData", all.files = FALSE,
use.cache = FALSE, simplify = TRUE)
Arguments
path |
[ |
extension |
[ |
all.files |
[ |
use.cache |
[ |
simplify |
[ |
Details
For a quick introduction on the usage, see https://github.com/mllg/fail.
An object with the following functions is returned by fail:
ls(pattern=NULL)-
Function to list keys in directory
pathmatching a regular expression patternpattern. Returns a character vector of keys. get(key, use.cache)-
Function to load a file identified by
keyfrom directorypath. To load many objects at once, useas.list,assignorgettogether withlapply. Argumentuse.cachecan be set to temporarily overwrite the globaluse.cacheflag. put(..., li, keys, use.cache)-
Function to save objects to directory
path. Names for objects provided via...will be looked up or can be provided using akey = valuesyntax. More objects can be passed as a named list using the argumentli: Each list item will be saved to a separate file. If you providekeysas a character vector, these names will be taken for the arguments passed via.... Argumentuse.cachetemporarily overwrites the globaluse.cacheflag. Returns a character vector of stored keys. remove(keys)-
Function to remove files identified by
keysfrom directorypath. Returns a character vector of deleted keys. apply(FUN, ..., keys, use.cache, simplify=FALSE, use.names=TRUE)-
Apply function
FUNon files identified bykeys.keysdefaults to all keys available and will be used to name the returned list. The loaded R objects will be past unnamed as first argument. Use...for additional function arguments. Argumentuse.cachecan be set to temporarily overwrite the globaluse.cacheflag. For argumentssimplifyanduse.names, seelapply. mapply(FUN, ..., keys, use.cache, moreArgs = NULL, simplify=FALSE, use.names=TRUE)-
Apply function
FUNon files identified bykeys.keysdefaults to all keys available and will be used to name the returned list. The functionFUNmust have the formal arguments “key” and “value”. Both key and value will be passed named. Use...and/ormoreArgsfor additional function arguments. Argumentuse.cachecan be set to temporarily overwrite the globaluse.cacheflag. For argumentsmoreArgs,simplifyanduse.names, seemapply. as.list(keys, use.cache)-
Return a named list of objects identified by
keys.keysdefaults to all keys available. Argumentuse.cachecan be set to temporarily overwrite the globaluse.cacheflag. assign(keys, envir=parent.frame(), use.cache)-
Assigns all objects identified by the character vector
keysin the environmentenvir. Argumentuse.cachecan be set to temporarily overwrite the globaluse.cacheflag. Returns a character vector of assigned keys. clear(keys)-
Clear the cache to free memory.
keysdefaults to all keys available. Returns a character vector of cleared keys. cached()-
Returns a character vector of keys of cached objects.
size(keys, unit="b")-
Get the file size in Bytes of the files identified by
keys.keysdefaults to all keys available. Argumentunitaccepts “b”, “Kb”, “Mb” and “Gb” and can be used to convert Bytes to KiloBytes, MegaBytes or GigaBytes, respectively. info()-
Returns a named list with
path,extensionanduse.cache. Internally used for theprintmethod with a much nicer summary of the FAIL object.
Furthermore, the package provides S3 methods for print and as.list.
Be aware of the following restriction regarding file names and keys:
The package performs some basic checks for illegal characters on the key names.
In principle all characters matching the pattern “[a-zA-Z0-9._-]” are allowed and should work on most or all file systems.
But be careful with key names which are not compatible with R's variable naming restrictions, e.g. using the minus character or
key names starting with a number: these provoke unwanted side effects and will result in errors if used with assign.
If two files would collide on case-insensitive file systems like Windows' NTFS, the package will throw warnings. Best practice is to not rely on case sensitivity.
Value
Object of class fail. See details.
Examples
# initialize a FAIL in a temporary directory
path <- tempfile("")
files <- fail(path)
# save x and y, vectors of random numbers
x <- runif(100)
files$put(x, y = runif(100))
# save columns of the iris data set as separate files
files$put(li = as.list(iris))
# load all RData files in a named list as a one-liner
as.list(fail(path))
# load a single object from the file system
files$get("Species")
files$as.list(c("x", "y"))
# remove an object (and related file)
files$remove("Species")
# apply a function over files
files$apply(mean)
files$mapply(function(key, value) sprintf("%s -> %f", key, mean(value)), simplify = TRUE)
# show file size informations
files$size(unit = "Mb")
# get an object and cache it
files$get("x", use.cache = TRUE)
files$cached()
files$clear()
files$cached()
# assign variables in the current environment
files$assign("y")
mean(y)
Create a source abstraction interface layer (SAIL) object.
Description
This function returns an object of class sail which behaves
like fail, but is indented for loading and saving
R source code files.
Usage
sail(path = getwd(), extension = "R", all.files = FALSE,
use.cache = FALSE, simplify = TRUE, suppressMessages = FALSE)
Arguments
path |
[ |
extension |
[ |
all.files |
[ |
use.cache |
[ |
simplify |
[ |
suppressMessages |
[ |
Value
Object of class sail. See the documentation of fail
for details.