1
1
".rk.get.meta" <- function (x) {
2
c (row.names (attr (x, ".rk.meta")), as.vector (attr (x, ".rk.meta")[[1]]), recursive=TRUE)
5
".rk.set.meta" <- function (x, l, m) {
6
eval (substitute (attr (x, ".rk.meta") <<- data.frame (d=m, row.names=l)))
9
".rk.editor.opened" <- function (x) {
10
if (!exists (".rk.editing")) .rk.editing <<- c ()
11
.rk.editing <<- c (.rk.editing, deparse (substitute (x)))
14
".rk.editor.closed" <- function (x) {
15
if (exists (".rk.editing")) .rk.editing <<- .rk.editing[.rk.editing != deparse (substitute (x))]
18
".rk.classify" <- function (x) {
20
if (is.data.frame (x)) type = type + 1
21
if (is.matrix (x)) type = type + 2
22
if (is.array (x)) type = type + 4
23
if (is.list (x)) type = type + 8
24
if (type != 0) type = type + 16 else type = 32
25
if (is.function (x)) type = type + 128
26
if (!is.null (attr (x, ".rk.meta"))) type = type + 256
28
if (length (d) < 1) d <- length (x); # handling for objects that according to R do not have a dimension (such as vectors, functions, etc.)
32
".rk.get.type" <- function (x) {
33
if (is.data.frame (x) || is.matrix (x) || is.array (x) || is.list (x)) return (1) # container
34
if (is.function (x)) return (2) # function
35
if (is.vector (x)) return (3) # a vector/variable
36
return (4) # something else
2
y <- attr (x, ".rk.meta");
3
c (names (y), as.character (y))
6
".rk.set.meta" <- function (x, m) {
7
eval (substitute (attr (x, ".rk.meta") <<- m))
10
".rk.set.invalid.field" <- function (x, r, d) {
11
l <- attr (x, ".rk.invalid.fields");
12
if (is.null (l)) l <- list ();
13
l[[as.character(r)]] <- d;
14
eval (substitute (attr (x, ".rk.invalid.fields") <<- l))
39
17
".rk.data.frame.insert.row" <- function (x, index=0) {
106
84
return(c(as.vector(H$topic),as.vector(H$title),as.vector(H$Package)))
87
# This function works like available.packages (with no arguments), but does simple caching of the result, and of course uses a cache if available. Cache is only used, if it is less than 1 hour old, and options("repos") is unchanged.
88
".rk.cached.available.packages" <- function () {
90
if (exists (".rk.available.packages.cache")) {
91
if (.rk.available.packages.cache$timestamp > (Sys.time () - 3600)) {
92
if (all (.rk.available.packages.cache$repos$repos == options ("repos")$repos)) {
93
x <- .rk.available.packages.cache$cache
99
x <- available.packages()
100
.rk.available.packages.cache <<- list (cache = x, timestamp = Sys.time (), repos = options ("repos"))
106
".rk.get.old.packages" <- function () {
107
x <- old.packages (available=.rk.cached.available.packages ())
108
return (list (as.character (x[,"Package"]), as.character (x[,"LibPath"]), as.character (x[,"Installed"]), as.character (x[,"ReposVer"]), rk.make.repos.string ()))
110
111
".rk.get.available.packages" <- function () {
111
x <- available.packages ()
112
return (c (as.vector (x[,1]), as.vector (x[,2])))
112
x <- .rk.cached.available.packages ()
113
return (list (as.character (x[,1]), as.character (x[,2]), rk.make.repos.string ()))
115
116
"require" <- function (package, quietly = FALSE, character.only = FALSE, ...)
153
154
# .rk.do.call ("endOpenX11", as.character (dev.cur ()));
157
# changed to allow assignment of values not in levels without losing information.
158
"[<-.factor" <- function (x, i, value) {
162
if (is.factor(value))
163
value <- levels(value)[value]
164
m <- match(value, lx)
165
if (any(is.na(m) & !is.na(value))) {
168
mode (x) <- "character"
169
warning("invalid factor level. Dropping factor-class. Restore using rk.restore.factor ().")
171
## here, let m revert to original value to allow temporary storage in different type (probably character)
172
## change storage back to 'normal' factor using "match (unclass (x), levels (x))"
177
attr(x, "levels") <- lx
181
tx <- cx[cx != "factor"]
182
if (length (tx) < 1) {
183
class (x) <- mode (x)
191
"rk.restore.factor" <- function (x) {
192
t <- match (x, levels (x))
193
if (length (class (x)) > 1) {
194
classes <- c ("factor", class (x))
198
attribs <- attributes (x)
199
eval (substitute (x <<- t))
200
eval (substitute (attributes (x) <<- attribs))
201
eval (substitute (class (x) <<- classes))
157
# these functions can be used to track assignments to R objects. The main interfaces are .rk.watch.symbol (k) and .rk.unwatch.symbol (k). This works by copying the symbol to a backup environment, removing it, and replacing it by an active binding to the backup location
158
".rk.watched.symbols" <- new.env ()
160
".rk.make.watch.f" <- function (k) {
162
if (!missing (value)) {
163
assign (k, value, envir=.rk.watched.symbols)
164
.rk.do.call ("ws", k);
167
get (k, envir=.rk.watched.symbols)
172
".rk.watch.symbol" <- function (k) {
173
f <- .rk.make.watch.f (k)
174
assign (k, get (k, envir=globalenv ()), envir=.rk.watched.symbols)
175
rm (list=k, envir=globalenv ())
177
makeActiveBinding (k, f, globalenv ())
182
# not needed by rkward
183
".rk.unwatch.symbol" <- function (k) {
184
rm (list=k, envir=globalenv ())
186
k <<- .rk.watched.symbols$k
188
rm (k, envir=.rk.watched.symbols);
193
".rk.watch.globalenv" <- function () {
194
newlist <- ls (globalenv (), all.names=TRUE)
195
oldlist <- ls (.rk.watched.symbols, all.names=TRUE)
196
for (old in oldlist) { # unwatch no longer present items
197
if (!(old %in% newlist)) {
198
rm (list=old, envir=.rk.watched.symbols);
202
for (new in newlist) { # watch new items
203
if (!(new %in% oldlist)) {
204
.rk.watch.symbol (new)
209
".rk.get.vector.data" <- function (x) {
211
ret$data <- as.vector (unclass (x));
212
ret$levels <- levels (x)
213
if (is.null (ret$levels)) ret$levels <- ""
214
i <- attr (x, ".rk.invalid.fields")
215
ret$invalids <- as.character (c (names (i), i));
216
if (length (ret$invalids) == 0) ret$invalids <- ""
220
".rk.get.structure" <- function (x, name, envlevel=0, namespacename=NULL, misplaced=FALSE) {
225
# Do not change the order! Make sure all fields exist, even if empty
227
# 1: name should always be first
228
name <- as.character (name)
231
if (is.data.frame (x)) type <- type + 1
232
if (is.matrix (x)) type <- type + 2
233
if (is.array (x)) type <- type + 4
234
if (is.list (x)) type <- type + 8
239
if (is.function (x)) {
242
} else if (is.environment (x)) {
247
if (is.factor (x)) type <- type + 32768 # 2 << 14
248
else if (is.numeric (x)) type <- type + 16384 # 1 << 14
249
else if (is.character (x)) type <- type + 49152 # 3 << 14
250
else if (is.logical (x)) type <- type + 65536 # 4 << 14
253
if (!is.null (attr (x, ".rk.meta"))) type = type + 4096
254
if (misplaced) type <- type + 8192
255
type <- as.integer (type)
259
if (is.null (classes)) classes = ""
262
meta <- .rk.get.meta (x)
263
if (is.null (meta)) meta <- ""
267
if (is.null (dims)) dims <- length (x) # handling for objects that - according to R - do not have a dimension (such as vectors, functions, etc.)
268
if (is.null (dims)) dims <- 0 # according to help ("length"), we need to play safe
269
dims <- as.integer (dims)
271
# 6: Special info valid for some objects ony. This should always be last in the returned structure, as the number of fields may vary
272
if (cont) { # a container
273
if (is.environment (x)) {
274
sub <- .rk.get.environment.children (x, envlevel+1, namespacename)
278
if (!is.null (nms)) {
282
sub[[nms[i]]] <- .rk.get.structure (child, nms[i], envlevel)
286
return (invisible (list (name, type, classes, meta, dims, sub)))
287
} else if (fun) { # a function
288
argnames <- as.character (names (formals (x)))
289
argvalues <- as.character (lapply (formals (x), function (v) {
290
if (is.character (v)) return (encodeString (v, quote="\""))
293
return (invisible (list (name, type, classes, meta, dims, argnames, argvalues)))
295
return (invisible (list (name, type, classes, meta, dims)))
298
".rk.get.environment.children" <- function (x, envlevel=0, namespacename=NULL) {
301
if (envlevel < 2) { # prevent infinite recursion
302
lst <- ls (x, all.names=TRUE)
303
if (is.null (namespacename)) {
304
for (childname in lst) {
305
ret[[childname]] <- .rk.get.structure (get (childname, envir=x), childname, envlevel)
308
# before R 2.4.0, operator "::" would only work on true namespaces, not on package names (operator "::" work, if there is a namespace, and that namespace has the symbol in it)
309
# TODO remove once we depend on R >= 2.4.0
310
if (compareVersion (paste (R.version$major, R.version$minor, sep="."), "2.4.0") < 0) {
311
ns <- tryCatch (asNamespace (namespacename), error = function(e) NULL)
312
for (childname in lst) {
314
if (is.null (ns) || (!exists (childname, envir=ns, inherits=FALSE))) misplaced <- TRUE
315
ret[[childname]] <- .rk.get.structure (get (childname, envir=x), childname, envlevel, misplaced=misplaced)
318
# for R 2.4.0 or greater: operator "::" works if package has no namespace at all, or has a namespace with the symbol in it
319
ns <- tryCatch (asNamespace (namespacename), error = function(e) NULL)
320
for (childname in lst) {
322
if ((!is.null (ns)) && (!exists (childname, envir=ns, inherits=FALSE))) misplaced <- TRUE
323
ret[[childname]] <- .rk.get.structure (get (childname, envir=x), childname, envlevel, misplaced=misplaced)