From osklyar at ebi.ac.uk  Mon Jul 23 14:31:09 2007
From: osklyar at ebi.ac.uk (Oleg Sklyar)
Date: Mon, 23 Jul 2007 13:31:09 +0100
Subject: [Rd] CHAR(STRING_ELT( - OK but CHAR(asChar(STRING_ELT( - not,
	why?
In-Reply-To: <Pine.LNX.4.64.0707231254420.30373@gannet.stats.ox.ac.uk>
References: <1185189270.6294.69.camel@dirac>
	<Pine.LNX.4.64.0707231254420.30373@gannet.stats.ox.ac.uk>
Message-ID: <1185193869.6294.77.camel@dirac>

Thank you Prof. Ripley. Funny enough after digging in the R sources I
was just writing a reply to my own question (in case someone else would
find it interesting) and it was approximately the same as what you
write. It just slipped from my attention initially.

O.


On Mon, 2007-07-23 at 13:22 +0100, Prof Brian Ripley wrote:
> 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
> >
> >
> >
> 
-- 
Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, UK * +441223494466



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