metaSub.character: Create Replicate Text files with Systematic Substitutions

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

metaSub is generic. A method is defined for character; a convenience wrapper is provided for passing names of text files to be read and then resampled.

metaSub collapses a character vector to one line of text. The vector is replicated as many times as there are elements in names, with flexible substitution of text fragments. If out is supplied, the replicates are saved as text files identified with names and a suffix.

metaSub.filename will process multiple filenames, if x is as long as names.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
	## S3 method for class 'character'
as.filename(x, ...)
	## S3 method for class 'filename'
metaSub(x, names, pattern=NULL, replacement=NULL, ...)
	## S3 method for class 'character'
metaSub(
		x, 
		names, 
		pattern = NULL,
		replacement = NULL, 
		out = NULL, 
		suffix = '.txt', 
		fixed = TRUE, 
		...
	)

Arguments

x

scalar character, or (second form) filename(s). Multi-element character will be collapsed to one element, with newline as the separator.

names

a list of (unique) names for resulting output elements. A vector of names will be coerced to character and to list.

pattern

a character vector of text fragments to replace, optionally encoded as regular expressions (fixed==FALSE, See ?gsub, ?regex). Can also be a list. See details.

replacement

A character vector of substitutions for patterns. The wildcard ‘*’ is available to represent the corresponding value of names. Can also be a list with as many elements as pattern (see details). Expressions are supported (see details).

out

(path and) directory in which to write the resulting control streams

suffix

file extension for filenames, if out is supplied

fixed

passed to gsub: use FALSE if pattern contains regular expressions. Scalar, or same length as pattern.

...

extra arguments, available to expressions or passed to gsub

Details

Typical usages are

1
2
3

Replacement is performed by gsub, so an element of pattern will be replaced everywhere it occurs in a line.

if pattern or replacement is a list, each element should be of length one, or as long as names. In the latter case, substitutions can be specific on a per-name basis. The wild card ‘*’ is still available.

It is necessary that pattern and replacement be of the same length, but it is not necessary that their corresponding elements have equal lengths. Thus, one can specify name-specific replacements for a single pattern, or a single replacement for name-specific patterns.

An expression can be specified for replacement itself, or one of its pattern-wise elements, or one of the name-wise elements of a pattern-wise element. Expressions are evaluated in an environment containing “name” (same meaning as ‘*’ above) and all other ... arguments. This is useful if extra arguments have a dimension indexed, at least loosely, by names. The evaluated expression is treated as character, and wildcard substitution is attempted. Use \* for a literal asterisk: in R: \\*.

NOTE: be very careful not to have trailing commas in your lists! An error results that is very hard to track. e.g. c(a,b,c,).

Value

an invisible named character vector. If out is supplied, elements are written as files with corresponding names.

Author(s)

Tim Bergsma

References

http://metrumrg.googlecode.com

See Also

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
ctl <- readLines(system.file('example/misc/8.ctl',package='metrumrg'))
dir()
e <- metaSub(
	ctl,
	names=1:3,
	pattern=c(
		'PROBLEM 8',
		'data8.csv',
		'8.MSF'
	),
	replacement=c(
		'PROBLEM *',
		'*.csv',
		'*.MSF'
	),
	out='.',
	suffix='.ctl'
)
t <- metaSub(
	ctl,
	names=c('test1','test2'),
	pattern=c('PROBLEM 8','data8.csv','METH=0'),
	replacement=c('PROBLEM *','*.csv','METH=1'),
)

t <- metaSub(
	ctl,
	names=c('test1','test2'),
	pattern=c(
		'PROBLEM 8',
		'data8.csv',
		'METH=0'
	),
	replacement=list(
		'PROBLEM *',
		'*.csv',
		c('METH=1','METH=2')
	),
	out='.',
	suffix='.ctl'
)
#just a file copy...
metaSub(as.filename('1.ctl'),names='4',out='.',suffix='.ctl')

#using a (nonsense) replacement expression...
options <- data.frame(var=c(8,9),alt=c(10,11))
a <- metaSub(
	ctl,
	names=rownames(options),
	pattern='9999', 
	replacement=expression(
		options$var[rownames(options)==name]
	),
	options=options
)
cat(a[[2]])

#replacement expression in a 'mixed' list...
b <- metaSub(
	ctl,
	names=rownames(options),
	pattern=list(
		'PRINT=2',
		'9999'
	), 
	replacement=list(
		'PRINT=3',
		expression(options$var[rownames(options)==name])
	),
	options=options
)
cat(b[[2]])

#replacement expressions on a per-name basis
d <- metaSub(
	ctl,
	names=rownames(options),
	pattern='9999',
	replacement=list(	#so that replacement is as long as pattern
		list(			#with different options for each 'name'
			expression(options$var[rownames(options)==name]),
			expression(options$alt[rownames(options)==name])
		)
	),
	options=options
)
cat(d[[2]])

metrumrg documentation built on May 2, 2019, 5:55 p.m.