-
Notifications
You must be signed in to change notification settings - Fork 4
/
whereami.R
88 lines (68 loc) · 2 KB
/
whereami.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' @title Return where the script is run from
#' @description Returns the file that a command is run from.
#'
#' If traceback is available then the line that it was run from is also returned.
#' @param path_expand logical, expand relational path, Default: FALSE
#' @param tag character, optional tag for the call, Default: NULL
#' @return character
#' @examples
#' whereami()
#' whereami(path_expand = TRUE)
#' @rdname whereami
#' @export
#' @family main
#' @author Jonathan Sidi
#' @importFrom rstudioapi getActiveDocumentContext isAvailable
#' @importFrom utils getSrcDirectory getSrcFilename
whereami <- function(path_expand = FALSE, tag = NULL) {
tf <- tempfile()
on.exit({
if (length(ret) > 1) {
bump(ret, tag)
}
unlink(tf)
}, add = TRUE)
cmd <- commandArgs(trailingOnly = FALSE)
if (any("RStudio" %in% cmd)) {
src <- "Untitled"
adc <- rstudioapi::getActiveDocumentContext()
if (adc$id == "#console") {
src <- "Console"
}
if (nchar(adc$path) > 0) {
src <- path.expand(adc$path)
}
if (length(getSrcFilename(sys.call(sys.nframe() - 1))) > 0) {
if (nchar(getSrcFilename(sys.call(sys.nframe() - 1))) > 0) {
src <- file.path(
utils::getSrcDirectory(sys.call(sys.nframe() - 1)),
utils::getSrcFilename(sys.call(sys.nframe() - 1))
)
src <- gsub("^\\.", getwd(), src)
}
}
if (!path_expand) {
src <- path_reduce(src)
}
cat(src, file = tf, sep = "\n")
} else {
src <- thisfile()
if (!is.null(src)) {
src <- normalizePath(src,winslash = '/', mustWork = FALSE)
if (!path_expand) {
src <- path_reduce(src)
}
} else {
cat("", file = tf, sep = "\n", append = TRUE)
}
if (!is.null(src)) {
cat(src, file = tf, sep = "\n", append = TRUE)
}
}
refloc <- crumb()
if (!is.null(refloc)) {
cat(refloc, file = tf, sep = "\n", append = TRUE)
}
ret <- readLines(tf)
structure(ret, class = c("whereami","character"))
}