Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
2701 views
1
2
### BREAKPOINTS
3
.ESSBP. <- new.env()
4
5
### DEBUG/UNDEBUG
6
.ess_find_funcs <- function(env)
7
{
8
objs <- ls(envir = env, all.names = TRUE)
9
objs[sapply(objs, exists, envir = env,
10
mode = 'function', inherits = FALSE)]
11
}
12
13
.ess_all_functions <- function(packages = c(), env = NULL)
14
{
15
if(is.null(env))
16
env <- parent.frame()
17
empty <- emptyenv()
18
coll <- list()
19
for(p in packages){
20
## package might not be attached
21
try({objNS <- .ess_find_funcs(asNamespace(p))
22
objPKG <- .ess_find_funcs(as.environment(paste0('package:', p)))
23
coll[[length(coll) + 1]] <-
24
paste0(p, ':::`', setdiff(objNS, objPKG), '`')
25
}, silent = TRUE)
26
}
27
while(!identical(empty, env)){
28
coll[[length(coll) + 1]] <- .ess_find_funcs(env)
29
env <- parent.env(env)
30
}
31
grep('^\\.ess', unlist(coll, use.names = FALSE),
32
invert = TRUE, value = TRUE)
33
}
34
35
.ess_dbg_getTracedAndDebugged <- function(packages = c())
36
{
37
tr_state <- tracingState(FALSE)
38
on.exit(tracingState(tr_state))
39
generics <- methods::getGenerics()
40
all_traced <- c()
41
for(i in seq_along(generics)){
42
genf <- methods::getGeneric(generics[[i]],
43
package=generics@package[[i]])
44
if(!is.null(genf)){ ## might happen !! v.2.13
45
menv <- methods::getMethodsForDispatch(genf)
46
traced <- unlist(eapply(menv, is, 'traceable', all.names=TRUE))
47
if(length(traced) && any(traced))
48
all_traced <- c(paste(generics[[i]],':',
49
names(traced)[traced],sep=''), all_traced)
50
tfn <- getFunction(generics[[i]], mustFind=FALSE, where = .GlobalEnv)
51
if(!is.null(tfn ) && is(tfn, 'traceable')) # if the default is traced, it does not appear in the menv :()
52
all_traced <- c(generics[[i]], all_traced)
53
}
54
}
55
debugged_pkg <- unlist(lapply(packages, function(pkgname){
56
ns <- asNamespace(pkgname)
57
funcs <- .ess_find_funcs(ns)
58
dbged <- funcs[unlist(lapply(funcs,
59
function(f){
60
isdebugged(get(f, envir = ns, inherits = FALSE))
61
}))]
62
if(length(dbged))
63
paste0(pkgname, ':::`', dbged, '`')
64
}))
65
env <- parent.frame()
66
## traced function don't appear here. Not realy needed and would affect performance.
67
all <- .ess_all_functions(packages = packages, env = env)
68
which_deb <- lapply(all, function(nm){
69
## if isdebugged is called with string it doess find
70
tryCatch(isdebugged(get(nm, envir = env)),
71
error = function(e) FALSE)
72
## try(eval(substitute(isdebugged(nm), list(nm = as.name(nm)))), silent = T)
73
})
74
debugged <- all[which(unlist(which_deb, recursive=FALSE, use.names=FALSE))]
75
unique(c(debugged_pkg, debugged, all_traced))
76
}
77
78
.ess_dbg_UntraceOrUndebug <- function(name, env = parent.frame())
79
{
80
tr_state <- tracingState(FALSE)
81
on.exit(tracingState(tr_state))
82
if( grepl('::', name) ){
83
## foo:::bar name
84
eval(parse(text = sprintf('undebug(%s)', name)))
85
}else{
86
## name is a name of a function to be undebugged or has a form
87
## name:Class1#Class2#Class3 for traced methods
88
name <- strsplit(name, ':', fixed = TRUE)[[1]]
89
if( length(name)>1 ){
90
## a method
91
fun <- name[[1]]
92
sig <- strsplit(paste(name[-1], collapse=''), '#', fixed=TRUE)[[1]]
93
untrace(fun, signature = sig)
94
}else{
95
## function
96
if( is(getFunction(name, where = parent.frame()), 'traceable') )
97
untrace(name)
98
else if(grepl(":", name))
99
undebug(name)
100
else
101
undebug(get(name, envir = env))
102
}}
103
}
104
105
.ess_dbg_UndebugALL <- function(funcs)
106
{
107
tr_state <- tracingState(FALSE)
108
on.exit(tracingState(tr_state))
109
env <- parent.frame()
110
invisible(lapply(funcs, function( nm ) {
111
## ugly tryCatch, but there might be several names pointing to the
112
## same function, like foo:::bar and bar. An alternative would be
113
## to call .ess_dbg_getTracedAndDebugged each time but that might
114
## be ery slow
115
try(.ess_dbg_UntraceOrUndebug(nm, env = env), TRUE)
116
}))
117
}
118
119
### WATCH
120
.ess_watch_expressions <- list()
121
122
.ess_watch_eval <- function()
123
{
124
env <- as.environment("ESSR")
125
exps <- get('.ess_watch_expressions', envir = env)
126
if(length(exps) == 0) {
127
## using old style so this can be parsed by R 1.9.1 (e.g):
128
cat('\n# Watch list is empty!\n',
129
'# a append new expression',
130
'# i insert new expression',
131
'# k kill',
132
'# e edit the expression',
133
'# r rename',
134
'# n/p navigate',
135
'# u/d,U move the expression up/down',
136
'# q kill the buffer',
137
sep="\n")
138
} else {
139
.parent_frame <- parent.frame()
140
.essWEnames <- allNames(exps)
141
len0p <- !nzchar(.essWEnames)
142
.essWEnames[len0p] <- seq_along(len0p)[len0p]
143
for(i in seq_along(exps)) {
144
cat('\n@---- ', .essWEnames[[i]], ' ',
145
rep.int('-', max(0, 35 - nchar(.essWEnames[[i]]))), '-@\n', sep = '')
146
cat(paste('@---:', deparse(exps[[i]][[1]])), ' \n', sep = '')
147
tryCatch(print(eval(exps[[i]],
148
envir = .parent_frame)),
149
error = function(e) cat('Error:', e$message, '\n' ),
150
warning = function(w) cat('warning: ', w$message, '\n' ))
151
}
152
}
153
}
154
155
.ess_watch_assign_expressions <- function(elist){
156
assign(".ess_watch_expressions", elist, envir = as.environment("ESSR"))
157
}
158
159
.ess_log_eval <- function(log_name)
160
{
161
env <- as.environment("ESSR")
162
if(!exists(log_name, envir = env, inherits = FALSE))
163
assign(log_name, list(), envir = env)
164
log <- get(log_name, envir = env, inherits = FALSE)
165
.essWEnames <- allNames(.ess_watch_expressions)
166
cur_log <- list()
167
.parent_frame <- parent.frame()
168
for(i in seq_along(.ess_watch_expressions)) {
169
capture.output( {
170
cur_log[[i]] <-
171
tryCatch(eval(.ess_watch_expressions[[i]]),
172
envir = .parent_frame,
173
error = function(e) paste('Error:', e$message, '\n'),
174
warning = function(w) paste('warning: ', w$message, '\n'))
175
if(is.null(cur_log[i][[1]]))
176
cur_log[i] <- list(NULL)
177
})
178
}
179
names(cur_log) <- .essWEnames
180
assign(log_name, c(log, list(cur_log)), envir = env)
181
invisible(NULL)
182
}
183
184
.ess_package_attached <- function(pack_name){
185
as.logical(match(paste0("package:", pack_name), search()))
186
}
187
188
189