scan(file = "", what = double(), nmax = -1, n = -1, sep = "", quote = if (identical(sep, "\n")) "" else "'\"", dec = ".", skip = 0, nlines = 0, na.strings = "NA", flush = FALSE, fill = FALSE, strip.white = FALSE, quiet = FALSE, blank.lines.skip = TRUE, multi.line = TRUE, comment.char = "", allowEscapes = FALSE, fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
file | the file or connection object to be scanned. If file is missing or empty (""), data is read from standard input. In this case, scan prompts with the index for the next data item, and input can be terminated by a blank line. For more details on reading data from connections, see file. |
what |
a vector of mode "logical", "integer", "numeric", "character", "raw" or "complex", or a list of vectors of these modes.
The scan function reads all fields in the file as data of the same mode as what.
Thus, what=character() or what="" reads data as character fields.
If what is missing, scan interprets all fields as numeric.
If what is a list, then each record is considered to have length(what) fields, and the mode of each field is the mode of the corresponding component in what. Any NULL values in the list implies skipping that field. |
nmax | an integer. Specifies the maximum number of data values to be read. When what is a list, it is the maximum number of records to be read. If note provided or not positive or invalid, then scan reads to the end of file. |
n | the maximum number of items to read from the file (the number of records times the fields per record). If omitted, the function reads to the end of file, or to an empty line if reading from standard input. |
sep | a single-character separator, often "\t" for tabs or "\n" for newlines. If omitted, any amount of white space (blanks, tabs, and possibly newlines) can separate fields. By default, sep="". |
quote | A character string listing the quote characters used in the file. Characters between a pair of identical quote characters are considered to be a single character string -- the quote characters are not part of that string. As a special case, NULL, may be used instead of "" to indicate that there are no quote characters. The default is "\"'", indicating that single and double quotation marks are the quote characters. Use "\"" to allow single quotes in strings (as in names). |
dec | a character as decimal point character. Only a character string with just one single-byte character(or NULL and zero-length character are also accepted, but considered as default). |
skip | the number of initial lines of the file that should be skipped prior to reading. By default, skip=0 and reading begins at the top of the file. |
nlines | an integer to specify the maximum number of lines of data to be read. It is omitted if not positive or zero. |
na.strings | a character string vector. Elements in this vector are to be considered as NA. |
flush | a logical value. If flush=TRUE, then the scan function flushes to the end of the line after reading the last of the fields requested. This allows you to include comments that are not read by scan after the last field. It also prevents multiple sets of items from being placed on one line. By default, flush=FALSE. |
fill | a logical value. If TRUE, when any line has fewer fields specified by what, then scan adds empty fields to them. |
strip.white | a vector of logical values corresponding to items in the what argument. The strip.white argument allows you to strip leading and trailing white space from character fields; scan always strips numeric fields in this way. If strip.white is not NULL, it must be either of length 1, in which case the single logical value tells whether to strip all fields read, or it must be the same length as what, in which case the logical vector tells which fields to strip. For example, if strip.white[1]=TRUE and field 1 is character, scan strips the leading and trailing white space from field 1. If you read free-format input by leaving sep unspecified, then strip.white has no effect. |
quiet | a logical value. If FALSE, scan prints a line of how many items have been read. |
blank.lines.skip | a logical value. If TRUE blank lines will be omitted, except when counting skip and nlines. |
multi.line | a logical value. Used only when what is a list. If multi.line=FALSE, then all fields must appear on one line. If scan reaches the end of a line without reading all the fields, then an error occurs. Thus, the number of fields on each line must be a multiple of the length of what, unless flush=TRUE. This is useful for checking that no fields have been omitted. If multi.line=TRUE, then reading continues and the positions of newlines are disregarded. By default, multi.line=FALSE. |
comment.char | a character vector of length one. Contains one single character or an empty string. The default value "" turns off the interpretation of comments altogether. |
allowEscapes | a logical value. To determine how to deal with C-style escapes such as \n. The escapes that are interpreted are the control characters \a, \b, \f, \n, \r, \t, \v and octal and hexadecimal representations like \040 and \x2A. |
fileEncoding | a character string. The character encoding for the input stream, used for converting input bytes to characters. This can be any of the encodings accepted by iconv, including "native.enc". The default value, the empty string, means to use options("encoding"). This argument is not used when the file argument is a connection, because a connection already has an associated encoding (see file). |
encoding | the string encoding to use for newly-created strings. (See Encoding.) In TERR, this argument is not implemented. |
text | a character vector. If specified, and if file is not specified, then scan reads from the character vector. |
skipNul |
a logical value. Specifies how to deal with nul (0) bytes in the input file.
|
# Read numeric values from standard input. ## Not run: num <- scan() ## End(Not run)## Not run: # Read text values from standard input. txt <- scan(what="") ## End(Not run)
# read from specified text argument scan(text=c("1 2\n3 4", "5 6"))
# Read a label and two numeric fields to make a matrix. tfile <- tempfile() cat("row1 9 10", "row2 2 3", sep="\n", file=tfile) z <- scan(tfile, list(name="", 0, 0)) mat <- cbind(z[[2]], z[[3]]) dimnames(mat) <- list(z$name, c("X","Y")) mat
# Read a CSV file, skip the first line of the file # and save in single precision: cat(file=tfile, sep="\n", "This line is skipped", "1,1,2,3,5,8", "13,21,34,55", "89,144,233,377") scan(tfile, single(0), skip=1)
# Clean up: unlink(tfile)