[Rd] Bug in prettyNum
Martin Maechler
m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon May 26 18:27:57 CEST 2025
Thank you, Marttila and Ivan,
As the original author of prettyNum() {etc ..},
I will commit such a bug fix to R-devel (and probably port it to
R 4.5.0 patched) quite soon
(but not yet today).
Best regards,
Martin Maechler
>>>>> Ivan Krylov via R-devel
>>>>> on Fri, 23 May 2025 17:14:57 +0300 writes:
> В Fri, 23 May 2025 11:47:33 +0000
> Marttila Mikko via R-devel <r-devel using r-project.org> пишет:
>> When called with a numeric vector, the `replace.zero` argument is
>> disregarded.
>>
>> > prettyNum(0, zero.print = "- ", replace.zero = TRUE)
>> [1] "-"
>> Warning message:
>> In .format.zeros(x, zero.print, replace = replace.zero) :
>> 'zero.print' is truncated to fit into formatted zeros; consider
>> 'replace=TRUE'
>> Please see below a patch which I believe would fix this.
> Surprisingly, it's not enough. The 'replace' argument to .format.zeros
> needs to be "threaded" through both the call to vapply(x, format, ...)
> and the internal call from format.default(...) to prettyNum(...):
R> options(warn = 2, error = recover)
R> prettyNum(0, zero.print = "--", replace.zero = TRUE)
> Error in .format.zeros(x, zero.print, replace = replace.zero) :
> (converted from warning) 'zero.print' is truncated to fit into formatted zeros; consider 'replace=TRUE'
> Enter a frame number, or 0 to exit
> 1: prettyNum(0, zero.print = "--", replace.zero = TRUE)
> 2: vapply(x, format, "", big.mark = big.mark, big.interval = big.interval, sma
> 3: FUN(X[[i]], ...)
> 4: format.default(X[[i]], ...)
> 5: prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3, na.encode, sc
> 6: .format.zeros(x, zero.print, replace = replace.zero)
> 7: warning("'zero.print' is truncated to fit into formatted zeros; consider 'r
> <...omitted...>
> Selection: 6
> <...>
> Browse[1]> ls.str()
> i0 : logi TRUE
> ind0 : int 1
> nc : int 1
> nx : num 0
> nz : int 2
> replace : logi FALSE
> warn.non.fitting : logi TRUE
> x : chr "0"
> zero.print : chr "--"
> Since prettyNum() accepts ... and thus ignores unknown arguments, it
> seems to be safe to forward the ellipsis from format.default() to
> prettyNum(). The patch survives LANGUAGE=en TZ=UTC make check-devel.
> Index: src/library/base/R/format.R
> ===================================================================
> --- src/library/base/R/format.R (revision 88229)
> +++ src/library/base/R/format.R (working copy)
> @@ -73,7 +73,7 @@
> decimal.mark = decimal.mark, input.d.mark = decimal.mark,
> zero.print = zero.print, drop0trailing = drop0trailing,
> is.cmplx = is.complex(x),
> - preserve.width = if (trim) "individual" else "common"),
> + preserve.width = if (trim) "individual" else "common", ...),
> ## all others (for now):
> stop(gettextf("Found no format() method for class \"%s\"",
> class(x)), domain = NA))
> @@ -338,7 +338,8 @@
> big.mark=big.mark, big.interval=big.interval,
> small.mark=small.mark, small.interval=small.interval,
> decimal.mark=decimal.mark, zero.print=zero.print,
> - drop0trailing=drop0trailing, ...)
> + drop0trailing=drop0trailing, replace.zero=replace.zero,
> + ...)
> }
> ## be fast in trivial case, when all options have their default, or "match"
> nMark <- big.mark == "" && small.mark == "" && (notChar || decimal.mark == input.d.mark)
> --
> Best regards,
> Ivan
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list