Bug 15201 - On windows, utf8ToInt(enc2utf8(as.character(NA))) does not translate to NA_INTEGER
On windows, utf8ToInt(enc2utf8(as.character(NA))) does not translate to NA_IN...
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Windows GUI / Window specific
R 2.15.2 patched
All Windows 64-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-02-10 12:15 UTC by Mark van der Loo
Modified: 2013-02-10 14:45 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 Mark van der Loo 2013-02-10 12:15:15 UTC
Under Windows, translating as.character(NA) to integer via enc2utf8 (as happens
in adist, for example) does not yield NA_INTEGER but (78,65) at C-level. Note that the latter are the ASCII codes for "N" and "A").

This does not happen under unix-alikes.

Below is an example showing that enc2utf8 is likely to cause the trouble. 

#include <R.h>
#include <Rdefines.h>

/* Return 1 if a[0] == NA_INTEGER, otherwise 0 */
SEXP test(SEXP a){
  PROTECT(a);
  for ( int i=0;i<length(a); ++i){
    Rprintf("input[%d]: %d\n",i,INTEGER(a)[i]);
  }
  SEXP t;
  PROTECT(t=allocVector(INTSXP,1));
  INTEGER(t)[0] = (INTEGER(a)[0]==NA_INTEGER) ? 1 : 0;
  UNPROTECT(2);
  return t;
} 


dyn.load("test.dll")

# this prints MIN_INT and returns 1, as expected
.Call("test",as.integer(NA))

# this prints '78', '65' and returns 0, *unexpectedly*
.Call("test",utf8ToInt(enc2utf8(as.character(NA))))

# this prints MIN_INT and returns 1, as expected
.Call("test",utf8ToInt(as.character(NA)))
Comment 1 Duncan Murdoch 2013-02-10 14:45:24 UTC
Fixed in R-devel and R-patched