// globals
php
function preprocess () { 
echo ('require (foreign)\n');
	}

function calculate () {
	if (getValue ("convert_dates")) {
	       convert_dates_opt = ", convert.dates=TRUE" ;
	} else {
	       convert_dates_opt = ", convert.dates=FALSE" ;
	}

	if (getValue ("convert_factors")) {
	       convert_factors_opt = ", convert.factors=TRUE" ;
	} else {
	       convert_factors_opt = ", convert.factors=FALSE" ;
	}

	if (getValue ("missing_type")) {
	       missing_type_opt = ", missing.type=TRUE" ;
	} else {
	       missing_type_opt = ", missing.type=FALSE" ;
	}

	if (getValue ("convert_underscore")) {
	       convert_underscore_opt = ", convert.underscore=TRUE" ;
	} else {
	       convert_underscore_opt = ", convert.underscore=FALSE" ;
	}

	if (getValue ("warn_missing_labels")) {
	       warn_missing_labels_opt = ", warn.missing.labels=TRUE" ;
	} else {
	       warn_missing_labels_opt = ", warn.missing.lables=FALSE" ;
	}


	object = getValue ("saveto");

echo ('data <- read.dta ("' + getValue ("file") + '"' + convert_dates_opt + convert_factors_opt + missing_type_opt + convert_underscore_opt + warn_missing_labels + ')\n');
echo ('\n');
echo ('\n');
echo ('# set variable labels for use in RKWard\n');
echo ('labels <- attr (data, "var.labels")\n');
echo ('if (!is.null (labels)) {\n');
echo ('        for (i in 1:length (labels)) {\n');
echo ('                col <- make.names (attr (data, "names")[i] )\n');
echo ('                if (!is.null (col)) {\n');
echo ('                        rk.set.label (data[[col]], labels[i])\n');
echo ('                }\n');
echo ('        }\n');
echo ('}\n');
echo ('\n');
echo ('\n');
echo ('\n');
echo (object + ' <<- data		# assign to globalenv()\n');
	if (getValue ("doedit") ) { 
echo ('rk.edit (' + object + ')\n');
	}
}


