Repository navigation
Expand file tree
/
Copy pathinitialize.R
More file actions
85 lines (67 loc) · 2.85 KB
/
Copy pathinitialize.R
File metadata and controls
85 lines (67 loc) · 2.85 KB
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
#
# Source all available functions and install requirements
#
# (c) Copyright 2012 J-O Irisson
# http://creativecommons.org/licenses/by/3.0/
#
#-----------------------------------------------------------------------------
## List source files
#-----------------------------------------------------------------------------
# Functions are in the folder "library", which is next to the current file; so we need to detect where we are
# This does it, for reasons I don't fully understand
# source: http://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
frame_files <- lapply(sys.frames(), function(x) x$ofile)
frame_files <- Filter(Negate(is.null), frame_files)
if (length(frame_files) == 0) {
path <- getwd()
} else {
path <- dirname(frame_files[[length(frame_files)]])
}
# Detect names of source files
sourceFiles = list.files(paste(path, "/library", sep=""), pattern="\\.(R|r)$", full=TRUE)
## Detect and install packages
#-----------------------------------------------------------------------------
# Detect which packages are used in those source files
requiredPackages = c()
for (file in sourceFiles) {
# read content
content = scan(file, what="character", quiet=T, sep="\n")
# extract require or library calls
m = regexpr("(require|library)\\(.*?\\)", content)
matched = regmatches(content, m)
# extract the name of the package inside the require call
m = regexpr("\\\".*?\\\"", matched)
matched = regmatches(matched, m)
# remove quotes
pack = gsub("\\\"", "", matched)
# store it as required
requiredPackages = c(requiredPackages, pack)
}
# Install missing packages
requiredPackages = c(unique(requiredPackages), "mapproj")
library("utils")
# NB: this is for installed.packages(). utils is usually loaded automatically with R but when we exectute the initialize.R script from .Rprofile, it seems this packages is not loaded yet.
installedPackages = row.names(installed.packages())
missingPackages = setdiff(requiredPackages, installedPackages)
if (length(missingPackages) > 0) {
install.packages(missingPackages, repos="http://cran.at.r-project.org")
}
## Set up computation environment
#-----------------------------------------------------------------------------
# Source required functions
for (file in sourceFiles) {
source(file)
}
message("-> All functions loaded")
# Check the status of the environment database
# location of environmental data
if (is.null(getOption("atlasr.env.data"))) {
env_path <- paste(path, "/../env_data/", sep="")
abs_path <- normalizePath(env_path)
options(atlasr.env.data=abs_path)
}
# NB: it can be set by the user in a script or in the .Rprofile config file
# check and update if necessary
update.env.data()
# Cleanup the environment
rm(file, content, installedPackages, m, matched, missingPackages, pack, requiredPackages, sourceFiles, frame_files, path)