/
zzz.r
80 lines (63 loc) · 2.12 KB
/
zzz.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
.onAttach <- function(...) {
# Assume that non-windows users have paths set correctly
if (.Platform$OS.type != "windows") return()
# Check if Rtools is already set up
if (all(on_path("ls.exe", "gcc.exe"))) return()
# Look for rtools
rtools_path <- rtools()
if (is.null(rtools_path)) return()
# Look for gcc
if (current_ver() == "2.15") {
gcc_bin <- file.path(rtools_path, "gcc-4.6.3", "bin")
} else {
gcc_bin <- file.path(rtools_path, "MinGW", "bin")
if (.Platform$r_arch == "x64") {
gcc_bin <- c(gcc_bin, file.path(rtools_path, "MinGW64", "bin"))
}
}
rtools_bin <- file.path(rtools_path, "bin")
paths <- normalizePath(c(rtools_bin, gcc_bin))
new_paths <- setdiff(paths, get_path())
if (length(new_paths) == 0) return()
packageStartupMessage("Rtools not in path, but I'm adding it automatically :)")
add_path(new_paths)
}
rtools_url <- "http://cran.r-project.org/bin/windows/Rtools/"
rtools <- function() {
# Look in registry
key <- NULL
try(key <- utils::readRegistry("SOFTWARE\\R-core\\Rtools",
hive = "HLM", view = "32-bit"), silent = TRUE)
if (!is.null(key)) {
version_match <- key$`Current Version` == current_ver()
if (!version_match) {
packageStartupMessage("Version of Rtools does not match R version :(. ",
"Please reinstall Rtools from ", rtools_url, ".")
return()
}
return(key$InstallPath)
}
# Look in default location
default_path <- normalizePath("c:\\Rtools\\bin", mustWork = FALSE)
if (file.exists(default_path)) return(default_path)
# Give up
packageStartupMessage("Rtools not installed :(. Please install from ",
rtools_url, ".")
invisible(NULL)
}
current_ver <- function() {
minor <- strsplit(version$minor, ".", fixed = TRUE)[[1]]
paste(version$major, ".", minor[1], sep = "")
}
.onLoad <- function(libname, pkgname) {
op <- options()
op.devtools <- list(
devtools.path="~/R-dev",
github.user="hadley"
)
toset <- !(names(op.devtools) %in% names(op))
if(any(toset)) options(op.devtools[toset])
}
on_path <- function(...) {
unname(Sys.which(c(...)) != "")
}