| 3251 |
mrmanese |
1 |
\name{sqlite.data.frame}
|
|
|
2 |
\alias{sqlite.data.frame}
|
| 3281 |
mrmanese |
3 |
\alias{sdf}
|
|
|
4 |
\title{SQLite Data Frame}
|
| 3509 |
mrmanese |
5 |
\description{ Creates an Sqlite Data Frame (SDF) from ordinary data frames. }
|
| 3251 |
mrmanese |
6 |
\usage{
|
| 3281 |
mrmanese |
7 |
sqlite.data.frame(x, name=NULL)
|
| 3251 |
mrmanese |
8 |
}
|
| 3324 |
mrmanese |
9 |
\arguments{
|
|
|
10 |
\item{x}{The object to be coerced into a data frame which is then stored in a
|
|
|
11 |
SQLite database. \code{as.data.frame} is called first on x before creating
|
|
|
12 |
the SDF database. }
|
|
|
13 |
\item{name}{The internal name of the SDF. If none is provided, a generic
|
|
|
14 |
name \emph{data}<n> is used (e.g. data1, data2, etc). Each SDF should have a unique
|
|
|
15 |
internal name and also be a valid R symbol. Numbers are appended to names in case of duplicates,
|
|
|
16 |
e.g. if name arg is \emph{iris}, and it already exists, then the
|
|
|
17 |
new SDF will have a name \emph{iris1}. If it still exists, then the name
|
|
|
18 |
will be \emph{iris2}, and so on. }
|
|
|
19 |
}
|
| 3251 |
mrmanese |
20 |
\details{
|
| 3324 |
mrmanese |
21 |
SQLite data frames (SDF's) are data frames whose data are stored in a SQLite database.
|
|
|
22 |
SQLite is an open source, powerful (considering its size), light weight data base engine.
|
|
|
23 |
It stores each database (composed of tables, indices, etc.) in a single file. Since a
|
|
|
24 |
single SDF occupies a whole database, each SDF will be contained in a single file.
|
| 3251 |
mrmanese |
25 |
|
| 3324 |
mrmanese |
26 |
Each SDF file contains the following tables:
|
|
|
27 |
\itemize{
|
| 3434 |
mrmanese |
28 |
\item{sdf\_attributes}{a key-value table that contains the SDF attributes. Currently,
|
| 3324 |
mrmanese |
29 |
only \emph{name} is used representing the SDF's internal name.}
|
| 3434 |
mrmanese |
30 |
\item{sdf\_data}{contains the actual data. Factors and ordered variables are stored as
|
| 3324 |
mrmanese |
31 |
integers. Their levels are stored in other tables. Numeric (real) are stored as double,
|
|
|
32 |
characters as text and integers as int's. Currently, complex numbers are not supported.
|
|
|
33 |
Column names correspond exactly to the variable names of the ordinary data frames. E.g.
|
|
|
34 |
Petal.Length will have a column name Petal.Length in the table. This is possible
|
|
|
35 |
because SQLite allows almost any kind of column name as long as it is quoted by
|
|
|
36 |
square brakets ([ ]). You're on your own if you try to be a smartass on this. Also,
|
|
|
37 |
an extra column named \emph{row name} (with the space between the words), of type
|
| 3471 |
mrmanese |
38 |
text is used to store the data frame row names and is set as the table's primary key.
|
| 3324 |
mrmanese |
39 |
So please don't use \emph{row name} as a variable name.}
|
|
|
40 |
\item{[factor <colname>] and [ordered <colname>]}{stores the levels and level labels
|
|
|
41 |
for each factor variable in the SDF. One such table will be created for every
|
|
|
42 |
factor or ordered var, even if two variables share the same level labels. Besides
|
|
|
43 |
storing the level data, it is used to mark a column as being a factor.}
|
| 3251 |
mrmanese |
44 |
}
|
| 3324 |
mrmanese |
45 |
|
|
|
46 |
SDF's are managed in a workspace separate from R's. When SQLiteDF is loaded, it searches
|
| 3684 |
mrmanese |
47 |
for the file \code{workspace.db} inside the subdirectory \code{.SQLiteDF} in the current
|
|
|
48 |
working directory. This file contains
|
| 3324 |
mrmanese |
49 |
a list of SDF's created/used in the previous session (i.e. SQLiteDF sessions are automatically
|
|
|
50 |
saved), including their full and relative path and attach information.
|
|
|
51 |
Workspace is managed using the SQLite engine
|
|
|
52 |
by opening \code{workspace.db} as the main database and then attaching (SQLite's attach) the SDF's.
|
|
|
53 |
Unfortunately, the number of attached databases is limited to 31 (actually 32, but 1 is reserved
|
|
|
54 |
for the temp db). Therefore, SDF's are \emph{scored} according to the number of times
|
|
|
55 |
it has been used. When the maximum allowed attachment is reached, the least used attached
|
| 3684 |
mrmanese |
56 |
SDF's is detached and the needed one is attached in its place. On compiling the package, the
|
|
|
57 |
configure script modifies the bundled SQLite source such that
|
|
|
58 |
constant controlling the maximum attachments is modified to 31 (default is 10).
|
| 3324 |
mrmanese |
59 |
|
|
|
60 |
Back to when SQLiteDF is loaded, after opening \code{workspace.db}, the SDF's stored in the list
|
|
|
61 |
are sorted according to their number of uses in the previous session and then the
|
| 3684 |
mrmanese |
62 |
first 30 are attached. The relative path is used for finding the SQLite file. If the file
|
| 3324 |
mrmanese |
63 |
cannot be found, it is deleted from the SQLiteDF workspace (with a warning message). The
|
|
|
64 |
scores are then all reset.
|
|
|
65 |
|
| 3358 |
mrmanese |
66 |
A sqlite.data.frame object is a list a single element:
|
| 3324 |
mrmanese |
67 |
\itemize{
|
|
|
68 |
\item{iname}{the internal name of the SDF.}
|
| 3358 |
mrmanese |
69 |
}
|
|
|
70 |
and the following attributes:
|
|
|
71 |
\itemize{
|
|
|
72 |
\item{class}{The S3 class vector \code{c("sqlite.data.frame", "data.frame")}}
|
| 3324 |
mrmanese |
73 |
\item{row.names}{A sqlite.vector of mode character containing the row names of the SDF}
|
|
|
74 |
}
|
| 3684 |
mrmanese |
75 |
|
|
|
76 |
All SDF's created in the session will have their SQLite file stored in the subdirectory .SQLiteDF in the
|
|
|
77 |
current working directory. SDF's created in the other session can be imported/attached
|
|
|
78 |
to the current SDF workspace using \code{attachSdf}, which may reside anywhere in the
|
|
|
79 |
file system.
|
| 3324 |
mrmanese |
80 |
}
|
| 3251 |
mrmanese |
81 |
\value{
|
| 3324 |
mrmanese |
82 |
A S3 object representing the SDF. The SDF database will be created in the same directory
|
|
|
83 |
with file name derived by appending the extension \emph{db} to the passed internal
|
|
|
84 |
name, or the default internal name if none is provided.
|
| 3251 |
mrmanese |
85 |
}
|
|
|
86 |
\author{Miguel A. R. Manese}
|
| 3281 |
mrmanese |
87 |
\note{
|
| 3324 |
mrmanese |
88 |
The full path is used to avoid attaching the same db which may have different relative path
|
| 3471 |
mrmanese |
89 |
after the user changes directory after loading SQLiteDF (see \code{attachSdf}).
|
| 3251 |
mrmanese |
90 |
}
|
| 3324 |
mrmanese |
91 |
\seealso{
|
|
|
92 |
\code{\link[SQLiteDF]{lsSdf}}
|
|
|
93 |
\code{\link[SQLiteDF]{getSdf}}
|
|
|
94 |
\code{\link[SQLiteDF]{attachSdf}}
|
|
|
95 |
\code{\link[SQLiteDF]{detachSdf}}
|
|
|
96 |
}
|
| 3251 |
mrmanese |
97 |
\examples{
|
| 3282 |
mrmanese |
98 |
library(datasets)
|
| 3251 |
mrmanese |
99 |
iris.sdf <- sqlite.data.frame(iris)
|
| 3281 |
mrmanese |
100 |
names(iris.sdf)
|
|
|
101 |
class(iris.sdf)
|
| 3282 |
mrmanese |
102 |
iris.sdf$Petal.Length[1:10]
|
| 3281 |
mrmanese |
103 |
iris.sdf[["Petal.Length"]][1:10]
|
| 3282 |
mrmanese |
104 |
iris.sdf[1,c(TRUE,FALSE)]
|
| 3251 |
mrmanese |
105 |
#apply(iris.sdf[1:4], 2, mean)
|
|
|
106 |
}
|
| 3434 |
mrmanese |
107 |
\keyword{data}
|
|
|
108 |
\keyword{manip}
|
|
|
109 |
\keyword{classes}
|