[Rd] best methods for strings and structures?
Thomas Lumley
tlumley at u.washington.edu
Wed Mar 17 01:30:21 MET 2004
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 at u.washington.edu University of Washington, Seattle
More information about the R-devel
mailing list