-
Notifications
You must be signed in to change notification settings - Fork 4
/
breadcrumb.R
39 lines (29 loc) · 851 Bytes
/
breadcrumb.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
my_traceback <- function(x = NULL) {
if(is.null(x) && !is.null(x <- get0(".Traceback", envir = baseenv())))
{}
else if (is.numeric(x)){
my_internal <- get0(".Internal", envir = baseenv())
x <- my_internal(traceback(x))
}
x
}
crumb <- function() {
x <- my_traceback(1)
if (any(grepl("^rmarkdown", x[[length(x)]]))) {
srloc <- gsub('^(.*?)\\(|\\,(.*?)$|["]', "", x[[length(x)]][1])
return(srloc)
}
idx <- which(sapply(x, function(xx) all(grepl(pattern = "whereami", xx))))
idx <- idx[length(idx)]
srcloc <- if (!is.null(srcref <- attr(x[[idx]], "srcref"))) {
srcfile <- attr(srcref, "srcfile")
srcfile_name <- srcfile$filename
if (!nzchar(srcfile_name)) {
return(NULL)
}
sprintf("%s#%s", srcfile_name, srcref[1L])
} else {
NULL
}
return(srcloc)
}