-
Notifications
You must be signed in to change notification settings - Fork 8
/
GUI-tools.R
249 lines (227 loc) · 9.43 KB
/
GUI-tools.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
## R GUI supplementary code and tools (loaded since R 2.9.0)
## target environment for all this
.e <- attach(NULL, name = "tools:RGUI")
if (getRversion() < "3.0.0") error(" NOTE: your R version is too old")
add.fn <- function(name, FN) {
assign(name, FN, .e)
environment(.e[[name]]) <- .e
}
## print.hsearch is our way to display search results internally
add.fn("print.hsearch", function (x, ...)
{
if (.Platform$GUI == "AQUA") {
db <- x$matches
rows <- NROW(db)
if (rows == 0) {
writeLines(strwrap(paste("No help files found matching",
sQuote(x$pattern), "using", x$type, "matching\n\n")))
} else {
## someone changed the case of some variables in R 3.2.0 so we have to ignore it
names(db) <- tolower(names(db))
url = character(rows)
for (i in 1:rows) {
lib <- dirname(db[i, "libpath"])
tmp <- as.character(help(db[i, "topic"],
package = db[i, "package"],
lib.loc = lib, help_type = 'html'))
if (length(tmp) > 0)
url[i] <- gsub(lib, '/library', tmp, fixed = TRUE)
}
wtitle <- paste("Help topics matching", sQuote(x$pattern))
showhelp <- which(.Call("hsbrowser", db[, "topic"],
db[, "package"], db[, "title"],
wtitle, url))
for (i in showhelp)
print(help(db[i, "topic"], package = db[i, "package"]))
}
invisible(x)
}
else utils:::printhsearchInternal(x, ...)
})
## --- the following functions are compatibility functions that wil go away very soon!
add.fn("browse.pkgs", function (repos = getOption("repos"), contriburl = contrib.url(repos, type), type = getOption("pkgType"))
{
if (.Platform$GUI != "AQUA")
stop("this function is intended to work with the Aqua GUI")
x <- installed.packages()
i.pkgs <- as.character(x[, 1])
i.vers <- as.character(x[, 3])
label <- paste("(", type, ") @", contriburl)
y <- available.packages(contriburl = contriburl)
c.pkgs <- as.character(y[, 1])
c.vers <- as.character(y[, 2])
idx <- match(i.pkgs, c.pkgs)
vers2 <- character(length(c.pkgs))
xx <- idx[which(!is.na(idx))]
vers2[xx] <- i.vers[which(!is.na(idx))]
i.vers <- vers2
want.update <- rep(FALSE, length(i.vers))
.Call("pkgbrowser", c.pkgs, c.vers, i.vers, label, want.update)
})
add.fn("Rapp.updates", function ()
{
if (.Platform$GUI != "AQUA")
stop("this function is intended to work with the Aqua GUI")
cran.ver <- readLines("http://cran.r-project.org/bin/macosx/VERSION")
ver <- strsplit(cran.ver, "\\.")
cran.ver <- as.numeric(ver[[1]])
rapp.ver <- paste(R.Version()$major, ".", R.version$minor, sep = "")
ver <- strsplit(rapp.ver, "\\.")
rapp.ver <- as.numeric(ver[[1]])
this.ver <- sum(rapp.ver * c(10000, 100, 1))
new.ver <- sum(cran.ver * c(10000, 100, 1))
if (new.ver > this.ver) {
cat("\nThis version of R is", paste(rapp.ver, collapse = "."))
cat("\nThere is a newer version of R on CRAN which is",
paste(cran.ver, collapse = "."), "\n")
action <- readline("Do you want to visit CRAN now? ")
if (substr(action, 1, 1) == "y")
system("open http://cran.r-project.org/bin/macosx/")
} else cat("\nYour version of R is up to date\n")
})
add.fn("package.manager", function ()
{
if (.Platform$GUI != "AQUA")
stop("this function is intended to work with the Aqua GUI")
loaded.pkgs <- .packages()
x <- library()
x <- x$results[x$results[, 1] != "base", ]
pkgs <- x[, 1]
pkgs.desc <- x[, 3]
is.loaded <- !is.na(match(pkgs, loaded.pkgs))
pkgs.status <- character(length(is.loaded))
pkgs.status[which(is.loaded)] <- "loaded"
pkgs.status[which(!is.loaded)] <- " "
pkgs.url <- file.path(find.package(pkgs, quiet=TRUE), "html", "00Index.html")
load.idx <-
.Call("pkgmanager", is.loaded, pkgs, pkgs.desc, pkgs.url)
toload <- which(load.idx & !is.loaded)
tounload <- which(is.loaded & !load.idx)
for (i in tounload) {
cat("unloading package:", pkgs[i], "\n")
do.call("detach", list(paste("package", pkgs[i], sep = ":")))
}
for (i in toload) {
cat("loading package:", pkgs[i], "\n")
library(pkgs[i], character.only = TRUE)
}
})
add.fn("rcompgen.completion", function (x)
{
utils:::.assignLinebuffer(x)
utils:::.assignEnd(nchar(x))
utils:::.guessTokenFromLine()
utils:::.completeToken()
utils:::.CompletionEnv[["comps"]]
})
add.fn("data.manager", function ()
{
if (.Platform$GUI != "AQUA")
stop("this function is intended to work with the Aqua GUI")
data.by.name <- function(datanames) {
aliases <- sub("^.+ +\\((.+)\\)$", "\\1", datanames)
data(list = ifelse(aliases == "", datanames, aliases))
}
x <- suppressWarnings(data(package = .packages(all.available = TRUE)))
dt <- x$results[, 3]
pkg <- x$results[, 1]
desc <- x$results[, 4]
len <- NROW(dt)
url <- character(len)
for (i in 1:len) {
tmp <- as.character(help(dt[i], package = pkg[i], help_type = "html"))
if (length(tmp) > 0)
url[i] <- tmp
}
as.character(help("BOD", package = "datasets", help_type = "html"))
load.idx <- which(.Call("datamanager", dt, pkg, desc, url))
for (i in load.idx) {
cat("loading dataset:", dt[i], "\n")
data.by.name(dt[i])
}
})
# added "interactive" argument to "prompt(...)"
# if interactive == TRUE the generated Rd doc will be opened in R.app
# for filename = NA or filename == NULL -> an untitled new Rd doc for passed function
# for filename = a_path -> a_path will be opened for passed function
add.fn("prompt", function (object, filename = NULL, name = NULL, interactive = FALSE, ...)
{
if(interactive == FALSE) {
## call default prompt()
## the name setting here is necessary to avoid taking 'object'
## as passed name - TODO has to be improved
if(missing(name))
name <- if(is.character(object))
object
else {
name <- substitute(object)
if(is.name(name))
as.character(name)
else if(is.call(name)
&& (as.character(name[[1L]]) %in% c("::", ":::", "getAnywhere"))) {
name <- as.character(name)
name[length(name)]
}
else
stop("cannot determine a usable name")
}
return(utils:::prompt(object, filename = filename, name= name, ...))
} else {
## let R.app handle the result of prompt()
isTempFile <- FALSE
if(is.null(filename) || is.na(filename)) {
## if no filename was passed we do it on a temporary file
## which will be removed by 'RappPrompt->RController.handlePromptRdFileAtPath
isTempFile <- TRUE
filename <- tempfile()
}
## call default prompt() by suppressing the outputted messages since
## we're in interactive mode
suppressMessages(utils:::prompt(object=object, filename = filename, name = name, ...))
## let RappPrompt - defined in main.m - handle the generated Rd file
invisible(.Call("RappPrompt", filename, isTempFile))
}
})
## we catch q/quit to make sure users don't use it inadvertently
if (!isTRUE(getOption("RGUI.base.quit"))) {
add.fn("q", function (save = "default", status = 0, runLast = TRUE)
.Call("RappQuit", save, status, runLast))
add.fn("quit", function (save = "default", status = 0, runLast = TRUE)
.Call("RappQuit", save, status, runLast))
}
.e[[".__RGUI__..First"]] <- .GlobalEnv$.First
add.fn("aqua.browser", function(x, ...) {
## El Capitan has a bug in ATS that makes it impossible to white-list 127.0.0.1 so we have to use localhost
x <- gsub("http://127.0.0.1", "http://localhost", x, fixed=TRUE)
.Call("aqua.custom.print", "help-files", x)
invisible(x)})
add.fn("main.help.url", function() help.start(browser = aqua.browser))
add.fn("wsbrowser", function(IDS, IsRoot, Container, ItemsPerContainer,
ParentID, NAMES, TYPES, DIMS)
{
.Call("wsbrowser", as.integer(IDS), IsRoot, Container,
as.integer(ItemsPerContainer), as.integer(ParentID),
NAMES, TYPES, DIMS)
invisible()
})
## As from R 2.15.x the BioC version cannot be determined algorithmically
add.fn("setBioCversion", function()
{
old <- getOption('BioC.Repos')
if(!is.null(old)) return(old)
mirror <- getOption("BioC_mirror", "http://www.bioconductor.org")
## as of R 3.2.0 it is a function and not a scalar
ver <- as.character(if (is.function(tools:::.BioC_version_associated_with_R_version)) tools:::.BioC_version_associated_with_R_version() else tools:::.BioC_version_associated_with_R_version)
options("BioC.Repos" = paste(mirror, "packages",
ver, c("bioc", "data/annotation", "data/experiment", "extra"),
sep = "/"))
getOption('BioC.Repos')
})
if (nzchar(Sys.getenv("R_GUI_APP_VERSION"))) {
cat("[R.app GUI ",
Sys.getenv("R_GUI_APP_VERSION")," (",
Sys.getenv("R_GUI_APP_REVISION"),") ",
R.version$platform,"]\n\n", sep = '')
} else {
cat("[Warning: GUI-tools are intended for internal use by the R.app GUI only]\n")
}