deparse(expr, short=F)
short=TRUE
, the deparsed character string may not be the full expression. Instead, short forms are used to keep the total length of the deparsed string within a constant limit. The short form is useful for trace printing, and for labels in situations where space is at a premium.
dput(expr)
unless
short=TRUE
, in which case only one character string is produced.
It is typically not necessary to deparse expressions to print them.
The process of coercing an expression to mode
"character"
with the
as.character
function, for example, usually achieves the same effect in a simpler way.
However, there are differences between
deparse
and
as.character
, as shown in the examples below.
The value of
deparse
is a character vector that has as many elements as there are lines in the output from
dput(expr)
.
Contrast this with
as.character
, which returns a character vector the same length as the input object.
This is most likely not what you wanted, if you started with a S-PLUS expression object.
The other extreme is that you want a single character string containing deparsed code.
To get this, use the command
paste(deparse(expr), collapse = "\n")
; you can use another value for the
collapse
argument if you do not want newlines in the string, but the string may not always parse correctly to recover
expr
.
This is a generic function; currently there are no methods defined for it.
# save a symbolic form of an argument > z$model <- deparse(model) # label a plot with the definition of a one-line function f > plot(x, f(x), ylab=deparse(f)[2], type="l") # Note the difference between parse and as.character. > foo <- function(x, y) x^y > deparse(foo) [1] "function(x, y)" "x^y" > as.character(foo) [1] "" "" "x^y" # Complimentary operations > x <- deparse(substitute(foo)) > x [1] "foo" > get(x) function(x, y) x^y