Title: | A Unified Framework for Input-Output Operations in R |
---|---|
Description: | One function to read files. One function to write files. One function to direct plots to screen or file. Automatic file format inference and directory structure creation. |
Authors: | David J. H. Shih |
Maintainer: | David J. H. Shih <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.3.4 |
Built: | 2024-11-23 06:20:11 UTC |
Source: | https://bitbucket.org/djhshih/io |
This function returns whether a type is supported by
qread
or qwrite
.
io_supported(type)
io_supported(type)
type |
data or file type |
a data.frame
with logical entries;
TRUE
if type is supported, FALSE
otherwise
io_supported("rds")
io_supported("rds")
This function extends list.files
by excluding the listing of directories.
list_files(path = ".", full.names = FALSE, ...)
list_files(path = ".", full.names = FALSE, ...)
path |
a character vector of path names |
full.names |
whether to return absolute paths |
... |
other arguments passed to |
a character
vector of only names of files
list.files(R.home()) list_files(R.home())
list.files(R.home()) list_files(R.home())
If the data does not already exist as a file, evaluate an expression to generate the data and save it to file.
qcache(expr, file, cache = getOption("cache"), mkpath = TRUE, verbose = TRUE)
qcache(expr, file, cache = getOption("cache"), mkpath = TRUE, verbose = TRUE)
expr |
expression for generating data |
file |
filename |
cache |
whether to cache the result |
mkpath |
whether to create parent directories (if they do not already exists) |
verbose |
whether to output message |
a data object (object type depends on the expression
## Not run: qcache( { data(cars) with(cars, dist / speed) }, file = "times.vtr" ) ## End(Not run)
## Not run: qcache( { data(cars) with(cars, dist / speed) }, file = "times.vtr" ) ## End(Not run)
This funtion draws a plot to screen, a file, or both.
qdraw( expr, file = NULL, device = getOption("plot.device"), width = NULL, height = NULL, aspect.ratio = NULL, units = NULL, res = NULL, mkpath = TRUE, symlink = TRUE, ... )
qdraw( expr, file = NULL, device = getOption("plot.device"), width = NULL, height = NULL, aspect.ratio = NULL, units = NULL, res = NULL, mkpath = TRUE, symlink = TRUE, ... )
expr |
expression for plotting |
file |
filename |
device |
plot device |
width |
plot width [default: 5] |
height |
plot height [default: 5] |
aspect.ratio |
ratio of width to height |
units |
unit of plot dimension [default: "in"] |
res |
bitmap resolution, used only by bitmap formats [default: 300] |
mkpath |
whether to create parent directories (if they do not already exists) |
symlink |
whether to create a symlink to file with a simplified
filename (ignored if file is not a |
... |
other arguments passed to the plot device function |
To send the plot to screen, set device
to NA (default).
Optionally, to print the plot on screen to a file, specify file
.
If device
is NULL
, the plot will be sent directly to the
the specified file
using a printing device inferred from the file
extension (no graphical window will open).
Set the global option plot.device
to affect multiple plots.
Graphical parameters including width
, height
, res
,
units
are obtained from the global option getOption("plot")
.
## Not run: # Set device to jpeg (remember to update file extensions for printed plots) options(plot.device=jpeg) qdraw(plot(1:10), "plot.jpeg") # Enable automatic plot format inference options(plot.device=NULL) # Plot directly to file (format is inferred from filename extension) qdraw(plot(1:10), "plot.pdf") # Plot to screen, then print to file (display will not be closed) qdraw(plot(1:10), "plot.png", device=NA) # If an error occurs, be sure to clear the current plot dev.off() # or clear all plots graphics.off() ## End(Not run)
## Not run: # Set device to jpeg (remember to update file extensions for printed plots) options(plot.device=jpeg) qdraw(plot(1:10), "plot.jpeg") # Enable automatic plot format inference options(plot.device=NULL) # Plot directly to file (format is inferred from filename extension) qdraw(plot(1:10), "plot.pdf") # Plot to screen, then print to file (display will not be closed) qdraw(plot(1:10), "plot.png", device=NA) # If an error occurs, be sure to clear the current plot dev.off() # or clear all plots graphics.off() ## End(Not run)
This function reads a file in a specified format.
qread(file, type = NULL, ...)
qread(file, type = NULL, ...)
file |
file name (character or |
type |
data or file type |
... |
other arguments passed to the underlying function |
If type
is NULL
, the file type is inferred from
the file extension.
Use io_supported
to check support for a file or data type.
a data object (type depends on the underlying function)
## Not run: data(cars) # write data to an RDS file qwrite(cars, "cars.rds") # infer output type based on the class of the cars object qwrite(cars, "cars.dfm", type=NA) # read data back in x1 <- qread("cars.rds") # specify the type explicitly x3 <- qread("cars.dfm", type="data.frame") # read all files (with extension) in current directory xs <- qread(".", pattern="cars") ## End(Not run)
## Not run: data(cars) # write data to an RDS file qwrite(cars, "cars.rds") # infer output type based on the class of the cars object qwrite(cars, "cars.dfm", type=NA) # read data back in x1 <- qread("cars.rds") # specify the type explicitly x3 <- qread("cars.dfm", type="data.frame") # read all files (with extension) in current directory xs <- qread(".", pattern="cars") ## End(Not run)
This function writes an object to file in a specified format.
qwrite(x, file, type = NULL, mkpath = TRUE, symlink = TRUE, ...)
qwrite(x, file, type = NULL, mkpath = TRUE, symlink = TRUE, ...)
x |
data object to write |
file |
filename (character or |
type |
data or file type |
mkpath |
whether to create parent directories (if they do not already exists) |
symlink |
whether to create a symlink to file with a simplified
file name (ignored if file is not a |
... |
other arguments passed to the underlying function |
If type
is NULL
, the file type is inferred from
the file extension. If type
is NA
or if the file extension is
unavailable or unknown, type
is inferred from class(x)
.
Use io_supported
to check support for a file or data type.
a data object (object type depends on the underlying function)
## Not run: data(cars) # write data to a TSV file qwrite(cars, "cars.tsv") # infer output type based on the class of the cars object qwrite(as.matrix(cars), "cars.mtx", type=NA) ## End(Not run)
## Not run: data(cars) # write data to a TSV file qwrite(cars, "cars.tsv") # infer output type based on the class of the cars object qwrite(as.matrix(cars), "cars.mtx", type=NA) ## End(Not run)
Write to RDS file
## S3 method for class 'rds' qwrite(x, file, type, deref = TRUE, ...)
## S3 method for class 'rds' qwrite(x, file, type, deref = TRUE, ...)
x |
data object to write |
file |
filename (character or |
type |
data or file type |
deref |
whether to deference data links in DelayedMatrix |
... |
other arguments passed to |