Description Usage Arguments Details Value Author(s) See Also Examples
High-level wrapper for stream editing functions (sed_insert
, sed_replace
,
sed_substitute
, and sed_comment
) to execute any number of sequential
insertion, replacement, substitution, or commenting steps.
1 2 | streamEdit(commandList, stream = NULL, inFile = NULL, outFile = NULL,
warn = FALSE)
|
commandList |
A list that designates the insertion, replacement, substitution, or commenting commands
that will be performed on |
stream |
A character vector, each element typically (but not necessarily) containing the text
from a single line in a file, which can be generated via |
inFile |
A character string designating a file that is to be read (using |
outFile |
A character string designating a file to which the resulting, edited stream will be
written using |
warn |
A logical that, when |
One (and only one) of inFile
or stream
must be specified.
If inFile
and outFile
are the same, a backup copy of inFile
is made by appending
"~" to the end of the filename, e.g., if the original file were ‘aFile.txt’, the backup would be
‘aFile.txt~’.
The value of warn
in streamEdit
is passed to the worker functions
(sed_insert
, sed_replace
, sed_substitute
, and sed_comment
)
unless the warn
argument is specified for a command in commandList
, in which case, for that
particular command, the locally supplied value of warn
takes precedence.
Invisibly returns the edited stream, and writes the stream to a file if outFile
is supplied.
Landon Sego
sed_insert
, sed_replace
, sed_substitute
, sed_comment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | ################################################################################
# Let's create an example stream we can edit
################################################################################
cat("Here's a line\n",
"Line after which we'll insert a string\n",
"A line we'll delete\n",
"A line we'll replace with something else\n",
"A line where we'll make a substitution\n",
"A line we'll comment\n",
"The last line\n",
sep = "", file = "tmpTest_streamEdit.txt")
# Read the file into a 'stream'
s <- readLines("tmpTest_streamEdit.txt")
################################################################################
# Excecute a series of commands 'manually', using the individual worker functions
################################################################################
s <- sed_insert(s, after = 3, insertion = "Here's an insertion")
s <- sed_replace(s, at = "delete", replacement = NULL)
s <- sed_replace(s, at = "we'll replace", replacement = "The replacement", fixed = TRUE)
s <- sed_substitute(s, pattern = "make a substitution", replacement = "have a party")
s <- sed_comment(s, at = "comment", type = "html")
s
################################################################################
# Now execute these same commands using a single call to streamEdit(), along
# with reading the input file and writing the output file
################################################################################
# Build the list of commands
comList <- list(
# i for 'insert', arguments for sed_insert()
i = list(after = 3, insertion = "Here's an insertion"),
# r for 'replace', arguments for sed_replace()
r = list(at = "delete", replacement = NULL),
# r for 'replace', arguments for sed_replace()
r = list(at = "we'll replace", replacement = "The replacement", fixed = TRUE),
# s for 'substitute', arguments for sed_substitute()
s = list(pattern = "make a substitution", replacement = "have a party"),
# c for 'comment', arguments for sed_comment()
c = list(at = "comment", type = "html")
)
s1 <- streamEdit(comList, inFile = "tmpTest_streamEdit.txt",
outFile = "tmpTest_streamEdit1.txt")
s1
# Compare the results
identical(s, s1)
# Remove the files
unlink(c("tmpTest_streamEdit.txt", "tmpTest_streamEdit1.txt"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.