setReadWriteArgsMethod | R Documentation |
Create a method for reading/writing command-line arguments from/to a file, typically by differing file extension.
has.ext(file, fileext, compression = FALSE, fixed = FALSE,
ignore.case = TRUE)
scan2(...)
format4scan(x, sep = " ", quote = "\"'", comment.char = "",
allowEscapes = FALSE, nlines.between.comment.and.args = 0,
nlines.between.args = 2)
setReadWriteArgsMethod(name, condition, read, write, sealed = FALSE)
file |
character vector, the files to test for a file extension. |
fileext |
character string, the file extension to be matched. Should
contain a |
compression |
logical. Should compressed files be included in the pattern matching? |
fixed |
logical. If |
ignore.case |
logical. Should the case of |
... |
arguments passed to |
sep |
Empty character string, |
quote |
Character string or |
comment.char |
character string containing a single character or an
empty string. Use |
allowEscapes |
logical. Should C-style escapes be processed? |
x |
any R object. If a list, each element will be turned into a
character vector (in the same way as |
nlines.between.comment.and.args |
a non-negative integer specifying the number of empty lines between each set of comments and arguments. |
nlines.between.args |
a non-negative integer specifying the number of
empty lines between each set of arguments. Only used for a list |
name |
A character string naming the method. |
condition |
a function accepting a single argument |
read |
a function accepting a single argument |
write |
a function accepting arguments |
sealed |
|
# suppose you wanted to define your own method for
# reading/writing command-line arguments to a file. we'll
# say the file extension will be ".myargs". with this, we
# start by making 'condition'
condition <- function(file) {
essentials::has.ext(file, ".myargs",
compression = TRUE, fixed = TRUE)
}
# next, we will make a reading function. this will typically
# be some variation of 'scan2', but feel free to use
# anything else that works. for this example, we'll use
# "-" as the delimiter, "`" as the quoting character, and
# "/" as the comment character
read <- function(file) {
essentials::scan2(file = file, sep = "-",
quote = "`", comment.char = "/")
}
# next, we will make a writing function. this will typically
# be some variation of 'format4scan', but feel free to use
# anything else that works
write <- function(x, comments = TRUE,
nlines.between.comment.and.args = 0,
nlines.between.args = 2) {
essentials::format4scan(x, sep = "-", quote = "`",
comment.char = if (comments) "/" else "",
nlines.between.comment.and.args = nlines.between.comment.and.args,
nlines.between.args = nlines.between.args)
}
# now, combine it all together
essentials::setReadWriteArgsMethod(
name = "myargs",
condition = condition,
read = read,
write = write
)
# try writing arguments with this new format
x <- letters
comment(x) <- "testing comments"
essentials::writeArgs(x, "", name = "myargs")
# confirm that writing and reading returns the same set of
# arguments
FILE <- essentials::writeArgs(x, fileext = ".myargs", at = FALSE)
y <- essentials::readArgs(FILE)
stopifnot(length(x) == length(y), x == y)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.