Learn R Programming

PivotalR (version 0.1.18.5)

preview: Read the actual data stored in a table of database.

Description

These functions read the actual data from a database table or operation, returning a data.frame or other object as appropriate. lookat and lk are actually the same.

Usage

lookat(x, nrows = 100, array = TRUE, conn.id = 1, drop = TRUE)

lk(x, nrows = 100, array = TRUE, conn.id = 1, drop = TRUE)

# S3 method for db.table as.data.frame(x, row.names = NULL, optional = FALSE, nrows = NULL, stringsAsFactors = default.stringsAsFactors(), array = TRUE, ...)

# S3 method for db.view as.data.frame(x,row.names=NULL, optional=FALSE, nrows = NULL, stringsAsFactors = default.stringsAsFactors(), array = TRUE, ...)

# S3 method for db.Rquery as.data.frame(x, row.names = NULL, optional = FALSE, nrows = NULL, stringsAsFactors = default.stringsAsFactors(), array = TRUE, ...)

Arguments

x

A '>db.data.frame (includes '>db.table and '>db.view) object, which points to a table or view in the database; or a '>db.Rquery object, which represents some operations on a '>db.data.frame object. If x is a string, which means a table name, this function directly reads data from the table without having to wrap it with a db.data.frame class object.

conn.id

An integer, the ID of the connection where the table resides.

nrows

An integer, how many rows of data to retrieve. If it is NULL or "all" or a non-positive value, then all data in the table will be send into R. Be careful, you do not want to do this if the data table is very large.

array

Logical, default is TRUE. This decides how to parse columns that have array as their elements. When TRUE, each element in the array is extracted and put into a new column. Otherwise, the array is read in as a string.

stringsAsFactors

Logical, whether character variables should be converted to factors.

drop

Whether to coerce single-column tables to a vector of the appropriate class.

row.names,optional,...

For compatibility with the as.data.frame generic; not used.

Value

For db.data.frame and db.Rquery objects, a data frame. Each column in the table becomes a column of the returned data.frame. A column of arrays is converted into a column of strings, see arraydb.to.arrayr for more details. Single-column tables created with lookat or lk will be coerced to a vector if drop == TRUE.

For db.Rcrossprod objects, a matrix or list of matrix objects as appropriate (see above).

Details

When x is a '>db.data.frame object, this function reads the data in a table or view in the connected database.

When x is a '>db.Rquery object, this function reads the result of some operations on a '>db.data.frame object.

When x is a '>db.Rcrossprod object, this function output a matrix to R. If the matrix is symmetric, it is returned as '>dspMatrix. Otherwise, it is returned as '>dgeMatrix. If there are multiple matrices in x, a list is returned and each element of the list is a matrix.

The as.data.frame method calls lookat with nrows = NULL to perform the conversion to a data frame. In this case drop is set to FALSE, ie the result will always be a data frame.

See Also

arraydb.to.arrayr convert strings extracted form database into arrays.

Examples

Run this code
# NOT RUN {
<!-- %% @test .port Database port number -->
<!-- %% @test .dbname Database name -->
## set up the database connection
## Assume that .port is port number and .dbname is the database name
cid <- db.connect(port = .port, dbname = .dbname, verbose = FALSE)

## create a table from the example data.frame "abalone"
x <- as.db.data.frame(abalone, conn.id = cid, verbose = FALSE)

## preview of a table
lk(x, nrows = 10) # extract 10 rows of data

## do some operations and preview the result
y <- (x[,1:2] + 1.2) * 2
lk(y, 20)

## table abalone has a column named "id"
lk(sort(x, INDICES = x$id), 20) # the preview is ordered by "id" value

## use as.data.frame
as.data.frame(x, 10)

db.disconnect(cid, verbose = FALSE)
# }

Run the code above in your browser using DataLab