Skip to content
Prev 281075 / 398503 Next

[newbie] read row from file into vector

Look into connection objects, which let you open a file or other
readable sort of thing and read it piece by piece.  E.g., the
following function opens your file (making a "file connection"),
skips some lines, reads the desired line into a character object,
then reads from that character object (as a "text connection")
to make a numeric object:

f <- function (fileName, lineNumber, ...) 
{
    connection <- file(fileName, "rt")
    on.exit(close(connection))
    if (lineNumber > 1) {
        readLines(connection, n = lineNumber - 1)
    }
    lineText <- readLines(connection, n = 1)
    scan(textConnection(lineText), ...)
}

Here is a self-contained example:
+     "101;102;103", "104,105", "106/107/108")
A data file with a header line
101;102;103
104,105
106/107/108
Read 2 items
[1] 104 105

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com