Oleg Sklyar
2007-Jul-23 11:14 UTC
[Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not, why?
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
Prof Brian Ripley
2007-Jul-23 12:22 UTC
[Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not, why?
I think you are asking why calling asChar on a CHARSXP gives NA_STRING. In particular, the calls you mention *are* perfectly OK and work as intended. As barely documented in R-exts, asChar is designed for vector arguments: a CHARSXP is not a vector. It gives NA_STRING for invalid inputs. The asXXXX family of functions are designed to coerce as necessary user inputs, and CHARSXPs are not visible at R level. In general, with internal functions you are expected to read the code to find out what they do before using them. On Mon, 23 Jul 2007, Oleg Sklyar wrote:> 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,It is required to be a CHARSXP, but 'character' usually refers to STRSXP.> 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):There is no R '2.4', but the behaviour of asChar was the same in R 2.4.0 except for the adding of SYMSXP at ------------- r40358 | maechler | 2007-01-04 11:07:04 +0000 (Thu, 04 Jan 2007) | 1 line eliminate CHAR_STAR in methods/src/ as per old "todo" ------------- which message does not help me at all. Perhaps Martin can explain?> 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 > > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595