evalOrEcho: Evaluate an Argument If Possible

Description Usage Arguments Details Value See Also

View source: R/evalOrEcho.R


This function parses, evaluates and returns the string given as its first argument. If it can't, the argument itself is returned. Use of evalOrEcho to process arguments inside a function can make for more flexible code.


evalOrEcho(x, resultMode = NULL, n = 0)



a string or other object to attempt to parse and evaluate.


a string or NULL. If non-NULL, the evaluation of x is considered to have failed if the resulting object is not of this mode.


parent generations to go back. The evaluation is attempted in the enviroment specified by parent.frame(n) (of the caller).


Using this function inside another function to process some of its arguments can be very useful. For example, tisPlot has a number or arguments that specify text labels for headers, subheaders, footnotes, axis labels, and so on. One of those arguments is sub, which specifies the subheader. By doing this:

sub <- evalOrEcho(sub, resultMode = "character")

tisPlot can handle the sub argument given in any of these forms:

  1. sub = "This is a simple subtitle".

  2. sub = c("this is a two", "line subtitle").

  3. sub = 'c("this is another", "two line subtitle")'.


If x is successfully parsed and evaluated, and its mode matches resultMode (if supplied), the resulting object is returned. Otherwise, x itself is returned.

See Also


tis documentation built on Sept. 29, 2021, 1:06 a.m.