Skip to content

Commit

Permalink
deparse() etc: cannot use "nice names" when some are NA_character_;
Browse files Browse the repository at this point in the history
 more logical: also for named list()s

git-svn-id: https://svn.r-project.org/R/trunk@73699 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Nov 10, 2017
1 parent a08a602 commit 62fced0
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 172 deletions.
14 changes: 8 additions & 6 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@
of its previous imperfect workaround R code).
\code{dput()}, \emph{etc} now print the \code{names()} information
only once, using the more readable \code{(tag = value)} syntax.
These functions gain a new (partly experimental) control option
\emph{via} \code{.deparseOpts()}, \code{"niceNames"}, which when
set (e.g.\sspace{}as part of \code{"all"}) also use the \code{(tag
= value)} syntax for atomic vectors.
only once, using the more readable \code{(tag = value)} syntax,
notably for \code{list()}s, i.e., including data frames.
These functions gain a new control option \code{"niceNames"} (see
\code{.deparseOpts()}), which when set (as by default) also uses
the \code{(tag = value)} syntax for atomic vectors. OTOH, without
deparse options \code{"showAttributes"} and \code{"niceNames"},
names are no longer shown also for lists.
\code{m:n} now also deparses nicely when \eqn{m > n}.
Expand Down
2 changes: 1 addition & 1 deletion src/include/Defn.h
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ void R_SetVarLocValue(R_varloc_t, SEXP);
#define NICE_NAMES 1024
/* common combinations of the above */
#define SIMPLEDEPARSE 0
#define DEFAULTDEPARSE 65 /* KEEPINTEGER | KEEPNA, used for calls */
#define DEFAULTDEPARSE 1089 /* KEEPINTEGER | KEEPNA | NICE_NAMES, used for calls */
#define FORSOURCING 95 /* not DELAYPROMISES, used in edit.c */

/* Coercion functions */
Expand Down
2 changes: 1 addition & 1 deletion src/library/base/R/New-Internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ rbind <- function(..., deparse.level = 1)
deparse <-
function(expr, width.cutoff = 60L,
backtick = mode(expr) %in% c("call", "expression", "(", "function"),
control = c("keepInteger", "showAttributes", "keepNA"),
control = c("keepNA", "keepInteger", "niceNames", "showAttributes"),
nlines = -1L)
.Internal(deparse(expr, width.cutoff, backtick,
.deparseOpts(control), nlines))
Expand Down
4 changes: 2 additions & 2 deletions src/library/base/R/dput.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# https://www.R-project.org/Licenses/

dput <-
function(x, file = "",
control = c("keepNA", "keepInteger", "showAttributes"))
function(x, file = "", ## keep in sync with deparse() ./New-Internal.R :
control = c("keepNA", "keepInteger", "niceNames", "showAttributes"))
{
if(is.character(file))
if(nzchar(file)) {
Expand Down
5 changes: 2 additions & 3 deletions src/library/base/man/deparse.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
}
\usage{
deparse(expr, width.cutoff = 60L,
backtick = mode(expr) \%in\%
c("call", "expression", "(", "function"),
control = c("keepInteger", "showAttributes", "keepNA"),
backtick = mode(expr) \%in\% c("call", "expression", "(", "function"),
control = c("keepNA", "keepInteger", "niceNames", "showAttributes"),
nlines = -1L)
}
\arguments{
Expand Down
12 changes: 9 additions & 3 deletions src/library/base/man/dput.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% File src/library/base/man/dput.Rd
% Part of the R package, https://www.R-project.org
% Copyright 1995-2014 R Core Team
% Copyright 1995-2017 R Core Team
% Distributed under GPL 2 or later

\name{dput}
Expand All @@ -13,7 +13,7 @@
}
\usage{
dput(x, file = "",
control = c("keepNA", "keepInteger", "showAttributes"))
control = c("keepNA", "keepInteger", "niceNames", "showAttributes"))

dget(file, keep.source = FALSE)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ dget(file, keep.source = FALSE)
designed to be used for transporting \R data, and will work with \R
objects that \code{dput} does not handle correctly as well as being
much faster.

To avoid the risk of a source attribute out of sync with the actual
function definition, the source attribute of a function will never
be written as an attribute.
Expand Down Expand Up @@ -98,6 +98,12 @@ dput(xx, "foo", control = "digits17")
dget("foo") - xx # slight rounding on some platforms
dput(xx, "foo", control = "hexNumeric"); dget("foo") - xx
unlink("foo")

xn <- setNames(xx, paste0("pi^",1:3))
dput(xn) # nicer, now "niceNames" being part of default 'control'
dput(xn, control = "S_compat") # no names
## explicitly asking for output as in R < 3.5.0:
dput(xn, control = c("keepNA", "keepInteger", "showAttributes"))
}
\keyword{file}
\keyword{programming}
Expand Down
2 changes: 1 addition & 1 deletion src/library/utils/R/str.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ str.default <-
has.class <- S4 || !is.null(cl) # S3 or S4
mod <- ""; char.like <- FALSE
if(give.attr) a <- attributes(object)#-- save for later...
dCtrl <- eval(formals(deparse)$control)
dCtrl <- unique(c(eval(formals(deparse)$control), "niceNames"))
if(drop.deparse.attr) dCtrl <- dCtrl[dCtrl != "showAttributes"]
deParse <- function(.) deparse(., width.cutoff = min(500L, max(20L, width-10L)),
control = dCtrl)
Expand Down
Loading

0 comments on commit 62fced0

Please sign in to comment.