[Rd] [PATCH 2/2] readtable: add test for type conversion hook 'colConvert'

Kurt Van Dijck dev@kurt @end|ng |rom v@nd|jck-|@ur|j@@en@be
Fri Mar 22 18:16:23 CET 2019


Signed-off-by: Kurt Van Dijck <dev.kurt using vandijck-laurijssen.be>
---
 tests/reg-tests-2.R         | 21 +++++++++++++++++++++
 tests/reg-tests-2.Rout.save | 27 +++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/tests/reg-tests-2.R b/tests/reg-tests-2.R
index 9fd5242..5026fe7 100644
--- a/tests/reg-tests-2.R
+++ b/tests/reg-tests-2.R
@@ -1329,6 +1329,27 @@ unlink(foo)
 ## added in 2.0.0
 
 
+## colConvert in read.table
+probecol <- function(col) {
+	tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y %H:%M"));
+	if (all(!is.na(tmp)))
+		return (tmp)
+	tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y"));
+	if (all(!is.na(tmp)))
+		return (tmp)
+}
+
+Mat <- matrix(c(1:3, letters[1:3], 1:3, LETTERS[1:3],
+                c("22/4/1969", "8/4/1971", "23/9/1973"),
+                c("22/4/1969 6:01", " 8/4/1971 7:23", "23/9/1973 8:45")),
+              3, 6)
+foo <- tempfile()
+write.table(Mat, foo, sep = ",", col.names = FALSE, row.names = FALSE)
+read.table(foo, sep = ",", colConvert=probecol)
+unlist(sapply(.Last.value, class))
+unlink(foo)
+
+
 ## write.table with complex columns (PR#7260, in part)
 write.table(data.frame(x = 0.5+1:4, y = 1:4 + 1.5i), file = "")
 # printed all as complex in 2.0.0.
diff --git a/tests/reg-tests-2.Rout.save b/tests/reg-tests-2.Rout.save
index 598dd71..668898e 100644
--- a/tests/reg-tests-2.Rout.save
+++ b/tests/reg-tests-2.Rout.save
@@ -4206,6 +4206,33 @@ Warning message:
 > ## added in 2.0.0
 > 
 > 
+> ## colConvert in read.table
+> probecol <- function(col) {
++ 	tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y %H:%M"));
++ 	if (all(!is.na(tmp)))
++ 		return (tmp)
++ 	tmp <- as.POSIXlt(col, optional=TRUE, tryFormats=c("%d/%m/%Y"));
++ 	if (all(!is.na(tmp)))
++ 		return (tmp)
++ }
+> 
+> Mat <- matrix(c(1:3, letters[1:3], 1:3, LETTERS[1:3],
++                 c("22/4/1969", "8/4/1971", "23/9/1973"),
++                 c("22/4/1969 6:01", " 8/4/1971 7:23", "23/9/1973 8:45")),
++               3, 6)
+> foo <- tempfile()
+> write.table(Mat, foo, sep = ",", col.names = FALSE, row.names = FALSE)
+> read.table(foo, sep = ",", colConvert=probecol)
+  V1 V2 V3 V4         V5                  V6
+1  1  a  1  A 1969-04-22 1969-04-22 06:01:00
+2  2  b  2  B 1971-04-08 1971-04-08 07:23:00
+3  3  c  3  C 1973-09-23 1973-09-23 08:45:00
+> unlist(sapply(.Last.value, class))
+       V1        V2        V3        V4       V51       V52       V61       V62 
+"integer"  "factor" "integer"  "factor" "POSIXlt"  "POSIXt" "POSIXlt"  "POSIXt" 
+> unlink(foo)
+> 
+> 
 > ## write.table with complex columns (PR#7260, in part)
 > write.table(data.frame(x = 0.5+1:4, y = 1:4 + 1.5i), file = "")
 "x" "y"
-- 
1.8.5.rc3



More information about the R-devel mailing list