| 3684 |
mrmanese |
1 |
.onLoad <- function(libname, pkgname) {
|
|
|
2 |
tryCatch({setwd(".SQLiteDF"); setwd("..")}, error=function (e) dir.create(".SQLiteDF"))
|
|
|
3 |
.Call("sdf_init_workspace")
|
|
|
4 |
}
|
| 3251 |
mrmanese |
5 |
|
| 3252 |
mrmanese |
6 |
.onUnload <- function(libpath) {
|
|
|
7 |
.Call("sdf_finalize_workspace")
|
|
|
8 |
library.dynam.unload("SQLiteDF", libpath)
|
|
|
9 |
}
|
|
|
10 |
|
| 3684 |
mrmanese |
11 |
sdf_tempdir <- function() .Call("sdf_tempdir")
|
| 3397 |
mrmanese |
12 |
# -------------------------------------------------------------------------
|
| 3252 |
mrmanese |
13 |
# workspace functions
|
| 3397 |
mrmanese |
14 |
# -------------------------------------------------------------------------
|
| 3446 |
mrmanese |
15 |
lsSdf <- function(pattern=NULL) .Call("sdf_list_sdfs", pattern)
|
|
|
16 |
getSdf <- function(name) .Call("sdf_get_sdf", name)
|
| 3252 |
mrmanese |
17 |
|
| 3255 |
mrmanese |
18 |
readTableSdf <- function(filename, sep=",", quote="\"'", name=NULL,
|
|
|
19 |
rownames, colnames)
|
| 3446 |
mrmanese |
20 |
.Call("sdf_import_table", filename, name, sep, quote, rownames, colnames)
|
| 3255 |
mrmanese |
21 |
|
|
|
22 |
attachSdf <- function(sdf_filename, sdf_iname=NULL)
|
| 3446 |
mrmanese |
23 |
invisible(.Call("sdf_attach_sdf", sdf_filename, sdf_iname))
|
|
|
24 |
detachSdf <- function(iname) .Call("sdf_detach_sdf", iname)
|
| 3255 |
mrmanese |
25 |
|
| 3397 |
mrmanese |
26 |
# -------------------------------------------------------------------------
|
| 3456 |
mrmanese |
27 |
# sqlite.vector functions
|
|
|
28 |
# -------------------------------------------------------------------------
|
| 3808 |
mrmanese |
29 |
sqlite.vector <- function(vec, name=NULL) {
|
|
|
30 |
if (!is.atomic(vec)) stop("vec is not an atomic vector")
|
|
|
31 |
tmp <- data.frame(V1=vec)
|
|
|
32 |
ret <- sqlite.data.frame(tmp, name)
|
|
|
33 |
ret$V1
|
|
|
34 |
}
|
| 3456 |
mrmanese |
35 |
typeSvec <- function(x) attr(x, "sdf.vector.type")
|
| 3457 |
mrmanese |
36 |
has.typeSvec <- function(x, type) {
|
| 3456 |
mrmanese |
37 |
if (inherits(x, "sqlite.vector")) typeSvec(x) == type else FALSE
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
# -------------------------------------------------------------------------
|
| 3307 |
mrmanese |
41 |
# sqlite.data.frame functions
|
| 3397 |
mrmanese |
42 |
# -------------------------------------------------------------------------
|
| 3419 |
mrmanese |
43 |
"sqlite.data.frame" <- function(x, name=NULL) {
|
|
|
44 |
if (inherits(x, "sqlite.data.frame")) x
|
|
|
45 |
else .Call("sdf_create_sdf", as.data.frame(x), name)
|
|
|
46 |
}
|
|
|
47 |
|
| 3307 |
mrmanese |
48 |
dupSdf <- function(sdf) {
|
| 3446 |
mrmanese |
49 |
if (!inherits(sdf, "sqlite.data.frame")) stop("Not a sqlite.data.frame.")
|
| 3307 |
mrmanese |
50 |
sdf[1:length(sdf)]
|
|
|
51 |
}
|
|
|
52 |
renameSdf <- function(sdf, name) {
|
| 3446 |
mrmanese |
53 |
if (!inherits(sdf, "sqlite.data.frame")) stop("Not a sqlite.data.frame.")
|
|
|
54 |
if (!is.character(name)) stop("name argument must be a string.")
|
|
|
55 |
.Call("sdf_rename_sdf", sdf, name)
|
| 3307 |
mrmanese |
56 |
}
|
| 3358 |
mrmanese |
57 |
inameSdf <- function(sdf) .Call("sdf_get_iname", sdf)
|
| 3307 |
mrmanese |
58 |
|
| 4160 |
mrmanese |
59 |
is.sqlite.data.frame <- function(x) inherits(x, "sqlite.data.frame")
|
|
|
60 |
|
| 3255 |
mrmanese |
61 |
# -------------------------------------------------------------------------
|
| 3419 |
mrmanese |
62 |
# sqlite.matrix functions
|
|
|
63 |
# -------------------------------------------------------------------------
|
| 3808 |
mrmanese |
64 |
.smat.fix.dimnames <- function(data.dimnames, data.dim=sapply(data.dimnames,length)) {
|
|
|
65 |
if (is.null(data.dimnames)) {
|
|
|
66 |
data.dimnames <- list(as.character(1:data.dim[1]), as.character(1:data.dim[2]))
|
|
|
67 |
} else {
|
|
|
68 |
if (is.null(data.dimnames[[1]]))
|
|
|
69 |
data.dimnames[[1]] <- as.character(1:data.dim[1])
|
|
|
70 |
if (is.null(data.dimnames[[2]]))
|
|
|
71 |
data.dimnames[[2]] <- as.character(1:data.dim[2])
|
|
|
72 |
}
|
|
|
73 |
data.dimnames
|
|
|
74 |
}
|
|
|
75 |
|
| 3419 |
mrmanese |
76 |
sqlite.matrix <- function(data, name=NULL) {
|
|
|
77 |
if (inherits(data, "sqlite.matrix")) data
|
|
|
78 |
else if (inherits(data, "sqlite.data.frame")) .Call("sdf_as_matrix", data, name)
|
| 3808 |
mrmanese |
79 |
else {
|
|
|
80 |
data <- as.matrix(data)
|
|
|
81 |
data.dim <- dim(data)
|
|
|
82 |
data.dimnames <- dimnames(data)
|
|
|
83 |
dim(data) <- NULL
|
|
|
84 |
vec <- sqlite.vector(data)
|
|
|
85 |
data.dimnames <- .smat.fix.dimnames(data.dimnames, data.dim)
|
|
|
86 |
if (typeSvec(vec) %in% c("factor", "ordered")) {
|
|
|
87 |
vec <- unclass(vec)
|
|
|
88 |
attr(vec, "levels") <- NULL
|
|
|
89 |
attr(vec, "sdf.vector.type") <- "character"
|
|
|
90 |
}
|
|
|
91 |
return(.Call("sdf_create_smat", vec, data.dimnames))
|
|
|
92 |
}
|
| 3419 |
mrmanese |
93 |
}
|
|
|
94 |
|
| 3855 |
mrmanese |
95 |
is.sqlite.matrix <- function(x) inherits(x, "sqlite.matrix")
|
|
|
96 |
|
| 3419 |
mrmanese |
97 |
# -------------------------------------------------------------------------
|
| 3397 |
mrmanese |
98 |
# external data functions
|
| 3358 |
mrmanese |
99 |
# -------------------------------------------------------------------------
|
| 3432 |
mrmanese |
100 |
sdfImportDBI <- function(con, sql, batch.size=2048, rownames="row_names", iname = NULL) {
|
| 3457 |
mrmanese |
101 |
on.exit(dbClearResult(rs))
|
|
|
102 |
rs <- dbSendQuery(con, sql)
|
|
|
103 |
df <- fetch(rs, batch.size)
|
| 3432 |
mrmanese |
104 |
if (length(rownames) > 1) stop("more than one column containing row names?")
|
|
|
105 |
|
|
|
106 |
if (is.numeric(rownames)) has_rn <- rownames
|
| 3457 |
mrmanese |
107 |
else if (is.character(rownames)) has_rn <- (1:length(df))[names(df) == rownames]
|
| 3432 |
mrmanese |
108 |
|
| 3446 |
mrmanese |
109 |
if (length(has_rn) == 0) has_rn <- FALSE
|
| 3432 |
mrmanese |
110 |
|
| 3397 |
mrmanese |
111 |
if (has_rn) {
|
| 3446 |
mrmanese |
112 |
rn <- df[,has_rn]; df <- df[,-has_rn]; row.names(df) <- rn
|
| 3397 |
mrmanese |
113 |
}
|
| 3457 |
mrmanese |
114 |
|
| 3446 |
mrmanese |
115 |
sdf <- sqlite.data.frame(df, iname)
|
| 3397 |
mrmanese |
116 |
rowname <- batch.size
|
|
|
117 |
while (! dbHasCompleted(rs)) {
|
| 3457 |
mrmanese |
118 |
df <- fetch(rs, batch.size)
|
| 3397 |
mrmanese |
119 |
if (has_rn) {
|
| 3446 |
mrmanese |
120 |
rn <- df[,has_rn]; df <- df[,-has_rn]; row.names(df) <- rn
|
| 3397 |
mrmanese |
121 |
}
|
| 3457 |
mrmanese |
122 |
rbindSdf(sdf, df)
|
| 3397 |
mrmanese |
123 |
}
|
| 3446 |
mrmanese |
124 |
sdf
|
| 3397 |
mrmanese |
125 |
}
|
| 3358 |
mrmanese |
126 |
|
| 3432 |
mrmanese |
127 |
sdfImportSQLite <- function(dbfilename, tablename, iname = tablename) {
|
| 3446 |
mrmanese |
128 |
.Call("sdf_import_sqlite_table", dbfilename, tablename, iname)
|
| 3397 |
mrmanese |
129 |
}
|
| 3358 |
mrmanese |
130 |
|
| 3419 |
mrmanese |
131 |
sdfImportText <- function(file, iname=NULL, sep="", quote="\"'", dec=".", as.is=FALSE,
|
|
|
132 |
na.strings="NA", colClasses=NA, skip=0, fill=!blank.lines.skip,
|
|
|
133 |
strip.white=FALSE, blank.lines.skip=FALSE, comment.char="#", allowEscapes=FALSE,
|
|
|
134 |
flush=FALSE, batch.size=2048) {
|
|
|
135 |
|
|
|
136 |
data <- read.table(file=file,sep=sep,quote=quote,dec=dec,as.is=as.is,na.strings=na.strings,
|
|
|
137 |
colClasses=colClasses,skip=skip,fill=fill,strip.white=strip.white,
|
|
|
138 |
blank.lines.skip=blank.lines.skip,comment.char=comment.char,
|
| 3446 |
mrmanese |
139 |
allowEscapes=allowEscapes,flush=flush,nrows=batch.size)
|
|
|
140 |
sdf <- sqlite.data.frame(data, iname)
|
| 3419 |
mrmanese |
141 |
|
|
|
142 |
if (length(colClasses) < length(data) || colClasses == NA)
|
|
|
143 |
colClasses <- sapply(data, function(x) class(x)[1]);
|
|
|
144 |
|
|
|
145 |
sskip <- skip;
|
|
|
146 |
while (nrow(data) == batch.size) {
|
| 3446 |
mrmanese |
147 |
sskip <- sskip + batch.size
|
| 3419 |
mrmanese |
148 |
data <- read.table(file=file,sep=sep,quote=quote,dec=dec,as.is=as.is,
|
| 3473 |
mrmanese |
149 |
na.strings=na.strings, colClasses=colClasses,skip=sskip,fill=fill,
|
| 3419 |
mrmanese |
150 |
strip.white=strip.white, blank.lines.skip=blank.lines.skip,
|
|
|
151 |
comment.char=comment.char, allowEscapes=allowEscapes,flush=flush,
|
| 3446 |
mrmanese |
152 |
nrows=batch.size)
|
| 3457 |
mrmanese |
153 |
rbindSdf(sdf, data)
|
| 3419 |
mrmanese |
154 |
}
|
| 3446 |
mrmanese |
155 |
sdf
|
| 3419 |
mrmanese |
156 |
}
|
|
|
157 |
|
| 4160 |
mrmanese |
158 |
sdfSelect <- function(sdf, select=NULL, where=NULL, limit=NULL, debug=FALSE) {
|
|
|
159 |
if (!is.sqlite.data.frame(sdf)) stop("sdf must be an sqlite.data.frame")
|
|
|
160 |
if (!is.null(limit)) limit = as.character(limit)
|
|
|
161 |
.Call("sdf_select", sdf, select, where, limit, debug)
|
|
|
162 |
}
|
|
|
163 |
|
| 3419 |
mrmanese |
164 |
|
| 3397 |
mrmanese |
165 |
# -------------------------------------------------------------------------
|
|
|
166 |
# overriden primitives
|
|
|
167 |
# -------------------------------------------------------------------------
|
| 3473 |
mrmanese |
168 |
ver = paste(R.version$major, R.version$minor[1], sep=".")
|
|
|
169 |
if (ver < "2.4.0") {
|
| 3456 |
mrmanese |
170 |
sort.default <- base::sort
|
|
|
171 |
sort <- function(x, ...) UseMethod("sort")
|
|
|
172 |
formals(sort.default) <- c(formals(sort.default), alist(...=))
|
| 3482 |
mrmanese |
173 |
|
|
|
174 |
# to make use of sort()
|
| 3473 |
mrmanese |
175 |
median <- function(x, na.rm=FALSE) as.numeric(quantile(x, 0.5, na.rm=na.rm))
|
| 3482 |
mrmanese |
176 |
|
|
|
177 |
# to get generic sort
|
|
|
178 |
environment(quantile.default) <- .GlobalEnv
|
| 3458 |
mrmanese |
179 |
}
|
| 3397 |
mrmanese |
180 |
|
|
|
181 |
|
| 3358 |
mrmanese |
182 |
# -------------------------------------------------------------------------
|
| 3397 |
mrmanese |
183 |
# biglm stuffs
|
|
|
184 |
# -------------------------------------------------------------------------
|
|
|
185 |
sdflm <- function(formula, sdf, batch.size=1024) {
|
| 3446 |
mrmanese |
186 |
n <- 1:batch.size
|
|
|
187 |
sdf.nrows <- nrow(sdf)
|
|
|
188 |
res <- biglm:::biglm(formula, sdf[n,])
|
|
|
189 |
n <- n + batch.size
|
| 3397 |
mrmanese |
190 |
while (n[1] < sdf.nrows) {
|
| 3446 |
mrmanese |
191 |
if (n[batch.size] > sdf.nrows) n <- n[1]:sdf.nrows
|
| 3700 |
mrmanese |
192 |
res <- biglm:::update(res, sdf[n,])
|
| 3446 |
mrmanese |
193 |
n <- n + batch.size
|
| 3397 |
mrmanese |
194 |
}
|
| 3446 |
mrmanese |
195 |
res
|
| 3397 |
mrmanese |
196 |
}
|
|
|
197 |
|
| 3821 |
mrmanese |
198 |
sdflm2 <- function(x, y, intercept=TRUE) {
|
|
|
199 |
if (!inherits(y, "sqlite.vector")) stop("y must be a sqlite.vector")
|
|
|
200 |
if (! attr(y, "sdf.vector.type") %in% c("numeric", "integer"))
|
|
|
201 |
stop("y must be a numeric sqlite.vector")
|
|
|
202 |
if (!inherits(x, "sqlite.data.frame")) stop("x must be a sqlite.data.frame")
|
|
|
203 |
if (! all(sapply(x, function (x) attr(x, "sdf.vector.type")) %in% c("numeric", "integer")))
|
|
|
204 |
stop("all columns of x must be numeric sqlite.vector-s")
|
|
|
205 |
if (nrow(x) != length(y)) stop("rows of x and length of y not equal")
|
|
|
206 |
|
|
|
207 |
rval <- .Call("sdf_do_biglm", x, y, dim(x), intercept)
|
|
|
208 |
if (intercept) rval$names <- c("(Intercept)", names(x))
|
|
|
209 |
else rval$names <- names(x)
|
|
|
210 |
rval$n <- nrow(x)
|
|
|
211 |
rval
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
|
| 3397 |
mrmanese |
215 |
# -------------------------------------------------------------------------
|
| 3252 |
mrmanese |
216 |
# S3 methods for sqlite.data.frame
|
| 3255 |
mrmanese |
217 |
# -------------------------------------------------------------------------
|
| 3446 |
mrmanese |
218 |
names.sqlite.data.frame <- function(x) .Call("sdf_get_names", x)
|
|
|
219 |
length.sqlite.data.frame <- function(x) .Call("sdf_get_length", x)
|
|
|
220 |
nrow.sqlite.data.frame <- function(x) .Call("sdf_get_row_count", x)
|
| 3358 |
mrmanese |
221 |
dim.sqlite.data.frame <- function(x)
|
| 3446 |
mrmanese |
222 |
c(nrow.sqlite.data.frame(x), length.sqlite.data.frame(x))
|
| 3281 |
mrmanese |
223 |
dimnames.sqlite.data.frame <- function(x) list(row.names(x), names(x))
|
| 3446 |
mrmanese |
224 |
"$.sqlite.data.frame" <- function(x, name) .Call("sdf_get_variable", x, name)
|
| 3255 |
mrmanese |
225 |
"[[.sqlite.data.frame" <- function(x, idx) {
|
| 3446 |
mrmanese |
226 |
if (length(idx) != 1) stop("index must be a 1-element vector.")
|
| 3255 |
mrmanese |
227 |
if (is.character(idx)) .Call("sdf_get_variable", x, idx)
|
|
|
228 |
else if (is.numeric(idx)) {
|
|
|
229 |
if (idx > length(x)) stop("subscript out of bounds")
|
|
|
230 |
else .Call("sdf_get_variable", x, names(x)[idx])
|
| 3446 |
mrmanese |
231 |
} else stop("don't know how to handle index.")
|
| 3255 |
mrmanese |
232 |
}
|
| 3471 |
mrmanese |
233 |
"[.sqlite.data.frame" <- function(x, row, col) {
|
|
|
234 |
Narg <- nargs()
|
|
|
235 |
if (Narg == 3) {
|
|
|
236 |
if (missing(row) && missing(col)) return(x) # x[,]
|
|
|
237 |
if (missing(row)) row = NULL
|
|
|
238 |
if (missing(col)) col = NULL
|
|
|
239 |
if (is.null(row) && is.null(col)) return(data.frame())
|
|
|
240 |
return(.Call("sdf_get_index", x, row, col, FALSE))
|
|
|
241 |
} else if (Narg == 2) {
|
|
|
242 |
if (missing(row)) return(x) # x[]
|
|
|
243 |
if (is.null(row)) return(data.frame())
|
|
|
244 |
return(.Call("sdf_get_index", x, NULL, row, TRUE))
|
|
|
245 |
}
|
| 3281 |
mrmanese |
246 |
}
|
| 3471 |
mrmanese |
247 |
|
| 3358 |
mrmanese |
248 |
as.list.sqlite.data.frame <- function(x, ...) {
|
| 3446 |
mrmanese |
249 |
ret <- list()
|
|
|
250 |
for (i in names(x)) ret[[i]] <- x[[i]]
|
|
|
251 |
ret
|
| 3358 |
mrmanese |
252 |
}
|
|
|
253 |
is.list.sqlite.data.frame <- function(x) FALSE;
|
| 3457 |
mrmanese |
254 |
rbindSdf <- function(sdf, df) {
|
|
|
255 |
.Call("sdf_rbind", sdf, df)
|
| 3358 |
mrmanese |
256 |
}
|
| 3432 |
mrmanese |
257 |
with.sqlite.data.frame <- function(data, expr, ...)
|
|
|
258 |
eval(substitute(expr), as.list(data), enclos=parent.frame())
|
| 3397 |
mrmanese |
259 |
as.data.frame.sqlite.data.frame <- function(x, ...) x
|
| 3419 |
mrmanese |
260 |
as.matrix.sqlite.data.frame <- function(x, ...) {
|
| 3446 |
mrmanese |
261 |
args <- as.list(...)
|
|
|
262 |
if ("name" %in% as.list) name <- args$name else name <- NULL
|
| 3419 |
mrmanese |
263 |
sqlite.matrix(x, name)
|
|
|
264 |
}
|
| 3457 |
mrmanese |
265 |
|
| 3456 |
mrmanese |
266 |
row.names.sqlite.data.frame <- function(x) attr(x, "sdf.row.names")
|
| 3457 |
mrmanese |
267 |
|
|
|
268 |
# row.names are overwritten with 1:n
|
| 3509 |
mrmanese |
269 |
head.sqlite.data.frame <- function(x, n = 6, ...) {
|
|
|
270 |
xrows <- nrow(x)
|
|
|
271 |
if (n > xrows) {
|
|
|
272 |
n <- min(6, xrows)
|
|
|
273 |
warning(paste("Number of rows specified exceeds the SDF's number of rows.",
|
|
|
274 |
"Trimming down to ", n, " rows", sep=""))
|
|
|
275 |
}
|
|
|
276 |
x[1:n,]
|
|
|
277 |
}
|
| 3457 |
mrmanese |
278 |
tail.sqlite.data.frame <- function(x, n = 6, ...) {
|
|
|
279 |
rows <- nrow(x); x[(rows-n+1):rows,]
|
|
|
280 |
}
|
|
|
281 |
|
| 3458 |
mrmanese |
282 |
|
| 3471 |
mrmanese |
283 |
print.sqlite.data.frame <- function(x, n = 6, ...) {
|
|
|
284 |
xdim <- dim(x)
|
|
|
285 |
xnames <- inameSdf(x)
|
| 3808 |
mrmanese |
286 |
n <- min(xdim[1], n)
|
| 3471 |
mrmanese |
287 |
cat(paste("SQLite data frame \"", xnames[1], "\" (",
|
| 3808 |
mrmanese |
288 |
xdim[1], " row(s) by ", xdim[2],
|
|
|
289 |
" column(s)) stored in file \"",
|
| 3471 |
mrmanese |
290 |
xnames[2], "\"\n\n", sep = ""))
|
|
|
291 |
cat(paste("First", n, "rows:\n"))
|
| 3509 |
mrmanese |
292 |
print(head(x, n, ...))
|
| 3471 |
mrmanese |
293 |
if (xdim[1] > n) cat(" ...\n")
|
|
|
294 |
}
|
| 3358 |
mrmanese |
295 |
|
| 3473 |
mrmanese |
296 |
summary.sqlite.data.frame <- function(object, maxsum=7, digits=max(3, getOption("digits")-3), ...)
|
|
|
297 |
base:::summary.data.frame(object, maxsum, digits, ...)
|
| 3358 |
mrmanese |
298 |
|
| 3255 |
mrmanese |
299 |
# -------------------------------------------------------------------------
|
|
|
300 |
# S3 methods for sqlite.vector
|
|
|
301 |
# -------------------------------------------------------------------------
|
|
|
302 |
"[.sqlite.vector" <- function(x, idx) {
|
|
|
303 |
# temporary, better to be in C because assumption is length(x) is large
|
| 3446 |
mrmanese |
304 |
if (is.numeric(idx) && all(idx <= 0)) idx <- (1:length(x))[idx]
|
|
|
305 |
.Call("sdf_get_variable_index", x, idx)
|
| 3255 |
mrmanese |
306 |
}
|
| 4590 |
mrmanese |
307 |
|
|
|
308 |
"[<-.sqlite.vector" <- function(x, idx, value) {
|
|
|
309 |
.Call("sdf_set_variable_index", x, idx, value)
|
|
|
310 |
}
|
| 3446 |
mrmanese |
311 |
length.sqlite.vector <- function(x) .Call("sdf_get_variable_length", x)
|
| 3473 |
mrmanese |
312 |
is.list.sqlite.vector <- function(x) FALSE
|
| 3255 |
mrmanese |
313 |
# methods to "coerce" to ordinary vectors
|
| 3432 |
mrmanese |
314 |
as.double.sqlite.vector <- function(x, ...) as.double(x[1:length(x)])
|
|
|
315 |
as.character.sqlite.vector <- function(x, ...) as.character(x[1:length(x)])
|
|
|
316 |
as.logical.sqlite.vector <- function(x, ...) as.logical(x[1:length(x)])
|
|
|
317 |
as.integer.sqlite.vector <- function(x, ...) as.integer(x[1:length(x)])
|
| 3307 |
mrmanese |
318 |
Math.sqlite.vector <- function(x, ...) {
|
| 3457 |
mrmanese |
319 |
if (any(has.typeSvec(x, "factor"), has.typeSvec(x, "ordered")))
|
| 3446 |
mrmanese |
320 |
stop(paste(.Generic, "not meaningful for factors"))
|
| 3457 |
mrmanese |
321 |
if (!any(has.typeSvec(x, "numeric"), has.typeSvec(x, "integer")))
|
| 3446 |
mrmanese |
322 |
stop("Non-numeric argument to mathematical function")
|
| 3307 |
mrmanese |
323 |
#.Generic
|
| 4599 |
mrmanese |
324 |
other.args <- formals(args(get(.Generic, mode="function")))[-1]
|
| 3446 |
mrmanese |
325 |
extra.args <- list(...)
|
| 3434 |
mrmanese |
326 |
|
|
|
327 |
# "union" of list elements, with values in extra.args taking precedence
|
|
|
328 |
# to get default values if missing.
|
|
|
329 |
# there is some "magic" with Math group functions: they already perform
|
|
|
330 |
# checking on number of args, ... is passed without the original param names,
|
|
|
331 |
# and even if you do round(digits=3,5.23512) ... will be list(3)
|
|
|
332 |
if (length(extra.args) > 0) other.args[1:length(extra.args)] <- extra.args
|
|
|
333 |
|
|
|
334 |
# as of 2.4.0, the most # of args in any of the func under Math is 2.
|
|
|
335 |
# the 2nd arg is tricky, since it can be a vector > 1 then we'd have
|
|
|
336 |
# to take care of recycling etc. simplify by allowing only scalars
|
|
|
337 |
# as 2nd arg.
|
|
|
338 |
if (length(other.args) > 0) {
|
| 3446 |
mrmanese |
339 |
argnames <- names(other.args)
|
| 4599 |
mrmanese |
340 |
|
| 3434 |
mrmanese |
341 |
if (is.call(other.args[[argnames[1]]]))
|
| 3446 |
mrmanese |
342 |
other.args[[argnames[1]]] <- eval(other.args[[argnames[1]]])
|
| 3434 |
mrmanese |
343 |
if (length(other.args[[argnames[1]]]) > 1)
|
| 3446 |
mrmanese |
344 |
stop(paste("non scalar", argnames[1], "is not supported"))
|
| 3434 |
mrmanese |
345 |
if (is.null(other.args[[argnames[1]]]))
|
| 3446 |
mrmanese |
346 |
stop(paste("NULL", argnames[1], "is not supported"))
|
| 3434 |
mrmanese |
347 |
}
|
| 4599 |
mrmanese |
348 |
|
| 3446 |
mrmanese |
349 |
ret <- .Call("sdf_do_variable_math", .Generic, x, other.args)
|
|
|
350 |
if (is.character(ret)) { file.remove(ret); ret <- NULL }
|
| 3307 |
mrmanese |
351 |
ret;
|
|
|
352 |
}
|
| 3434 |
mrmanese |
353 |
|
| 3458 |
mrmanese |
354 |
Summary.sqlite.vector <- function(x, ..., na.rm=F) {
|
| 3457 |
mrmanese |
355 |
if (!any(has.typeSvec(x, "numeric"), has.typeSvec(x, "integer"), has.typeSvec(x, "logical")))
|
| 3446 |
mrmanese |
356 |
stop("Non-numeric argument")
|
| 3324 |
mrmanese |
357 |
ret <- .Call("sdf_do_variable_summary", .Generic, x, as.logical(na.rm))
|
| 3446 |
mrmanese |
358 |
if (is.character(ret)) { file.remove(ret); ret <- NULL }
|
|
|
359 |
ret
|
| 3324 |
mrmanese |
360 |
}
|
| 3358 |
mrmanese |
361 |
Ops.sqlite.vector <- function(e1, e2) {
|
| 3457 |
mrmanese |
362 |
if (any(has.typeSvec(e1, "factor"), has.typeSvec(e2, "factor"),
|
| 3456 |
mrmanese |
363 |
inherits(e1, "factor"), inherits(e2, "factor")))
|
| 3446 |
mrmanese |
364 |
stop("not meaningful for factors")
|
|
|
365 |
arg.reversed <- FALSE
|
| 3434 |
mrmanese |
366 |
if (!inherits(e1, "sqlite.vector")) {
|
|
|
367 |
tmp <- e1; e1 <- e2; e2 <- tmp; arg.reversed = TRUE;
|
|
|
368 |
}
|
| 3808 |
mrmanese |
369 |
# if e2 is not sqlite.vector nor atomic vector, come what may
|
| 3446 |
mrmanese |
370 |
.Call("sdf_do_variable_op", .Generic, e1, e2, arg.reversed)
|
| 3358 |
mrmanese |
371 |
}
|
| 3456 |
mrmanese |
372 |
|
| 3358 |
mrmanese |
373 |
sort.sqlite.vector <- function(x, decreasing=FALSE, ...) {
|
|
|
374 |
.Call("sdf_sort_variable", x, as.logical(decreasing))
|
|
|
375 |
}
|
| 3473 |
mrmanese |
376 |
|
|
|
377 |
#quantile.sqlite.vector <- function(x, probs=seq(0,1,0.25), names=FALSE,
|
|
|
378 |
# na.rm=FALSE, type=7, ...) NextMethod()
|
|
|
379 |
|
| 3432 |
mrmanese |
380 |
summary.sqlite.vector <- function(object, maxsum=100, digits=max(3, getOption("digits")-3), ...) {
|
| 3473 |
mrmanese |
381 |
if (has.typeSvec(object, "factor") || has.typeSvec(object, "ordered") || has.typeSvec(object, "logical"))
|
| 3432 |
mrmanese |
382 |
.Call("sdf_variable_summary", object, as.integer(maxsum))
|
| 3473 |
mrmanese |
383 |
else if (has.typeSvec(object, "numeric") || has.typeSvec(object, "integer")) {
|
|
|
384 |
# copied from summary.default
|
|
|
385 |
qq <- quantile(object)
|
|
|
386 |
qq <- signif(c(qq[1:3], mean(object), qq[4:5]), digits)
|
|
|
387 |
names(qq) <- c("Min.", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.")
|
|
|
388 |
class(qq) <- "table"
|
|
|
389 |
qq
|
|
|
390 |
} else if (has.typeSvec(object, "character")) {
|
|
|
391 |
ret <- c(as.character(length(object)), "sqlite.vector", "character")
|
|
|
392 |
names(ret) <- c("Length", "Class", "Type")
|
|
|
393 |
class(ret) <- "table"
|
|
|
394 |
ret
|
| 3700 |
mrmanese |
395 |
} else stop(paste("not implemented for type ", typeSvec(object), sep=""))
|
| 3419 |
mrmanese |
396 |
}
|
|
|
397 |
|
| 3473 |
mrmanese |
398 |
mean.sqlite.vector <- function(x, ...) {
|
|
|
399 |
if (!(has.typeSvec(x, "numeric") || has.typeSvec(x, "integer") || has.typeSvec(x, "logical"))) {
|
|
|
400 |
warning("argument is not numeric or logical: returning NA")
|
|
|
401 |
return(as.numeric(NA))
|
|
|
402 |
}
|
|
|
403 |
sum(x) / length(x)
|
|
|
404 |
}
|
|
|
405 |
|
| 3855 |
mrmanese |
406 |
is.sqlite.vector <- function(x) inherits(x, "sqlite.vector")
|
| 3458 |
mrmanese |
407 |
|
|
|
408 |
all.equal.sqlite.vector <- function(target, current, batch.size=1024, ...) {
|
|
|
409 |
len <- length(target)
|
|
|
410 |
if (len != length(current))
|
|
|
411 |
return(paste("sqlite.vector: lengths(", len, ", ", length(current),
|
|
|
412 |
") differ", sep=""))
|
|
|
413 |
batch.all.equal <- function(x, y) {
|
|
|
414 |
i <- 1;
|
|
|
415 |
while (i < len) {
|
|
|
416 |
last <- min(i+batch.size, len)
|
| 3471 |
mrmanese |
417 |
if (!isTRUE(all.equal(target[i:last], current[i:last], ...)))
|
| 3458 |
mrmanese |
418 |
return(FALSE)
|
|
|
419 |
i <- i + batch.size
|
|
|
420 |
}
|
|
|
421 |
return(TRUE)
|
|
|
422 |
}
|
|
|
423 |
if (is.sqlite.vector(target) & is.sqlite.vector(current)) {
|
|
|
424 |
if (typeSvec(target) != typeSvec(current))
|
|
|
425 |
return(paste("target is ", typeSvec(target), " sqlite.vector, current is ",
|
|
|
426 |
typeSvec(current), " sqlite.vector", sep=""))
|
|
|
427 |
# not the most efficient, but the quickest to code
|
|
|
428 |
return(batch.all.equal(target, current))
|
|
|
429 |
} else if (typeSvec(target) == class(current)[1]) {
|
|
|
430 |
return(batch.all.equal(target, current))
|
|
|
431 |
}
|
|
|
432 |
return(FALSE)
|
|
|
433 |
}
|
|
|
434 |
|
| 3471 |
mrmanese |
435 |
print.sqlite.vector <- function(x, n = 6, ...) {
|
|
|
436 |
xdim <- length(x)
|
|
|
437 |
xlist <- as.list(x)
|
|
|
438 |
xnames <- inameSdf(x)
|
| 3808 |
mrmanese |
439 |
n <- min(xdim, n)
|
| 3471 |
mrmanese |
440 |
cat(paste("SQLite vector (",
|
|
|
441 |
xdim, " elements)",
|
|
|
442 |
" of type ", typeSvec(x), "\n",
|
|
|
443 |
"Column \"", xlist$varname, "\" in SQLite data frame \"",
|
|
|
444 |
xnames[1], "\" stored in file \"",
|
|
|
445 |
xnames[2], "\"\n\n", sep = ""))
|
|
|
446 |
cat(paste("First", n, "elements:\n"))
|
|
|
447 |
print(x[1:n], ...)
|
|
|
448 |
if (xdim > n) cat(" ...\n")
|
|
|
449 |
}
|
| 3419 |
mrmanese |
450 |
# -------------------------------------------------------------------------
|
|
|
451 |
# S3 methods for sqlite.matrix
|
|
|
452 |
# -------------------------------------------------------------------------
|
| 3446 |
mrmanese |
453 |
length.sqlite.matrix <- function(x) .Call("sdf_get_variable_length", x)
|
| 3509 |
mrmanese |
454 |
dim.sqlite.matrix <- function(x) attr(x, "sdf.dim") # nrow(), ncol()
|
|
|
455 |
dimnames.sqlite.matrix <- function(x) attr(x, "sdf.dimnames") # rownames(), colnames()
|
|
|
456 |
head.sqlite.matrix <- function(x, n=6, ...) {
|
|
|
457 |
mdim <- dim(x)
|
|
|
458 |
mrows <- mdim[1]
|
|
|
459 |
if (n > mrows) {
|
|
|
460 |
n <- min(6, mrows)
|
|
|
461 |
warning(paste("Number of rows specified exceeds the SDF's number of rows.",
|
|
|
462 |
"Trimming down to ", n, " rows", sep=""))
|
|
|
463 |
}
|
|
|
464 |
start.idx <- seq(1, mdim[1]*mdim[2], by=mdim[1])
|
|
|
465 |
stopifnot(length(start.idx) == mdim[2])
|
|
|
466 |
idx <- sapply(start.idx, function(x) x:(x+n-1))
|
|
|
467 |
ret <- .Call("sdf_get_variable_index", x, idx)
|
|
|
468 |
if (is.null(ret)) return(invisible(NULL))
|
|
|
469 |
ret <- matrix(ret, n, mdim[2])
|
|
|
470 |
colnames(ret) <- colnames(x)
|
| 3684 |
mrmanese |
471 |
rownames(ret) <- rownames(x)[1:n]
|
| 3509 |
mrmanese |
472 |
ret
|
|
|
473 |
}
|
|
|
474 |
print.sqlite.matrix <- function(x, n = 6, ...) {
|
|
|
475 |
xdim <- dim(x)
|
|
|
476 |
xnames <- inameSdf(x)
|
|
|
477 |
cat(paste("SQLite matrix \"", xnames[1], "\" (",
|
|
|
478 |
xdim[1], " rows by ", xdim[2],
|
| 3808 |
mrmanese |
479 |
" column(s)) stored in file \"",
|
| 3509 |
mrmanese |
480 |
xnames[2], "\"\n\n", sep = ""))
|
|
|
481 |
cat(paste("First", n, "rows:\n"))
|
|
|
482 |
print(head(x, n, ...))
|
|
|
483 |
if (xdim[1] > n) cat(" ...\n")
|
|
|
484 |
}
|
| 3808 |
mrmanese |
485 |
"[.sqlite.matrix" <- function(x, row, col) {
|
|
|
486 |
Narg <- nargs()
|
|
|
487 |
Ncol <- ncol(x)
|
|
|
488 |
Nrow <- nrow(x)
|
|
|
489 |
return.matrix <- function(x, row, col) {
|
|
|
490 |
ncolx <- length(col); nrowx <- length(row)
|
|
|
491 |
idxcol <- Nrow * (col - 1) # base-0 index of 1st column elems
|
|
|
492 |
idx <- as.numeric(sapply(idxcol, function(x) x + row))
|
|
|
493 |
ret <- .Call("sdf_get_variable_index", x, idx)
|
|
|
494 |
if (ncolx > 1 && nrowx > 1) {
|
|
|
495 |
ret <- matrix(x[idx], nrow=length(row), ncolx)
|
|
|
496 |
colnames(ret) <- colnames(x)[col]
|
|
|
497 |
rownames(ret) <- rownames(x)[row]
|
|
|
498 |
} else if (ncolx > 1) names(ret) <- colnames(x)[col]
|
|
|
499 |
else if (nrowx > 1) names(ret) <- row
|
|
|
500 |
|
|
|
501 |
return(ret)
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
if (Narg == 3) {
|
|
|
505 |
if (missing(row) && missing(col)) return(x) # x[,]
|
|
|
506 |
if (missing(row)) return(.Call("sdf_get_matrix_columns", x, col)) # x[,m]
|
|
|
507 |
if (missing(col)) return(return.matrix(x, row, 1:Ncol)) # x[n,]
|
|
|
508 |
if (is.null(row) && is.null(col)) return (matrix(nrow=0,ncol=0))
|
|
|
509 |
return(return.matrix(x, row, col)) # x[n,m]
|
|
|
510 |
} else if (Narg == 2) {
|
|
|
511 |
if (missing(row)) return(x) # x[]
|
|
|
512 |
if (is.null(row)) return(numeric(0)) # x[NULL]
|
|
|
513 |
return(.Call("sdf_get_variable_index", x, row)) # x[n]
|
|
|
514 |
}
|
|
|
515 |
}
|
|
|
516 |
|
|
|
517 |
Ops.sqlite.matrix <- function(e1, e2) {
|
|
|
518 |
arg.reversed <- FALSE
|
|
|
519 |
if (!inherits(e1, "sqlite.matrix")) {
|
|
|
520 |
tmp <- e1; e1 <- e2; e2 <- tmp; arg.reversed = TRUE;
|
|
|
521 |
}
|
|
|
522 |
if (!all(dim(e1) == dim(e2))) stop("non-conformable arrays")
|
|
|
523 |
ret <- Ops.sqlite.vector(e1, e2)
|
|
|
524 |
if (arg.reversed) {
|
|
|
525 |
if (is.atomic(e2)) {
|
|
|
526 |
e2.dimnames <- .smat.fix.dimnames(dimnames(e2), dim(e2))
|
|
|
527 |
return(.Call("sdf_create_smat", ret, e2.dimnames))
|
|
|
528 |
} else if (is.sqlite.matrix(e2)) {
|
|
|
529 |
return(.Call("sdf_create_smat", ret, dimnames(e2)))
|
|
|
530 |
}
|
| 4160 |
mrmanese |
531 |
} else return(.Call("sdf_create_smat", ret, dimnames(e1)))
|
| 3808 |
mrmanese |
532 |
}
|
| 3684 |
mrmanese |
533 |
|
| 3821 |
mrmanese |
534 |
|
|
|
535 |
# -------------------------------------------------------------------------
|
|
|
536 |
# S3 methods for sdflm
|
|
|
537 |
# -------------------------------------------------------------------------
|
|
|
538 |
#coef.sdflm <- function(object, ...) {
|
|
|
539 |
# rval <- biglm:::coef.biglm(object, ...)
|
|
|
540 |
# names(rval) <- names(rval$X)
|
|
|
541 |
# rval
|
|
|
542 |
#}
|
|
|
543 |
|
|
|
544 |
update.sdflm <- function(object, moredata, ...) {
|
| 3855 |
mrmanese |
545 |
stop("not updatable")
|
| 3821 |
mrmanese |
546 |
}
|