[R-gregmisc-users] SF.net SVN: r-gregmisc:[1596] trunk/gdata/R/read.xls.R
Brought to you by:
warnes
From: <wa...@us...> - 2012-08-22 15:45:29
|
Revision: 1596 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1596&view=rev Author: warnes Date: 2012-08-22 15:45:22 +0000 (Wed, 22 Aug 2012) Log Message: ----------- Change code to have R read the csv/tab data from the file rather than from the connetion we made, so that file encodings can be properly handled. Modified Paths: -------------- trunk/gdata/R/read.xls.R Modified: trunk/gdata/R/read.xls.R =================================================================== --- trunk/gdata/R/read.xls.R 2012-08-22 14:29:40 UTC (rev 1595) +++ trunk/gdata/R/read.xls.R 2012-08-22 15:45:22 UTC (rev 1596) @@ -28,19 +28,21 @@ findPerl(perl, verbose = verbose) con <- xls2sep(xls, sheet, verbose=verbose, ..., method=method, perl = perl) + ## While xls2sep returns a connection, we are better off directly + ## opening the file, so that R can properly handle the encoding. So, + ## just grab the full file path to use later, and close the connection. + tfn <- summary(con)$description + close(con) - ## load the csv file - open(con) - tfn <- summary(con)$description if (missing(pattern)) { if(verbose) cat("Reading", method, "file ", dQuote(tfn), "...\n") if(method=="csv") - retval <- read.csv(con, na.strings=na.strings, ...) + retval <- read.csv(tfn, na.strings=na.strings, ...) else if (method %in% c("tsv","tab") ) - retval <- read.delim(con, na.strings=na.strings, ...) + retval <- read.delim(tfn, na.strings=na.strings, ...) else stop("Unknown method", method) @@ -49,8 +51,8 @@ } else { if(verbose) - cat("Searching for lines containing pattern ", pattern, "... ") - idx <- grep(pattern, readLines(con)) + cat("Searching for lines tfntaining pattern ", pattern, "... ") + idx <- grep(pattern, readLines(tfn)) if (length(idx) == 0) { warning("pattern not found") return(NULL) @@ -58,22 +60,19 @@ if(verbose) cat("Done.\n") - seek(con, 0) - if(verbose) cat("Reading", method, "file ", dQuote(tfn), "...\n") if(method=="csv") - retval <- read.csv(con, skip = idx[1]-1, na.strings=na.strings, ...) + retval <- read.csv(tfn, skip = idx[1]-1, na.strings=na.strings, ...) else if (method %in% c("tsv","tab") ) - retval <- read.delim(con, skip = idx[1]-1, na.strings=na.strings, ...) + retval <- read.delim(tfn, skip = idx[1]-1, na.strings=na.strings, ...) else stop("Unknown method", method) if(verbose) cat("Done.\n") } - close(con) retval } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |