R Quotes In String
Lets create a sample string a string' A reminder before we proceed: Strings in R imply a character vector. If you recall the lowest datastructure in R is a vector. So a String is a character vector of length 1. You can do this: a1 1 'This is a sample string' the print function can be used to print the argument. Like strings, regexps use the backslash, to escape special behaviour. So to match an., you need the regexp. Unfortunately this creates a problem. We use strings to represent regular expressions, and is also used as an escape symbol in strings. So to create the regular expression. We need the string '.'
substitute {base} | R Documentation |
Substituting and Quoting Expressions
Description
substitute
returns the parse tree for the (unevaluated)expression expr
, substituting any variables bound inenv
.
quote
simply returns its argument. The argument is not evaluatedand can be any R expression.
enquote
is a simple one-line utility which transforms a call ofthe form Foo(....)
into the call quote(Foo(....))
. Thisis typically used to protect a call
from early evaluation.
The print method (print.noquote) prints character strings without quotes (' dots'). If right is specified in a call print(x, right=.), it takes precedence over a possible right setting of x, e.g., created by x. String is surrounded in double quotes, which suppresses all special meanings of metacharacters except dollar, backquote and backslash, so these (and of course double quote) are preceded by backslash. This type of quoting is also appropriate for bash, kshand.
Usage
Arguments
expr | any syntactically valid R expression |
cl | a |
env | an environment or a list object. Defaults to thecurrent evaluation environment. |
Details
The typical use of substitute
is to create informative labelsfor data sets and plots.The myplot
example below shows a simple use of this facility.It uses the functions deparse
and substitute
to create labels for a plot which are character string versionsof the actual arguments to the function myplot
.
Substitution takes place by examining each component of the parse treeas follows: If it is not a bound symbol in env
, it isunchanged. If it is a promise object, i.e., a formal argument to afunction or explicitly created using delayedAssign()
,the expression slot of the promise replaces the symbol. If it is anordinary variable, its value is substituted, unless env
is.GlobalEnv
in which case the symbol is left unchanged.
Both quote
and substitute
are ‘special’primitive functions which do not evaluate their arguments.
Value
The mode
of the result is generally 'call'
butmay in principle be any type. In particular, single-variableexpressions have mode 'name'
and constants have theappropriate base mode.
Note
substitute
works on a purely lexical basis. There is noguarantee that the resulting expression makes any sense.
Substituting and quoting often cause confusion when the argument isexpression(...)
. The result is a call to theexpression
constructor function and needs to be evaluatedwith eval
to give the actual expression object.
R Quotes In String Quartets
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
See Also
missing
for argument ‘missingness’,bquote
for partial substitution,sQuote
and dQuote
for adding quotationmarks to strings,
R Replace Quotes In String
all.names
to retrieve the symbol names from an expressionor call.