Changeset: 58b47875e389 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=58b47875e389
Modified Files:
        clients/R/MonetDB.R/NEWS
        clients/R/MonetDB.R/R/dbi.R
        clients/R/MonetDB.R/man/monetdb.read.csv.Rd
        clients/R/Tests/dbi.stable.out
Branch: default
Log Message:

R Connector: new col.names parameter to monetdb.read.csv() that allows to 
explicitly set the column names in the generated table


diffs (141 lines):

diff --git a/clients/R/MonetDB.R/NEWS b/clients/R/MonetDB.R/NEWS
--- a/clients/R/MonetDB.R/NEWS
+++ b/clients/R/MonetDB.R/NEWS
@@ -8,7 +8,8 @@ 1.0.0
 - Fix for case when query only returns a prompt (CALL ..., Thanks, Roman)
 - Fix for empty result set on dbGetQuery(), no longer returning NULL (Thanks, 
Fabian)
 - Fix for dbConnect(), it ignored the url parameter somehow, which broke some 
sqlsurvey (Thanks, Anthony)
-- Added lower.case.names= to monet.read.csv() in case users want to avoid 
quoting
+- Added col.names argument to monet.read.csv()
+- Fix for dbConnect() that should be more robust to invalid connections
 
 0.9.7
 - Fixed crash on Windows (Sorry, everyone)
diff --git a/clients/R/MonetDB.R/R/dbi.R b/clients/R/MonetDB.R/R/dbi.R
--- a/clients/R/MonetDB.R/R/dbi.R
+++ b/clients/R/MonetDB.R/R/dbi.R
@@ -103,7 +103,7 @@ setMethod("dbConnect", "MonetDBDriver", 
         break
       }, error = function(e) {
         if ("connection" %in% class(socket)) {
-          close(socket)
+          tryCatch(close(socket), error=function(e){}) 
         }
         message("Server not ready(", e$message, "), retrying (ESC or CTRL+C to 
abort)")
         Sys.sleep(1)
@@ -283,8 +283,6 @@ setMethod("dbSendQuery", signature(conn=
   invisible(new("MonetDBResult", env=env))
   })
 
-
-
 # quoting
 quoteIfNeeded <- function(conn, x, ...) {
   chars <- !grepl("^[a-z][a-z0-9_]*$", x, perl=T) & !grepl("^\"[^\"]*\"$", x, 
perl=T)
@@ -315,7 +313,7 @@ setMethod("dbWriteTable", "MonetDBConnec
     if (!is.data.frame(value)) value <- as.data.frame(value)
   }
   if (overwrite && append) {
-    stop("Setting both overwrite and append to true makes no sense.")
+    stop("Setting both overwrite and append to TRUE makes no sense.")
   }
 
   qname <- quoteIfNeeded(conn, name)
@@ -622,7 +620,8 @@ setMethod("dbGetInfo", "MonetDBResult", 
 # adapted from RMonetDB, no java-specific things in here...
 monet.read.csv <- monetdb.read.csv <- function(conn, files, tablename, 
nrows=NA, header=TRUE, 
                                                locked=FALSE, na.strings="", 
nrow.check=500, 
-                                               delim=",", newline="\\n", 
quote="\"", create=TRUE, lower.case.names=FALSE, ...){
+                                               delim=",", newline="\\n", 
quote="\"", create=TRUE, 
+                                               col.names=NULL, 
lower.case.names=FALSE, ...){
   
   if (length(na.strings)>1) stop("na.strings must be of length 1")
   headers <- lapply(files, utils::read.csv, sep=delim, na.strings=na.strings, 
quote=quote, nrows=nrow.check, 
@@ -631,7 +630,7 @@ monet.read.csv <- monetdb.read.csv <- fu
   if (!missing(nrows)) {
     warning("monetdb.read.csv(): nrows parameter is not neccessary any more 
and deprecated.")
   }
-  
+
   if (length(files)>1){
     nn <- sapply(headers, ncol)
     if (!all(nn==nn[1])) stop("Files have different numbers of columns")
@@ -642,9 +641,19 @@ monet.read.csv <- monetdb.read.csv <- fu
   } 
   
   if (create){
-    if(lower.case.names) names(headers[[1]]) <- tolower(names( headers[[1]]))
+    if(lower.case.names) names(headers[[1]]) <- tolower(names(headers[[1]]))
+    if(!is.null(col.names)) {
+      if (lower.case.names) {
+        warning("Ignoring lower.case.names parameter as overriding col.names 
are supplied.")
+      }
+      col.names <- as.character(col.names)
+      if (length(unique(col.names)) != length(names(headers[[1]]))) {
+        stop("You supplied ", length(unique(col.names)), " unique column 
names, but file has ", 
+          length(names(headers[[1]])), " columns.")
+      }
+      names(headers[[1]]) <- quoteIfNeeded(conn, col.names)
+    }
     dbWriteTable(conn, tablename, headers[[1]][FALSE, ])
-
   }
   
   delimspec <- paste0("USING DELIMITERS '", delim, "','", newline, "','", 
quote, "'")
@@ -663,6 +672,6 @@ monet.read.csv <- monetdb.read.csv <- fu
         delimspec, "NULL as ", paste("'", na.strings[1], "'", sep=""), 
if(locked) " LOCKED "))
     }
   }
-  dbGetQuery(conn, paste("select count(*) from", tablename))
+  dbGetQuery(conn, paste("SELECT COUNT(*) FROM", tablename))[[1]]
 }
 
diff --git a/clients/R/MonetDB.R/man/monetdb.read.csv.Rd 
b/clients/R/MonetDB.R/man/monetdb.read.csv.Rd
--- a/clients/R/MonetDB.R/man/monetdb.read.csv.Rd
+++ b/clients/R/MonetDB.R/man/monetdb.read.csv.Rd
@@ -11,14 +11,14 @@
 \usage{
    monetdb.read.csv (conn, files, tablename, nrows=NA, header=TRUE, 
   locked=FALSE, na.strings="", nrow.check=500, delim=",",
-  newline = "\\\\n", quote = "\"", create=TRUE, lower.case.names=FALSE, ...)
+  newline = "\\\\n", quote = "\"", create=TRUE, col.names=NULL, 
lower.case.names=FALSE, ...)
 }
 \arguments{
   \item{conn}{A MonetDB.R database connection. Created using 
\code{\link[DBI]{dbConnect}} with the 
   \code{\link[MonetDB.R]{MonetDB.R}} database driver.}
    \item{files}{A single string or a vector of strings containing the absolute 
file names of the CSV files to be imported.}
    \item{tablename}{Name of the database table the CSV files should be 
imported in. Created if necessary.}
-   \item{nrows}{Total number of rows to import. Can be an upper bound.}
+   \item{nrows}{Total number of rows to import (deprecated).}
    \item{header}{Whether or not the CSV files contain a header line.}
    \item{locked}{Whether or not to disable transactions for import. 
    Setting this to TRUE can greatly improve the import performance.}
@@ -31,6 +31,7 @@
    \item{quote}{Quote character(s) in CSV file.}
   \item{create}{Create table before importing?}
   \item{lower.case.names}{Convert all column names to lowercase in the 
database?}
+  \item{col.names}{Optional column names in case the ones from CSV file should 
not be used}
  }
  
 \value{
@@ -48,6 +49,6 @@ data(iris)
 file <- tempfile()
 write.table(iris, file, sep=",")
 # create table and import CSV
-monetdb.read.csv(conn, file, "iris", 150)
+monetdb.read.csv(conn, file, "iris")
 }}
 \keyword{interface}
diff --git a/clients/R/Tests/dbi.stable.out b/clients/R/Tests/dbi.stable.out
--- a/clients/R/Tests/dbi.stable.out
+++ b/clients/R/Tests/dbi.stable.out
@@ -52,8 +52,7 @@ Ready.
 [1] TRUE
 [1] TRUE
 [1] TRUE
-   L1
-1 150
+[1] 150
 [1] TRUE
 [1] TRUE
 [1] TRUE
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to