deparseOpts {base} | R Documentation |
Options for Expression Deparsing
Description
Process the deparsing options for deparse
, dput
and
dump
.
Usage
.deparseOpts(control)
..deparseOpts
Arguments
control |
character vector of deparsing options. |
Details
..deparseOpts
is the character
vector of possible
deparsing options used by .deparseOpts()
.
.deparseOpts()
is called by deparse
, dput
and
dump
to process their control
argument.
The control
argument is a vector containing zero or more of the
following strings (exactly those in ..deparseOpts
). Partial
string matching is used.
"keepInteger"
:-
Either surround integer vectors by
as.integer()
or use suffixL
, so they are not converted to type double when parsed. This includes making sure that integerNA
s are preserved (viaNA_integer_
if there are no non-NA
values in the vector, unless"S_compatible"
is set). "quoteExpressions"
:-
Surround unevaluated expressions, but not
formula
s, withquote()
, so they are not evaluated when re-parsed. "showAttributes"
:-
If the object has
attributes
(other than asource
attribute, seesrcref
), usestructure()
to display them as well as the object value unless the only such attribute isnames
and the"niceNames"
option is set. This ("showAttributes"
) is the default fordeparse
anddput
. "useSource"
:-
If the object has a
source
attribute (srcref
), display that instead of deparsing the object. Currently only applies to function definitions. "warnIncomplete"
:-
Some exotic objects such as environments, external pointers, etc. can not be deparsed properly. This option causes a warning to be issued if the \Ideparser recognizes one of these situations.
Also, the parser in R < 2.7.0 would only accept strings of up to 8192 bytes, and this option gives a warning for longer strings.
"keepNA"
:-
Integer, real and character
NA
s are surrounded by coercion functions where necessary to ensure that they are parsed to the same type. Since e.g.NA_real_
can be output in R, this is mainly used in connection withS_compatible
. "niceNames"
:-
If true,
list
s and atomic vectors with non-NA
names (seenames
) are deparsed as e.g.,c(A = 1)
instead ofstructure(1, names = "A")
, independently of the"showAttributes"
setting. "all"
:-
An abbreviated way to specify all of the options listed above plus
"digits17"
. This is the default fordump
, and, without"digits17"
, the options used byedit
(which are fixed). "delayPromises"
:-
Deparse promises in the form <promise: expression> rather than evaluating them. The value and the environment of the promise will not be shown and the deparsed code cannot be sourced.
"S_compatible"
:-
Make deparsing as far as possible compatible with S and R < 2.5.0. For compatibility with S, integer values of double vectors are deparsed with a trailing decimal point. Backticks are not used.
"hexNumeric"
:-
Real and finite complex numbers are output in ‘"%a"’ format as binary fractions (coded as hexadecimal: see
sprintf
) with maximal opportunity to be recorded exactly to full precision. Complex numbers with one or both non-finite components are output as if this option were not set.(This relies on that format being correctly supported: known problems on Windows are worked around as from R 3.1.2.)
"digits17"
:-
Real and finite complex numbers are output using format ‘"%.17g"’ which may give more precision than the default (but the output will depend on the platform and there may be loss of precision when read back). Complex numbers with one or both non-finite components are output as if this option were not set.
"exact"
:-
An abbreviated way to specify
control = c("all", "hexNumeric")
which is guaranteed to be exact for numbers, see also below.
For the most readable (but perhaps incomplete) display, use
control = NULL
. This displays the object's value, but not its
attributes. The default in deparse
is to display the
attributes as well, but not to use any of the other options to make
the result parseable. (dump
uses more default options via
control = "all"
, and printing of functions without sources
uses c("keepInteger", "keepNA")
to which one may add
"warnIncomplete"
.)
Using control = "exact"
(short for control = c("all", "hexNumeric")
)
comes closest to making deparse()
an inverse of parse()
(but we have not yet seen an example where "all"
, now including
"digits17"
, would not have been as good). However, not all
objects are deparse-able even with these options, and a warning will be
issued if the function recognizes that it is being asked to do the
impossible.
Only one of "hexNumeric"
and "digits17"
can be specified.
Value
An integer value corresponding to the control
options
selected.
Examples
stopifnot(.deparseOpts("exact") == .deparseOpts(c("all", "hexNumeric")))
(iOpt.all <- .deparseOpts("all")) # a four digit integer
## one integer --> vector binary bits
int2bits <- function(x, base = 2L,
ndigits = 1 + floor(1e-9 + log(max(x,1), base))) {
r <- numeric(ndigits)
for (i in ndigits:1) {
r[i] <- x%%base
if (i > 1L)
x <- x%/%base
}
rev(r) # smallest bit at left
}
int2bits(iOpt.all)
## What options does "all" contain ? =========
(depO.indiv <- setdiff(..deparseOpts, c("all", "exact")))
(oa <- depO.indiv[int2bits(iOpt.all) == 1])# 8 strings
stopifnot(identical(iOpt.all, .deparseOpts(oa)))
## ditto for "exact" instead of "all":
(iOpt.X <- .deparseOpts("exact"))
data.frame(opts = depO.indiv,
all = int2bits(iOpt.all),
exact= int2bits(iOpt.X))
(oX <- depO.indiv[int2bits(iOpt.X) == 1]) # 8 strings, too
diffXall <- oa != oX
stopifnot(identical(iOpt.X, .deparseOpts(oX)),
identical(oX[diffXall], "hexNumeric"),
identical(oa[diffXall], "digits17"))