From tlumley at u.washington.edu  Wed Mar 17 01:30:21 2004
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Wed Mar 17 01:30:26 2004
Subject: [Rd] best methods for strings and structures?
In-Reply-To: <000e01c40bb2$8b6df560$0c00a8c0@rodan>
References: <000e01c40bb2$8b6df560$0c00a8c0@rodan>
Message-ID: <Pine.A41.4.58.0403161624260.52078@homer39.u.washington.edu>

On Tue, 16 Mar 2004, Jeff D. Hamann wrote:
>
>    PROTECT( ret_val = allocVector( VECSXP, 13 ) );
>    SET_STRING_ELT( ret_val, 0, mkChar( cfg_rec.coeffs_filename) );
>    SET_STRING_ELT( ret_val, 1, mkChar( cfg_rec.species_filename) );
>    SET_VECTOR_ELT( ret_val, 2, ScalarInteger( cfg_rec.use_random_error ) );
<snip>

> The resulting list contains :
>
> > cfg
> $"coeffs_filename"
> <CHARSXP: "coeffs.txt">
> $"species_filename"
> <CHARSXP: "species.txt">
> $"use_random_error"
> [1] 1
<snip>

> which seems to be acceptable, except for the two string at the beginning of
> the list. I'm not sure the best method to return string in the return vector
> and would like some advice on the best method to perform these operations.

You need the first two elements of the list to be vectors of character
objects, not character objects (just as the third element is a vector of
integers rather than a single integer.
Something like

PROTECT(tempstr=allocVector(STRSXP,1));
SET_STRING_ELT(tempstr,0, mkChar( cfg_rec.coeffs_filename));
SET_VECTOR_ELT(ret_val,0, tempstr);
UNPROTECT(1);  /* tempstr */

(there may be more elegant versions of this).


	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle


[Rd] best methods for strings and structures?
Thomas Lumley