Skip to content

Commit

Permalink
Fixed issue 06189: Export to R - csv file uses incorrect quote character
Browse files Browse the repository at this point in the history
Dev modified SPSSExportData() to use either 'single quotes' or "double quotes" as quoting character
Dev modified SPSSExportData() to print column headers if export to R
Dev modified exportr() to use R code read.csv() rather than read.table()
  • Loading branch information
andrie committed Aug 21, 2012
1 parent 7e49096 commit 85c8111
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
12 changes: 5 additions & 7 deletions application/controllers/admin/export.php
Expand Up @@ -624,11 +624,11 @@ public function exportr()

//$typeMap = $this->_getTypeMap();

$length_vallabel = '120'; // Set the max text length of Value Labels
// $length_vallabel = '120'; // Set the max text length of Value Labels
$iLength = '25500'; // Set the max text length of Text Data
$length_varlabel = '25500'; // Set the max text length of Variable Labels
$headerComment = '';
$tempFile = '';
// $tempFile = '';

if ( ! isset($iSurveyID) ) { $iSurveyID = returnGlobal('sid'); }
$filterstate = incompleteAnsFilterState();
Expand Down Expand Up @@ -679,7 +679,7 @@ public function exportr()
header("Pragma: public");

$na = ""; //change to empty string instead of two double quotes to fix warnings on NA
SPSSExportData($iSurveyID, $iLength);
SPSSExportData($iSurveyID, $iLength, $na='', $q='"', $header=TRUE);

exit;
}
Expand All @@ -692,11 +692,9 @@ public function exportr()
header("Pragma: public");

echo $headerComment;
echo "data <- read.table(\"survey_" . $iSurveyID
."_R_data_file.csv\", sep=\",\", quote = \"'\", "
."na.strings=c(\"\",\"\\\"\\\"\"), "
."stringsAsFactors=FALSE)\n\n";

echo ('data <- read.csv("survey_' . $iSurveyID .'_R_data_file.csv", na.strings=c(",", "\"\""), stringsAsFactors=FALSE)');
echo ("\n\n");

// Build array that has to be returned
$fields = SPSSFieldMap($iSurveyID,"V");
Expand Down
48 changes: 32 additions & 16 deletions application/helpers/export_helper.php
Expand Up @@ -54,8 +54,10 @@ function isNumericExtended($value) {
* @param mixed $iSurveyID The survey ID
* @param mixed $iLength Maximum text lenght data, usually 255 for SPSS <v16 and 16384 for SPSS 16 and later
* @param mixed $na Value for N/A data
* @param sep Quote separator. Use '\'' for SPSS, '"' for R
* @param logical $header If TRUE, adds SQGA code as column headings (used by export to R)
*/
function SPSSExportData ($iSurveyID, $iLength, $na = '') {
function SPSSExportData ($iSurveyID, $iLength, $na = '', $q='\'', $header=FALSE) {

// Build array that has to be returned
$fields = SPSSFieldMap($iSurveyID);
Expand All @@ -68,6 +70,19 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') {

//This shouldn't occur, but just to be safe:
if (count($fields)<>$num_fields) safeDie("Database inconsistency error");

// Add column headers (used by R export)
if($header==TRUE)
{
$i = 1;
foreach ($fields as $field) {
echo $q.strtoupper($field['sql_name']).$q;
if ($i<$num_fields && !$field['hide']) echo ',';
$i++;
}
echo("\n");
}


foreach ($result as $row) {
$row = array_change_key_case($row,CASE_UPPER);
Expand All @@ -84,7 +99,7 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') {
list( $year, $month, $day, $hour, $minute, $second ) = preg_split( '([^0-9])', $row[$fieldno] );
if ($year != '' && (int)$year >= 1970)
{
echo "'".date('d-m-Y H:i:s', mktime( $hour, $minute, $second, $month, $day, $year ) )."'";
echo $q.date('d-m-Y H:i:s', mktime( $hour, $minute, $second, $month, $day, $year ) ).$q;
} else
{
echo ($na);
Expand All @@ -97,66 +112,67 @@ function SPSSExportData ($iSurveyID, $iLength, $na = '') {
{
if ($row[$fieldno] == 'Y') // Yes/No Question Type
{
echo( "'1'");
echo( $q. 1 .$q);
} else if ($row[$fieldno] == 'N'){
echo( "'2'");
echo( $q. 2 .$q);
} else {
echo($na);
}
} else if ($field['LStype'] == 'G') //Gender
{
if ($row[$fieldno] == 'F')
{
echo( "'1'");
echo( $q. 1 .$q);
} else if ($row[$fieldno] == 'M'){
echo( "'2'");
echo( $q. 2 .$q);
} else {
echo($na);
}
} else if ($field['LStype'] == 'C') //Yes/No/Uncertain
{
if ($row[$fieldno] == 'Y')
{
echo( "'1'");
echo( $q. 1 .$q);
} else if ($row[$fieldno] == 'N'){
echo( "'2'");
echo( $q. 2 .$q);
} else if ($row[$fieldno] == 'U'){
echo( "'3'");
echo( $q. 3 .$q);
} else {
echo($na);
}
} else if ($field['LStype'] == 'E') //Increase / Same / Decrease
{
if ($row[$fieldno] == 'I')
{
echo( "'1'");
echo( $q. 1 .$q);
} else if ($row[$fieldno] == 'S'){
echo( "'2'");
echo( $q. 2 .$q);
} else if ($row[$fieldno] == 'D'){
echo( "'3'");
echo( $q. 3 .$q);
} else {
echo($na);
}
} elseif (($field['LStype'] == 'P' || $field['LStype'] == 'M') && (substr($field['code'],-7) != 'comment' && substr($field['code'],-5) != 'other'))
{
if ($row[$fieldno] == 'Y')
{
echo("'1'");
echo($q. 1 .$q);
} else
{
echo("'0'");
echo($q. 0 .$q);
}
} elseif (!$field['hide']) {
$strTmp=mb_substr(stripTagsFull($row[$fieldno]), 0, $iLength);
if (trim($strTmp) != ''){
$strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp));
if($q=='\'') $strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp));
if($q=='"') $strTemp=str_replace(array('"',"\n","\r"),array('---',' ',' '),trim($strTmp));
/*
* Temp quick fix for replacing decimal dots with comma's
if (isNumericExtended($strTemp)) {
$strTemp = str_replace('.',',',$strTemp);
}
*/
echo "'$strTemp'";
echo $q. $strTemp .$q ;
}
else
{
Expand Down

0 comments on commit 85c8111

Please sign in to comment.