read.csv.sql: Read File Filtered by SQL

Description Usage Arguments Details Value Examples

Description

Read a file into R filtering it with an sql statement. Only the filtered portion is processed by R so that files larger than R can otherwise handle can be accommodated.

Usage

1
2
3
4
5
6
read.csv.sql(file, sql = "select * from file", header = TRUE, sep = ",", 
row.names, eol, skip, filter, nrows, field.types, 
colClasses, dbname = tempfile(), drv = "SQLite", ...)
read.csv2.sql(file, sql = "select * from file", header = TRUE, sep = ";", 
row.names, eol, skip, filter, nrows, field.types,
colClasses, dbname = tempfile(), drv = "SQLite", ...)

Arguments

file

A file path or a URL (beginning with http:// or ftp://). If the filter argument is used and no file is to be input to the filter then file can be omitted, NULL, NA or "".

sql

character string holding an SQL statement. The table representing the file should be referred to as file.

header

As in read.csv.

sep

As in read.csv.

row.names

As in read.csv.

eol

Character which ends line.

skip

Skip indicated number of lines in input file.

filter

If specified, this should be a shell/batch command that the input file is piped through. For read.csv2.sql it is by default the following on non-Windows systems: tr , .. This translates all commas in the file to dots. On Windows similar functionalty is provided but to do that using a vbscript file that is included with sqldf to emulate the tr command.

nrows

Number of rows used to determine column types. It defaults to 50. Using -1 causes it to use all rows for determining column types. This argument is rarely needed.

field.types

A list whose names are the column names and whose contents are the SQLite types (not the R class names) of the columns. Specifying these types improves how fast it takes. Unless speed is very important this argument is not normally used.

colClasses

As in read.csv.

dbname

As in sqldf except that the default is tempfile(). Specifying NULL will put the database in memory which may improve speed but will limit the size of the database by the available memory.

drv

This argument is ignored. Currently the only database SQLite supported by read.csv.sql and read.csv2.sql is SQLite. Note that the H2 database has a builtin SQL function, CSVREAD, which can be used in place of read.csv.sql.

...

Passed to sqldf.

Details

Reads the indicated file into an sql database creating the database if it does not already exist. Then it applies the sql statement returning the result as a data frame. If the database did not exist prior to this statement it is removed.

Note that it uses facilities of SQLite to read the file which are intended for speed and therefore not as flexible as in R. For example, it does not recognize quoted fields as special but will regard the quotes as part of the field. See the sqldf help for more information.

read.csv2.sql is like read.csv.sql except the default sep is ";" and the default filter translates all commas in the file to decimal points (i.e. to dots).

On Windows, if the filter argument is used and if Rtools is detected in the registry then the Rtools bin directory is added to the search path facilitating use of those tools without explicitly setting any the path.

Value

If the sql statement is a select statement then a data frame is returned.

Examples

1
2
3
4
5
6
7
8
## Not run: 
# might need to specify eol= too depending on your system
write.csv(iris, "iris.csv", quote = FALSE, row.names = FALSE)
iris2 <- read.csv.sql("iris.csv", 
	sql = "select * from file where Species = 'setosa' ")


## End(Not run)

Example output

Loading required package: gsubfn
Loading required package: proto
Loading required package: RSQLite
Warning message:
no DISPLAY variable so Tk is not available 
Warning message:
In .Internal(exists(x, envir, mode, inherits)) :
  closing unused connection 3 (iris.csv)

sqldf documentation built on May 2, 2019, 11:01 a.m.