[R-gregmisc-users] SF.net SVN: r-gregmisc:[1370] trunk/gdata/R
                
                Brought to you by:
                
                    warnes
                    
                
            
            
        
        
        
    | 
      
      
      From: <wa...@us...> - 2010-01-22 12:45:31
      
     | 
| Revision: 1370
          http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1370&view=rev
Author:   warnes
Date:     2010-01-22 12:45:21 +0000 (Fri, 22 Jan 2010)
Log Message:
-----------
- Move xls2csv(), xls2tab(), xls2sep() to a separate file
- Move qQuote.ascii to a separate file
- Bug Fix: xls2csv(), xls2tab() failed to pass the provided 'perl'
  parameter to xls2sep()
- New Feature: xls2sep() (and hence xls2csv, xls2tab, and read.xls)
  now supports ftp URLs.
Modified Paths:
--------------
    trunk/gdata/R/read.xls.R
Added Paths:
-----------
    trunk/gdata/R/dQuote.ascii.R
    trunk/gdata/R/sheetCount.R
    trunk/gdata/R/xls2sep.R
Added: trunk/gdata/R/dQuote.ascii.R
===================================================================
--- trunk/gdata/R/dQuote.ascii.R	                        (rev 0)
+++ trunk/gdata/R/dQuote.ascii.R	2010-01-22 12:45:21 UTC (rev 1370)
@@ -0,0 +1,9 @@
+## s$Id: read.xls.R 1342 2009-07-16 02:49:11Z warnes $
+
+## Double quote string using *ASCII* double quotes.
+##
+## (The base 'dQuote' uses local-specific quotes (e.g UTF-8 quotes)
+## which Unix command line doesn't like.)
+##
+dQuote.ascii <- function(x) paste('"',x,'"',sep='')
+
Modified: trunk/gdata/R/read.xls.R
===================================================================
--- trunk/gdata/R/read.xls.R	2009-12-06 22:34:29 UTC (rev 1369)
+++ trunk/gdata/R/read.xls.R	2010-01-22 12:45:21 UTC (rev 1370)
@@ -1,106 +1,5 @@
 ## s$Id$
 
-## Creating a temporary function to quote the string
-dQuote.ascii <- function(x) paste('"',x,'"',sep='')
-
-
-xls2csv <- function(xls, sheet=1, verbose=FALSE, ..., perl="perl")
-  xls2sep(xls=xls, sheet=sheet, verbose=verbose, ..., method="csv",
-          perl="perl") 
-
-xls2tab <- function(xls, sheet=1, verbose=FALSE, ..., perl="perl")
-  xls2sep(xls=xls, sheet=sheet, verbose=verbose, ..., method="tab",
-          perl="perl") 
-
-xls2sep <- function(xls, sheet = 1, verbose=FALSE, ...,
-                    method=c("csv","tab"), perl="perl")
-  {
-    
-    method <- match.arg(method)
-    
-    ##
-    ## directories
-    package.dir <- .path.package('gdata')
-    perl.dir <- file.path(package.dir,'perl')
-    ##
-
-    ##
-    ## files
-    tf <- NULL
-    if (substring(xls, 1, 7) == "http://") {
-      tf <- paste(tempfile(), "xls", sep = ".")
-      if(verbose)
-        cat("Downloading",
-            dQuote.ascii(xls), " to ",
-            dQuote.ascii(tf), "...\n")
-      else
-        cat("Downloading...\n")
-      download.file(xls, tf, mode = "wb")
-      cat("Done.\n")
-      xls <- tf
-    }
-
-    if(file.access(xls, 4)!=0)
-      stop("Unable to read xls file '", xls, "'." )
-
-    if(method=="csv")
-      {
-        script <- file.path(perl.dir,'xls2csv.pl')
-        targetFile <- paste(tempfile(), "csv", sep = ".")
-      }
-    else if(method=="tab")
-      {
-        script <- file.path(perl.dir,'xls2tab.pl')
-        targetFile <- paste(tempfile(), "tab", sep = ".")
-      }
-    else
-      {
-        stop("Unknown method", method)
-      }
-    
-    ##
-    ##
-
-    ##
-    ## execution command
-    cmd <- paste(perl, script, dQuote.ascii(xls), dQuote.ascii(targetFile),
-                 sheet, sep=" ")
-    ##
-    ##
-
-    if(verbose)
-      {
-        cat("\n")
-        cat("Converting xls file\n")
-        cat("   ", dQuote.ascii(xls), "\n")
-        cat("to", method, " file \n")
-        cat("   ", dQuote.ascii(targetFile), "\n")
-        cat("... \n\n")
-      }
-    else
-      cat("Converting xls file to", method, "file... ")         
-    
-    ##
-    ## do the translation
-    if(verbose)  cat("Executing ", cmd, "... \n\n")
-                                        #
-    results <- system(cmd, intern=!verbose)
-                                        #
-    if (verbose) cat("Done.\n\n")
-                                        #
-    ##
-
-    if(file.access(targetFile, 4)!=0)
-      stop("Unable to read translated", method, "file '", targetFile, "'." )
-    
-    cat("Done.\n")
-
-    
-    ## prepare for cleanup now, in case of error reading file
-    file(targetFile)
-  }
-
-
 read.xls <- function(xls, sheet = 1, verbose=FALSE, pattern, ...,
                      method=c("csv","tab"), perl="perl")
 {
Added: trunk/gdata/R/sheetCount.R
===================================================================
--- trunk/gdata/R/sheetCount.R	                        (rev 0)
+++ trunk/gdata/R/sheetCount.R	2010-01-22 12:45:21 UTC (rev 1370)
@@ -0,0 +1,58 @@
+sheetCount <- function(xls, verbose = FALSE, perl = "perl")
+{
+
+  ##
+  ## directories
+  package.dir <- .path.package('gdata')
+  perl.dir <- file.path(package.dir,'perl')
+  ##
+  ##
+
+  ##
+  ## files
+  tf <- NULL
+  if ( substring(xls, 1, 7) == "http://" ||
+      substring(xls, 1, 6) == "ftp://" )
+    {
+      tf <- paste(tempfile(), "xls", sep = ".")
+      if(verbose)
+        cat("Downloading",
+            dQuote.ascii(xls), " to ",
+            dQuote.ascii(tf), "...\n")
+      else
+        cat("Downloading...\n")
+      download.file(xls, tf, mode = "wb")
+      cat("Done.\n")
+      xls <- tf
+    }
+  ##
+  
+  sc <- file.path(perl.dir,'sheetCount.pl')
+  
+  ##
+  ##
+
+  ##
+  ## execution command
+  cmd <- paste(perl, sc, dQuote.ascii(xls), sep=" ")
+  ##
+  ##
+
+  ##
+  ## do the translation
+  if(verbose)
+    {
+      cat("\n")
+      cat("Extracting sheet count from\n")
+      cat("   ", dQuote.ascii(xls), "\n")
+      cat("... \n\n")
+    }
+  ##
+  results <- system(cmd, intern=TRUE)
+  ##
+  if (verbose) cat("Done.\n\n")
+  
+  as.numeric(results)
+}
+
+
Copied: trunk/gdata/R/xls2sep.R (from rev 1342, trunk/gdata/R/read.xls.R)
===================================================================
--- trunk/gdata/R/xls2sep.R	                        (rev 0)
+++ trunk/gdata/R/xls2sep.R	2010-01-22 12:45:21 UTC (rev 1370)
@@ -0,0 +1,100 @@
+## s$Id$
+
+xls2csv <- function(xls, sheet=1, verbose=FALSE, ..., perl="perl")
+  xls2sep(xls=xls, sheet=sheet, verbose=verbose, ..., method="csv",
+          perl=perl)
+
+xls2tab <- function(xls, sheet=1, verbose=FALSE, ..., perl="perl")
+  xls2sep(xls=xls, sheet=sheet, verbose=verbose, ..., method="tab",
+          perl=perl) 
+
+xls2sep <- function(xls, sheet = 1, verbose=FALSE, ...,
+                    method=c("csv","tab"), perl="perl")
+  {
+    
+    method <- match.arg(method)
+    
+    ##
+    ## directories
+    package.dir <- .path.package('gdata')
+    perl.dir <- file.path(package.dir,'perl')
+    ##
+
+    ##
+    ## files
+    tf <- NULL
+    if ( substring(xls, 1, 7) == "http://" ||
+         substring(xls, 1, 6) == "ftp://" )
+      {
+        tf <- paste(tempfile(), "xls", sep = ".")
+        if(verbose)
+          cat("Downloading",
+              dQuote.ascii(xls), " to ",
+              dQuote.ascii(tf), "...\n")
+        else
+          cat("Downloading...\n")
+        download.file(xls, tf, mode = "wb")
+        cat("Done.\n")
+        xls <- tf
+      }
+
+    if(file.access(xls, 4)!=0)
+      stop("Unable to read xls file '", xls, "'." )
+
+    if(method=="csv")
+      {
+        script <- file.path(perl.dir,'xls2csv.pl')
+        targetFile <- paste(tempfile(), "csv", sep = ".")
+      }
+    else if(method=="tab")
+      {
+        script <- file.path(perl.dir,'xls2tab.pl')
+        targetFile <- paste(tempfile(), "tab", sep = ".")
+      }
+    else
+      {
+        stop("Unknown method", method)
+      }
+    
+    ##
+    ##
+
+    ##
+    ## execution command
+    cmd <- paste(perl, script, dQuote.ascii(xls), dQuote.ascii(targetFile),
+                 dQuote.ascii(sheet), sep=" ")
+    ##
+    ##
+
+    if(verbose)
+      {
+        cat("\n")
+        cat("Converting xls file\n")
+        cat("   ", dQuote.ascii(xls), "\n")
+        cat("to", method, " file \n")
+        cat("   ", dQuote.ascii(targetFile), "\n")
+        cat("... \n\n")
+      }
+    else
+      cat("Converting xls file to", method, "file... ")         
+    
+    ##
+    ## do the translation
+    if(verbose)  cat("Executing ", cmd, "... \n\n")
+    ##
+    results <- system(cmd, intern=!verbose)
+    ##
+    if (verbose) cat("Done.\n\n")
+    ##
+    ##
+
+    if(file.access(targetFile, 4)!=0)
+      stop("Unable to read translated", method, "file '", targetFile, "'." )
+    
+    cat("Done.\n")
+
+    
+    ## prepare for cleanup now, in case of error reading file
+    file(targetFile)
+  }
+
Property changes on: trunk/gdata/R/xls2sep.R
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:mergeinfo
   + 
Added: svn:eol-style
   + native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |