From simon.urbanek at r-project.org  Fri Aug 28 16:10:44 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 28 Aug 2009 10:10:44 -0400
Subject: [Rd] Clarifications please.
In-Reply-To: <fc212e850908280642j6c75f749p5961130cc5e1d509@mail.gmail.com>
References: <fc212e850908250541i16b1918h58187248193b4cbf@mail.gmail.com>
	<6phd46km2gl.fsf@gopher4.fhcrc.org>
	<fc212e850908260007w616bc806wccfe5e89d33b3466@mail.gmail.com>
	<fc212e850908260715u749f0c3dtfd8599477cb7c805@mail.gmail.com>
	<4A95690E.1070806@fhcrc.org>
	<fc212e850908280441r7c316b68se9192913e30cdc0d@mail.gmail.com>
	<E2BDA7E3-D753-4CA9-AB3B-A36755AFCEA0@r-project.org>
	<fc212e850908280642j6c75f749p5961130cc5e1d509@mail.gmail.com>
Message-ID: <BFC7DAF8-F59E-4901-B79E-3F3BF593DC73@r-project.org>


On Aug 28, 2009, at 9:42 AM, Abhijit Bera wrote:

> Hi Simon
>
> Thanks. All the confusion between CHAR and STRING. First I read this :
>
> http://www1.cuni.cz/~obo/r_surprises.html
>

Uh, oh, that doc confuses things even more and is not really correct  
(well, the author admits that he has no idea, but that doesn't help).  
Ok, so a quick clarification of the world of strings and character  
vectors.

In R speak string vector is a vector containing 0 or more character  
strings (e.g. c("foo","bar")). It is sometimes also called character  
vector, because it is of type and mode "character". In the R C-level  
API string vectors are represented by the type STRSXP and you can use  
LENGTH(), STRING_ELT(x,i) and SET_STRING_ELT(x,i,a) macros on it to  
get the length and get/set elements. The elements are character  
strings which are represented by CHARSXP (this is not a vector in R  
land) and are always read-only.  They can be created using mkChar*()  
functions and the C-level const char* content (\0-terminated for now)  
can be retrieved using CHAR(x).

The author of the above page is confused about mkString - which  
creates a string vector (STRSXP) and mkChar which creates a character  
string (CHARSXP). Essentially mkString(x) is just a shortcut for  
v=allocVector(STRSXP, 1); SET_STRING_ELT(v, 0, mkChar(x)); x; Users in  
R never see CHARSXP directly - it is never used when interfacing  
another function since there are conceptually no scalars in the R  
language.

I hope this helps to sort out the confusion.

Cheers,
Simon


> In the meantime I got your reply. Then I read the documentation and  
> it made sense!
>
> Regards
>
> Abhijit Bera
>
> [..snip, previously posed code omitted ..]



[Rd] Clarifications please.
Simon Urbanek