copySubstitute: Copy Between Connections or Files with Configure-Like...

Description Usage Arguments Details Value Author(s) Examples

View source: R/tools.R

Description

Copy files, directory trees or between connections and replace all occurences of a symbol by the corresponding value.

Usage

1
2
copySubstitute(src, dest, symbolValues, symbolDelimiter="@", allowUnresolvedSymbols=FALSE,
               recursive = FALSE, removeExtension = "\\.in$")

Arguments

src

Source, either a character vector with filenames and/or directory names, or a connection object.

dest

Destination, either a character vector of length 1 with the name of an existing, writable directory, or a connection object. The class of the dest argument must match that of the src argument.

symbolValues

A named list of character strings.

symbolDelimiter

A character string of length one with a single character in it.

allowUnresolvedSymbols

Logical. If FALSE, then the function will execute stop if it comes across symbols that are not defined in symbolValues.

recursive

Logical. If TRUE, the function works recursively down a directory tree (see details).

removeExtension

Character. Matches to this regular expression are removed from filenames and directory names.

Details

Symbol substitution: this is best explained with an example. If the list symbolValues contains an element with name FOO and value bar, and symbolDelimiter is @, then any occurrence of @[email protected] is replaced by bar. This applies both the text contents of the files in src as well as to the filenames. See examples.

If recursive is FALSE, both src and dest must be connection or a filenames. The text in src is read through the function readLines, symbols are replaced by their values, and the result is written to dest through the function writeLines.

If recursive is TRUE, copySubstitute works recursively down a directory tree (see details and example). src must be a character vector with multiple filenames or directory names, dest a directory name.

One use of this function is in createPackage for the automatic generation of packages from a template package directory.

Value

None. The function is called for its side effect.

Author(s)

Wolfgang Huber http://www.dkfz.de/mga/whuber

Examples

 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
## create an example file
infile  = tempfile()
outfile = tempfile()

writeLines(text=c("We will perform in @[email protected]:",
  "So, thanks to @[email protected] at once and to each one,",
  "Whom we invite to see us crown'd at @[email protected]"),
  con = infile)

## create the symbol table
z = list(WHAT="measure, time and place", WHOM="all", WHERE="Scone")

## run copySubstitute
copySubstitute(infile, outfile, z)

## display the results
readLines(outfile)



##--------------------------------------------------------------
## This is a slightly more complicated example that demonstrates
## how copySubstitute works on nested directories
##--------------------------------------------------------------
d = tempdir()
my.dir.create = function(x) {dir.create(x); return(x)}

unlink(file.path(d, "src"), recursive=TRUE)
unlink(file.path(d, "dest"), recursive=TRUE)

## create some directories and files:
src  = my.dir.create(file.path(d, "src"))
dest = file.path(d, "dest")
d1   = my.dir.create(file.path(src, "dir1.in"))
d2   = my.dir.create(file.path(src, "[email protected]@.in"))
d3   = my.dir.create(file.path(d2, "dir3"))
d4   = my.dir.create(file.path(d3, "dir4"))
d5   = my.dir.create(file.path(d4, "[email protected]@"))
writeLines(c("File1:", "FOO: @[email protected]"),     file.path(d1, "file1.txt.in"))
writeLines(c("File2:", "BAR: @[email protected]"),     file.path(d2, "file2.txt.in"))
writeLines(c("File3:", "SUN: @[email protected]"),     file.path(d3, "file3.txt.in"))
writeLines(c("File4:", "MOON: @[email protected]"),   file.path(d4, "@[email protected]"))

## call copySubstitute
copySubstitute(src, dest, recursive=TRUE,
               symbolValues = list(FOO="thefoo", BAR="thebar",
                                   SUN="thesun", MOON="themoon"))

## view the result
listsrc  = dir(src,  full.names=TRUE, recursive=TRUE)
listdest = dir(dest, full.names=TRUE, recursive=TRUE)
listsrc
listdest

cat(unlist(lapply(listsrc,  readLines)), sep="\n")
cat(unlist(lapply(listdest, readLines)), sep="\n")

Bioconductor/Biobase documentation built on July 20, 2018, 12:34 p.m.