[rkward-cvs] SF.net SVN: rkward:[2652] trunk/rkward/rkward/plugins/00saveload/import

sjar at users.sourceforge.net sjar at users.sourceforge.net
Tue Sep 22 22:45:09 UTC 2009


Revision: 2652
          http://rkward.svn.sourceforge.net/rkward/?rev=2652&view=rev
Author:   sjar
Date:     2009-09-22 22:45:09 +0000 (Tue, 22 Sep 2009)

Log Message:
-----------
new plugins for Stata import by Michael Ash, need review and testing

Added Paths:
-----------
    trunk/rkward/rkward/plugins/00saveload/import/import_stata.php
    trunk/rkward/rkward/plugins/00saveload/import/import_stata.rkh
    trunk/rkward/rkward/plugins/00saveload/import/import_stata.xml

Added: trunk/rkward/rkward/plugins/00saveload/import/import_stata.php
===================================================================
--- trunk/rkward/rkward/plugins/00saveload/import/import_stata.php	                        (rev 0)
+++ trunk/rkward/rkward/plugins/00saveload/import/import_stata.php	2009-09-22 22:45:09 UTC (rev 2652)
@@ -0,0 +1,89 @@
+<?php
+function preprocess () { ?>
+require (foreign)
+<?	if (getRK_val ("do_locale_conversion")) { ?>
+
+# helper function to convert all strings to the current encoding
+iconv.recursive <- function (x, from) {
+	attribs <- attributes (x);
+	if (is.character (x)) {
+		x <- iconv (x, from=from, to="", sub="")
+	} else if (is.list (x)) {
+		x <- lapply (x, function (sub) iconv.recursive (sub, from))
+	}
+	# convert factor levels and all other attributes
+	attributes (x) <- lapply (attribs, function (sub) iconv.recursive (sub, from))
+	x
+}
+<?	}
+}
+
+function calculate () {
+
+	if (getRK_val ("convert_dates")) {
+	       $convert_dates_opt = ", convert.dates=TRUE" ;
+	} else {
+	       $convert_dates_opt = ", convert.dates=FALSE" ;
+	}
+
+	if (getRK_val ("convert_factors")) {
+	       $convert_factors_opt = ", convert.factors=TRUE" ;
+	} else {
+	       $convert_factors_opt = ", convert.factors=FALSE" ;
+	}
+
+	if (getRK_val ("missing_type")) {
+	       $missing_type_opt = ", missing.type=TRUE" ;
+	} else {
+	       $missing_type_opt = ", missing.type=FALSE" ;
+	}
+
+	if (getRK_val ("convert_underscore")) {
+	       $convert_underscore_opt = ", convert.underscore=TRUE" ;
+	} else {
+	       $convert_underscore_opt = ", convert.underscore=FALSE" ;
+	}
+
+	if (getRK_val ("warn_missing_labels")) {
+	       $warn_missing_labels_opt = ", warn.missing.labels=TRUE" ;
+	} else {
+	       $warn_missing_labels_opt = ", warn.missing.lables=FALSE" ;
+	}
+
+
+	$object = getRK_val ("saveto");
+?>
+data <- read.dta ("<? getRK ("file"); ?>"<? echo ($convert_dates_opt); echo ($convert_factors_opt); echo ($missing_type_opt); echo ($convert_underscore_opt); echo ($warn_missing_labels);  ?>)
+<?	if (getRK_val ("do_locale_conversion")) {
+		$from_locale = getRK_val ("encoding");
+		if ($from_locale == "other") {
+			$from_locale = getRK_val ("user_encoding");
+		} ?>
+
+# convert all strings to the current encoding
+data <- iconv.recursive (data, from="<? echo ($from_locale); ?>")
+<?	}
+	if (getRK_val ("convert_var_labels")) { ?>
+
+# set variable labels for use in RKWard
+labels <- attr (data, "variable.labels");
+if (!is.null (labels)) {
+	for (i in 1:length (labels)) {
+		col <- make.names (names (labels[i]))
+		if (!is.null (col)) {
+			rk.set.label (data[[col]], labels[i])
+		}
+	}
+}
+<?	} ?>
+
+<? echo ($object); ?> <<- data		# assign to globalenv()
+<?
+	if (getRK_val ("doedit") ) { ?>
+rk.edit (<? echo ($object); ?>)
+<?	}
+}
+
+function printout () {
+}
+?>

Added: trunk/rkward/rkward/plugins/00saveload/import/import_stata.rkh
===================================================================
--- trunk/rkward/rkward/plugins/00saveload/import/import_stata.rkh	                        (rev 0)
+++ trunk/rkward/rkward/plugins/00saveload/import/import_stata.rkh	2009-09-22 22:45:09 UTC (rev 2652)
@@ -0,0 +1,35 @@
+<!DOCTYPE rkhelp>
+<document>
+	<summary>
+Import STATA data files.
+	</summary>
+
+	<usage>
+Choose the STATA data file to import. Usually those files have the ending '*.dta. An R object containing the STATA data will be created. This can either be a data.frame, or a list. Several options deal with handling labels and character encoding. See <link href="rkward://rhelp/read.dta" />. Other STATA files than data files (i. e.:*.log, *.smcl, *.do, *.ado) are not supported. 
+	</usage>
+
+	<settings>
+		<caption id="tab_general"/>
+		<setting id="file">The filename of the file to import</setting>
+		<setting id="saveto">The name of an R object to store the imported data in. If you chose an existing symbol name, you will be prompted, whether to overwrite this object.</setting>
+		<setting id="doedit">Whether the object should be opened for editing after it was imported. Currently, this is only possible, if the object is saved as a data.frame (see next option).</setting>
+		<setting id="data_frame">Whether to save the data as a data.frame. The alternative is saving as a list (useful, if the columns in your STATA data have different lengths)</setting>
+
+		<setting id="convert_var_labels">RKWard can display STATA like variable labels. Check this option, if you would like to convert the STATA variable labels to RKWard format. If you leave the option unchecked, the STATA variable labels will still be available as an attribute "variable.labels" (see <link href="rkward://rhelp/read.dta" />)</setting>
+		<setting id="use_labels">Should STATA variables with value labels be converted to R factors with those levels?</setting>
+		<setting id="labels_limit">Maximum number of factor levels to use (see <link href="rkward://rhelp/read.dta" />)</setting>
+		<setting id="trim_labels">Trim trailing white space from labels?</setting>
+
+		<caption id="tab_encoding"/>
+		<setting id="do_locale_conversion">If special character (e.g. umlauts) do not show up correctly, the STATA file probably uses a different locale than your R session. In this case, check this option, and specify the correct character encoding below.</setting>
+		<setting id="encoding">Select character encoding to convert from. The option above needs to be checked, for this to be enabled.</setting>
+		<setting id="user_encoding">If none of the encodings above matches, you can specify an encoding here. Note that the available encodings may differ from platform to platform. See <link href="rkward://rhelp/iconvlist" /> for a list of available encodings.</setting>
+	</settings>
+	<related>
+		<ul>
+			<li><link href="rkward://component/import_csv"/></li>
+			<li><link href="rkward://rhelp/read.dta"/></li>
+			<li><link href="rkward://rhelp/iconv"/></li>
+		</ul>
+	</related>
+</document>

Added: trunk/rkward/rkward/plugins/00saveload/import/import_stata.xml
===================================================================
--- trunk/rkward/rkward/plugins/00saveload/import/import_stata.xml	                        (rev 0)
+++ trunk/rkward/rkward/plugins/00saveload/import/import_stata.xml	2009-09-22 22:45:09 UTC (rev 2652)
@@ -0,0 +1,65 @@
+<!DOCTYPE rkplugin>
+<document>
+	<code file="import_stata.php" />
+	<help file="import_stata.rkh" />
+	<logic>
+		<external id="filename"/>
+		<connect governor="filename" client="file.selection"/>
+		
+		<connect governor="data_frame.state" client="doedit.enabled" />
+		
+		<connect governor="use_labels.state" client="labels_limit.enabled"/>
+		<connect governor="use_labels.state" client="trim_labels.enabled"/>
+		
+		<connect governor="do_locale_conversion.state" client="encoding.enabled" />
+		<convert id="other_encoding" mode="equals" standard="other" sources="encoding.string" />
+		<convert id="other_encoding2" mode="and" sources="other_encoding;do_locale_conversion.state" />
+		<connect governor="other_encoding2" client="user_encoding.enabled" />
+		<connect governor="other_encoding2" client="user_encoding.required" />
+	</logic>
+	<dialog label="Import STATA file">
+		<tabbook>
+			<tab id="tab_general" label="General">
+				<browser type="file" id="file" label="File name" />
+				<stretch/>
+				<row>
+					<saveobject id="saveto" initial="my.stata.data" label="Object to save to"/>
+					<checkbox id="doedit" value="1" value_unchecked="0" label="Edit Object" checked="true" />
+				</row>
+				<stretch/>
+				<frame label="Options">
+					<checkbox id="convert_dates" checked="true" label="Convert Stata dates to 'Date' class" value="1" value_unchecked="0"/>
+					<checkbox id="convert_factors" checked="true" label="Use Stata value labels to create factors?" value="1" value_unchecked="0"/>
+					<checkbox id="missing_type" checked="false" label="For version 8 or later, store information about different types of missing data?" value="1" value_unchecked="0"/>
+					<checkbox id="convert_underscore" checked="false" label="Convert '_' in Stata variable names to '.' in R names" value="1" value_unchecked="0"/>
+					<checkbox id="warn_missing_labels" checked="true" label="Warn if a variable is specified with value labels and those value labels are not present in the file." value="1" value_unchecked="0"/>
+				</frame>
+			</tab>
+			<tab id="tab_encoding" label="Encoding">
+				<checkbox id="do_locale_conversion" checked="false" label="Convert string encoding" value="1" value_unchecked="0"/>
+				<dropdown id="encoding" label="Convert from:">
+					<option value="latin1" label="Latin 1" checked="true"/>
+					<option value="UTF-8" label="UTF-8"/>
+					<option value="ISO8859-1" label="ISO8859-1 Latin-1 Western European"/>
+					<option value="ISO8859-2" label="ISO8859-2 Latin-2 Central European"/>
+					<option value="ISO8859-3" label="ISO8859-3 Latin-3 South European"/>
+					<option value="ISO8859-4" label="ISO8859-4 Latin-4 North European"/>
+					<option value="ISO8859-5" label="ISO8859-5 Latin/Cyrillic"/>
+					<option value="ISO8859-6" label="ISO8859-6 Latin/Arabic"/>
+					<option value="ISO8859-7" label="ISO8859-7 Latin/Greek"/>
+					<option value="MS-GREEK" label="MS-GREEK"/>
+					<option value="ISO8859-8" label="ISO8859-8 Latin/Hebrew"/>
+					<option value="ISO8859-9" label="ISO8859-9 Latin-5 Turkish"/>
+					<option value="ISO8859-10" label="ISO8859-10 Latin-6 Nordic"/>
+					<option value="ISO8859-11" label="ISO8859-11 Latin/Thai"/>
+					<option value="ISO8859-13" label="ISO8859-13 Latin-7 Baltic Rim"/>
+					<option value="ISO8859-14" label="ISO8859-14 Latin-8 Celtic"/>
+					<option value="ISO8859-15" label="ISO8859-15 Latin-9 Western Europen (EUR)"/>
+					<option value="ISO8859-16" label="ISO8859-16 Latin-10 South-Eastern European"/>
+					<option value="other" label="Other (specify below)"/>
+				</dropdown>
+				<input id="user_encoding" label="Other encoding" />
+			</tab>
+		</tabbook>
+	</dialog>
+</document>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list