[Rd] bug in format.default: trim=TRUE does not always work as advertised (PR#9114)

tplate at acm.org tplate at acm.org
Mon Jul 31 17:52:10 CEST 2006

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit


Output from format.default sometimes has whitespace around it when using
big.mark="," and trim=TRUE.  E.g.:

  > # works ok as long as big.mark is not actually used:
  > format(c(-1,1,10,999), big.mark=",", trim=TRUE)
[1] "-1"  "1"   "10"  "999"
  > # but if big.mark is used, output is justified and not trimmed:
  > format(c(-1,1,10,999,1e6), big.mark=",", trim=TRUE)
[1] "       -1" "        1" "       10" "      999" "1,000,000"

The documentation for the argument 'trim' to format.default() states:
     trim: logical; if 'FALSE', logical, numeric and complex values are
           right-justified to a common width: if 'TRUE' the leading
           blanks for justification are suppressed.

Thus, the above behavior of including blanks for justification when 
trim=FALSE (in some situations) seems to contradict the documentation.


The last call to prettyNum() in format.default() 
(src/library/base/R/format.R) has the argument

    preserve.width = "common"

If this is changed to

    preserve.width = if (trim) "individual" else "common"

then output is formatted correctly in the case above.

A patch for this one line is attached to this message (patch done 
against the released R-2.3.1 source tarball (2006/06/01), the format.R 
file in this release is not different to the one in the current snapshot 
of the devel version of R).  After making these changes, I ran "make 
check-all".  I did not see any tests that seemed to  break with these 

-- Tony Plate

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

--- R-2.3.1-orig/src/library/base/R/format.R	2006-04-09 16:19:19.000000000 -0600
+++ R-2.3.1/src/library/base/R/format.R	2006-07-26 15:52:42.117456700 -0600
@@ -37,7 +37,7 @@
 			 small.interval = small.interval,
 			 decimal.mark = decimal.mark,
 			 zero.print = zero.print,
-			 preserve.width = "common")
+                         preserve.width = if (trim) "individual" else "common")


More information about the R-devel mailing list