Bug 15247 - str.default fails if object names contain invalid bytes
str.default fails if object names contain invalid bytes
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Misc
R 2.15.3
All Linux
: P5 minor
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-03-27 10:03 UTC by Adrian Baddeley
Modified: 2013-04-18 10:16 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Adrian Baddeley 2013-03-27 10:03:42 UTC
(1) Demonstration of problem in R.2.15.3 

  x <- data.frame(1:10, 1:10)
  names(x) <- c("A", "N\xba")
  str(x)

Yields the error message

 Error in format.default(nam.ob, width = max.ncnam, justify = "left") : 
  invalid 'width' argument

(2) Diagnosis
    Error occurs in utils:::str.default 
    in the following lines

        max.ncnam <- max(nchar(nam.ob, type = "w"))
        format(nam.ob, width = max.ncnam, justify = "left")

    In the example above, nam.obj <- names(x) contains invalid bytes,
    so nchar(nam.obj, type="w") returns a vector that contains NA values,
    and the maximum of the vector is NA. Then format.default complains 
    that NA is not a valid width.

 (3) Solution
     In str.default, in the call to max() set na.rm=TRUE
     Alternatively, replace the invalid bytes in nam.obj
Comment 1 Martin Maechler 2013-04-04 15:23:51 UTC
(In reply to comment #0)

>  (3) Solution
>      In str.default, in the call to max() set na.rm=TRUE
>      Alternatively, replace the invalid bytes in nam.obj

Thank you Adrian.

I've decided to use a slightly more satisfying solution, based on
format.info(), in the case where  nchar() contains NA.

This will be in  "R-3.0.0 patched" and later.
Martin