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

From: Oleg Sklyar <osklyar_at_ebi.ac.uk>
Date: Mon, 23 Jul 2007 13:31:09 +0100

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Mon 23 Jul 2007 - 12:33:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 23 Jul 2007 - 19:36:42 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.