[Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not, why?

Oleg Sklyar osklyar at ebi.ac.uk
Mon Jul 23 13:14:30 CEST 2007


Any idea why CHAR(asChar(STRING_ELT( produces NA whereas
CHAR(STRING_ELT( gets a pointer to a string? It's generally expected
that STRING_ELT should already be a character, but why the coercion does
not work? Here is a simple example (consistent over R2.5.1-R2.6 rev
42284, I didn't check earlier versions, but it used to be different in
2.4):

install.packages("inline")

library(inline)

sig <- signature(x="character")
code1 <- "
  for (int i = 0; i < LENGTH(x); i++ )
    Rprintf(\"%s\\n\", CHAR(STRING_ELT(x, i)));
  return R_NilValue;
"

code2 <- "
  for (int i = 0; i < LENGTH(x); i++ )
    Rprintf(\"%s\\n\", CHAR(asChar(STRING_ELT(x, i))));
  return R_NilValue;
"

setCMethod(c("p1","p2"), list(sig,sig), list(code1,code2))


#----------------------------------------------------------
p1(c("str1", "str2"))
# str1
# str2
# NULL

p2(c("str1", "str2"))
# NA
# NA
# NULL


-- 
Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223494466



More information about the R-devel mailing list