forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.r
65 lines (53 loc) · 1.58 KB
/
data.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
# Environment for caching connections etc
cache <- new.env(parent = emptyenv())
is_cached <- function(name) exists(name, envir = cache)
set_cache <- function(name, value) {
# message("Setting ", name, " in cache")
assign(name, value, envir = cache)
value
}
get_cache <- function(name) {
# message("Getting ", name, " from cache")
get(name, envir = cache)
}
cache_computation <- function(name, computation) {
if (is_cached(name)) {
get_cache(name)
} else {
res <- force(computation)
set_cache(name, res)
res
}
}
load_srcs <- function(f, src_names, quiet = NULL) {
if (is.null(quiet)) {
quiet <- !identical(Sys.getenv("NOT_CRAN"), "true")
}
srcs <- lapply(src_names, function(x) {
out <- NULL
try(out <- f(x), silent = TRUE)
if (is.null(out) && !quiet) {
message("Could not instantiate ", x, " src")
}
out
})
compact(setNames(srcs, src_names))
}
db_location <- function(path = NULL, filename) {
if (!is.null(path)) {
# Check that path is a directory and is writeable
if (!file.exists(path) || !file.info(path)$isdir) {
stop(path, " is not a directory", call. = FALSE)
}
if (!is_writeable(path)) stop("Can not write to ", path, call. = FALSE)
return(file.path(path, filename))
}
pkg <- file.path(system.file("db", package = "dplyr"))
if (is_writeable(pkg)) return(file.path(pkg, filename))
tmp <- tempdir()
if (is_writeable(tmp)) return(file.path(tmp, filename))
stop("Could not find writeable location to cache db", call. = FALSE)
}
is_writeable <- function(x) {
unname(file.access(x, 2) == 0)
}