2
2
3
3
# ' @export
4
4
.load_opts <- function (x , check , envir , verbose , .parse , ... ){
5
-
5
+
6
6
if (! file.exists(x )){
7
7
message(" Configuration file does not exist, loading skipped. Expecting a file at:" , x )
8
8
return ()
11
11
if (class(conf ) == " try-error" )
12
12
stop(" error in read_sheet \n There was a problem reading this file: " , x , " \n Make sure that all lines are two columns " ,
13
13
" seperated by TAB. " )
14
-
14
+
15
15
colnames(conf ) = c(" name" , " value" )
16
16
lst1 = as.list(conf $ value )
17
17
names(lst1 ) = conf $ name
18
-
18
+
19
19
# # for auto-completion its best to have
20
20
lst2 = get_opts(envir = envir , .use.names = TRUE )
21
21
lst = c(lst2 , lst1 )
22
-
22
+
23
23
if (.parse )
24
24
lst = parse_opts(lst , envir = envir )
25
-
25
+
26
26
# # -- check the ones with file paths
27
27
if (check ){
28
28
tmp <- chk_conf(lst [names(lst ) %in% names(lst1 )])
29
29
}
30
-
30
+
31
31
# options(lst)
32
32
set_opts(.dots = lst , envir = envir )
33
33
# opts()$set(lst)
39
39
# ' @seealso \link{read_sheet}
40
40
# ' @export
41
41
load_opts <- function (x , check = TRUE , envir = opts , verbose = TRUE , .parse = TRUE , ... ){
42
-
42
+
43
43
if (missing(x ))
44
44
stop(" Please supply path to a file to load as x" )
45
-
45
+
46
46
# # .load_opts: works on a single file
47
47
lst <- lapply(x , .load_opts , check = check , envir = envir , .parse = .parse , verbose = verbose , ... )
48
-
48
+
49
49
# # only one conf file is read
50
50
if (length(x ) == 1 )
51
51
lst = lst [[1 ]]
52
-
52
+
53
53
# # return them as a list
54
54
invisible (lst )
55
55
}
@@ -72,28 +72,28 @@ load_conf <- function(...){
72
72
# ' @import whisker
73
73
# '
74
74
parse_opts <- function (lst , envir ){
75
-
75
+
76
76
# # get values from previous envir
77
77
# # which are being called by name in newer options
78
78
# # example {{{mydir}}}
79
79
get_vars <- function (x ){
80
80
unlist(regmatches(x , gregexpr(' (?<=\\ {\\ {)[[:alnum:]_.]+(?=\\ }\\ })' , x , perl = TRUE )))
81
81
}
82
-
82
+
83
83
# get variables which need to be expanded
84
84
vars = get_vars(unlist(lst ))
85
85
# x = get_opts(c("var", unlist(vars)), envir = envir) ## ensure, always a list
86
86
x = as.list(get_opts(vars , .use.names = TRUE ), envir = envir ) # # ensure, always a list
87
-
87
+
88
88
# # if there are multiple elements with the same name
89
89
# # this ensures we take the last/latest element
90
90
lst = c(x , lst )
91
91
lst = rev(lst )
92
92
lst = lst [! duplicated(names(lst ))]
93
-
93
+
94
94
# # handling duplicates
95
95
# # if a option is set multiple times, we consider the last one.
96
-
96
+
97
97
# # --- sequentially evaluae each configuration
98
98
for (i in 1 : length(lst )){
99
99
# # resolve ONLY when neccesary
@@ -105,7 +105,7 @@ parse_opts <- function(lst, envir){
105
105
106
106
107
107
chk_conf <- function (x ){
108
- path_pattern = c(" path$|dir$|exe$" )
108
+ path_pattern = c(" path$|dir$|exe$|file$ " )
109
109
pths = grep(path_pattern , names(x ))
110
110
mis_pths = ! (file.exists(as.character(x )[pths ]))
111
111
if (sum(mis_pths ) > 0 ){
0 commit comments