From jeff.hamann at forestinformatics.com  Wed Mar 17 00:58:09 2004
From: jeff.hamann at forestinformatics.com (Jeff D. Hamann)
Date: Wed Mar 17 00:58:15 2004
Subject: [Rd] best methods for strings and structures?
Message-ID: <000e01c40bb2$8b6df560$0c00a8c0@rodan>

I'm trying to generate an R interface for a library that's commonly used and
I'm currently writing wrapper functions for file i/o and return an object
(list?) that contains the elements of the C structure. For example, reading
a file that contains:

struct CONFIG_RECORD
{
    char            coeffs_filename[256];
    char            species_filename[256];
    unsigned long   use_random_error;
    unsigned long   random_seed;
    unsigned long   endemic_mortality;
    unsigned long   sdi_mortality;
    unsigned long   file_in_format;
    unsigned long   file_out_format;
    double          fixed_plot_radius;
    double          min_prism_dbh;
    double          baf;
    unsigned long   max_sample_size;
    unsigned long   use_precip_in_hg;
};

with an internal function and I'm building the return object using the
following code:


   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 ) );
   SET_VECTOR_ELT( ret_val, 3, ScalarInteger( cfg_rec.random_seed ) );
   SET_VECTOR_ELT( ret_val, 4, ScalarInteger( cfg_rec.endemic_mortality ) );
   SET_VECTOR_ELT( ret_val, 5, ScalarInteger( cfg_rec.sdi_mortality ) );
   SET_VECTOR_ELT( ret_val, 6, ScalarInteger( cfg_rec.file_in_format ) );
   SET_VECTOR_ELT( ret_val, 7, ScalarInteger( cfg_rec.file_out_format ) );
   SET_VECTOR_ELT( ret_val, 8, ScalarReal( cfg_rec.fixed_plot_radius ) );
   SET_VECTOR_ELT( ret_val, 9, ScalarReal( cfg_rec.min_prism_dbh ) );
   SET_VECTOR_ELT( ret_val, 10, ScalarReal( cfg_rec.baf ) );
   SET_VECTOR_ELT( ret_val, 11, ScalarInteger( cfg_rec.max_sample_size ) );
   SET_VECTOR_ELT( ret_val, 12, ScalarInteger( cfg_rec.use_precip_in_hg ) );

The resulting list contains :

> cfg
$"coeffs_filename"
<CHARSXP: "coeffs.txt">
$"species_filename"
<CHARSXP: "species.txt">
$"use_random_error"
[1] 1
$"random_seed"
[1] 109
$"endemic_mortality"
[1] 1
$"sdi_mortality"
[1] 1
$"file_in_format"
[1] 4
$"file_out_format"
[1] 4
$"fixed_plot_radius"
[1] 11.77522
$"min_prism_dbh"
[1] 5.6
$baf
[1] 20
$"max_sample_size"
[1] 600
$"use_precip_in_hg"
[1] 600

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.
My other question is this: Should I be using the insert() to assign these
values as attributes, like

   setAttrib( ret_val, install("use_random_error"), ScalarInteger(
cfg_rec.use_random_error ) );

since I'll be using these objects to pass back and forth from R to the model
library and using R as the simulation language in which I'll have to obtain
the values and build the resulting structures.

Thanks,
Jeff.


---
Jeff D. Hamann
Forest Informatics, Inc.
PO Box 1421
Corvallis, Oregon USA 97339-1421
541-754-1428
jeff.hamann@forestinformatics.com
www.forestinformatics.com


[Rd] best methods for strings and structures?
Jeff D. Hamann