Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 125 lines (108 sloc) 3.595 kB
47e92d1 regression tests for read.table: fixes used here not yet committed
ripley authored
1 ## tests of boundary cases in read.table()
be4d47e tweaks to work with stringsAsFactors=FALSE
ripley authored
2
3 ## force standard handling for character cols
4 options(stringsAsFactors=TRUE)
5
47e92d1 regression tests for read.table: fixes used here not yet committed
ripley authored
6 # empty file
7 file.create("foo1")
8 try(read.table("foo1")) # fails
9 read.table("foo1", col.names=LETTERS[1:4])
10 unlink("foo1")
11
12 # header only
13 cat("head\n", file = "foo2")
14 read.table("foo2")
15 try(read.table("foo2", header=TRUE)) # fails in 1.2.3
16 unlink("foo2")
17 # header detection
18 cat("head\n", 1:2, "\n", 3:4, "\n", file = "foo3")
19 read.table("foo3", header=TRUE)
20 read.table("foo3", header=TRUE, col.names="V1")
21 read.table("foo3", header=TRUE, row.names=1)
22 read.table("foo3", header=TRUE, row.names="row.names")
23 read.table("foo3", header=TRUE, row.names="head") # fails in 1.2.3
24
25 # wrong col.names
26 try(read.table("foo3", header=TRUE, col.names=letters[1:4]))
27 unlink("foo3")
28
29 # incomplete last line
30 cat("head\n", 1:2, "\n", 3:4, file = "foo4")
31 read.table("foo4", header=TRUE)
32 unlink("foo4")
33
34 # blank last line
35 cat("head\n\n", 1:2, "\n", 3:4, "\n\n", file = "foo5")
36 read.table("foo5", header=TRUE)
37
38 # test of fill
39 read.table("foo5", header=FALSE, fill=TRUE, blank.lines.skip=FALSE) # fails in 1.2.3
40 unlink("foo5")
41
42 cat("head\n", 1:2, "\n", 3:5, "\n", 6:9, "\n", file = "foo6")
43 try(read.table("foo6", header=TRUE))
44 try(read.table("foo6", header=TRUE, fill=TRUE))
45 read.table("foo6", header=FALSE, fill=TRUE)
46 unlink("foo6")
47
4b5fdbc enhance read.table
ripley authored
48 # test of type conversion in 1.4.0 and later.
49 cat("A B C D E F\n",
50 "1 1 1.1 1.1+0i NA F abc\n",
51 "2 NA NA NA NA NA NA\n",
52 "3 1 2 3 NA TRUE def\n",
53 sep = "", file = "foo7")
54 (res <- read.table("foo7"))
55 sapply(res, typeof)
56 sapply(res, class)
57 (res2 <- read.table("foo7",
58 colClasses = c("character", rep("numeric", 2),
59 "complex", "integer", "logical", "character")))
60 sapply(res2, typeof)
61 sapply(res2, class)
62 unlink("foo7")
63
64 # should be logical
65 type.convert(character(0))
66
025eef3 comment chars allowed in read.table
ripley authored
67 # test of comments in data files
68 cat("# a test file",
69 "# line 2",
70 "# line 3",
ead62cd Add #line directive to R parser, and in a few places to code processi…
murdoch authored
71 "# line 4",
025eef3 comment chars allowed in read.table
ripley authored
72 "# line 5",
73 "## now the header",
74 " a b c",
75 "# some more comments",
76 "1 2 3",
77 "4 5 6# this is the second data row of the file",
78 " # some more comments",
79 "7 8 9",
80 "# trailing comment\n",
81 file= "ex.data", sep="\n")
82 read.table("ex.data", header = T)
83 unlink("ex.data")
84
85f79f8 allow comment chars inside headers in read.table
ripley authored
85 ## comment chars in headers
86 cat("x1\tx#2\tx3\n1\t2\t2\n2\t3\t3\n", file = "test.dat")
87 read.table("test.dat", header=T, comment.char="")
88 unlink("test.dat")
89
cee0ef3 read.table was failing wiht quoted comment chars in first 5 lines
ripley authored
90 cat('#comment\n\n#another\n#\n#\n',
91 'C1\tC2\tC3\n"Panel"\t"Area Examined"\t"# Blemishes"\n',
92 '"1"\t"0.8"\t"3"\n', '"2"\t"0.6"\t"2"\n', '"3"\t"0.8"\t"3"\n',
93 file = "test.dat", sep="")
94 read.table("test.dat")
95 unlink("test.dat")
96
bc5f450 back to arbitrary comment.char
ripley authored
97 cat('%comment\n\n%another\n%\n%\n',
98 'C1\tC2\tC3\n"Panel"\t"Area Examined"\t"% Blemishes"\n',
99 '"1"\t"0.8"\t"3"\n', '"2"\t"0.6"\t"2"\n', '"3"\t"0.8"\t"3"\n',
100 file = "test.dat", sep="")
101 read.table("test.dat", comment.char = "%")
102 unlink("test.dat")
103
5fb2926 handle BOMs in UCS-2LE encoding
ripley authored
104 ## test on Windows Unicode file
8b5ea55 clean up now iconv is always available
ripley authored
105 con <- file(file.path(Sys.getenv("SRCDIR"), "WinUnicode.dat"),
106 encoding="UCS-2LE")
107 scan(con, 0, quiet=TRUE)
108 close(con)
1ab4181 add allowEscapes to read.table()
ripley authored
109
110 ## tests of allowEscape
111 x <- "1 2 3 \\ab\\c"
112 writeLines(x, "test.dat")
113 readLines("test.dat")
fff5dc4 update
ripley authored
114 scan("test.dat", "", allowEscapes=TRUE)
1ab4181 add allowEscapes to read.table()
ripley authored
115 scan("test.dat", "", allowEscapes=FALSE)
fff5dc4 update
ripley authored
116 read.table("test.dat", header=FALSE, allowEscapes=TRUE)
1ab4181 add allowEscapes to read.table()
ripley authored
117 read.table("test.dat", header=FALSE, allowEscapes=FALSE)
118 x <- c("TEST", 1, 2, "\\b", 4, 5, "\\040", "\\x20",
119 "c:\\spencer\\tests",
120 "\\t", "\\n", "\\r")
121 writeLines(x, "test.dat")
122 read.table("test.dat", allowEscapes=FALSE, header = TRUE)
123 unlink("test.dat")
47e92d1 regression tests for read.table: fixes used here not yet committed
ripley authored
124 ## end of tests
Something went wrong with that request. Please try again.