Skip to content

Commit 971998b

Browse files
committed
- load_opts, now does a file check for options like:
- `_exe`, `_dir`, `_path`, `_file`
1 parent b2f48b3 commit 971998b

File tree

7 files changed

+25
-23
lines changed

7 files changed

+25
-23
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
p a r a m s 0.6.0
22
------------------------------------------------
33
- `read_sheet`, now recognizes `.mat` as a tsv
4+
- `load_opts`, now does a file check for options like:
5+
- `_exe`, `_dir`, `_path`, `_file`
46

57
p a r a m s 0.5.0
68
------------------------------------------------

R/load_opts.R

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#' @export
44
.load_opts <- function(x, check, envir, verbose, .parse, ...){
5-
5+
66
if(!file.exists(x)){
77
message("Configuration file does not exist, loading skipped. Expecting a file at:", x)
88
return()
@@ -11,23 +11,23 @@
1111
if(class(conf) == "try-error")
1212
stop("error in read_sheet \nThere was a problem reading this file: ", x, "\nMake sure that all lines are two columns ",
1313
"seperated by TAB. ")
14-
14+
1515
colnames(conf) = c("name", "value")
1616
lst1 = as.list(conf$value)
1717
names(lst1) = conf$name
18-
18+
1919
## for auto-completion its best to have
2020
lst2 = get_opts(envir = envir, .use.names = TRUE)
2121
lst = c(lst2, lst1)
22-
22+
2323
if(.parse)
2424
lst = parse_opts(lst, envir = envir)
25-
25+
2626
## -- check the ones with file paths
2727
if(check){
2828
tmp <- chk_conf(lst[names(lst) %in% names(lst1)])
2929
}
30-
30+
3131
#options(lst)
3232
set_opts(.dots = lst, envir = envir)
3333
#opts()$set(lst)
@@ -39,17 +39,17 @@
3939
#' @seealso \link{read_sheet}
4040
#' @export
4141
load_opts <- function(x, check = TRUE, envir = opts, verbose = TRUE, .parse = TRUE, ...){
42-
42+
4343
if(missing(x))
4444
stop("Please supply path to a file to load as x")
45-
45+
4646
## .load_opts: works on a single file
4747
lst <- lapply(x, .load_opts, check = check, envir = envir, .parse = .parse, verbose = verbose, ...)
48-
48+
4949
## only one conf file is read
5050
if(length(x) == 1)
5151
lst = lst[[1]]
52-
52+
5353
## return them as a list
5454
invisible(lst)
5555
}
@@ -72,28 +72,28 @@ load_conf <- function(...){
7272
#' @import whisker
7373
#'
7474
parse_opts <- function(lst, envir){
75-
75+
7676
## get values from previous envir
7777
## which are being called by name in newer options
7878
## example {{{mydir}}}
7979
get_vars <- function(x){
8080
unlist(regmatches(x, gregexpr('(?<=\\{\\{)[[:alnum:]_.]+(?=\\}\\})', x, perl=TRUE)))
8181
}
82-
82+
8383
# get variables which need to be expanded
8484
vars = get_vars(unlist(lst))
8585
#x = get_opts(c("var", unlist(vars)), envir = envir) ## ensure, always a list
8686
x = as.list(get_opts(vars, .use.names = TRUE), envir = envir) ## ensure, always a list
87-
87+
8888
## if there are multiple elements with the same name
8989
## this ensures we take the last/latest element
9090
lst = c(x, lst)
9191
lst = rev(lst)
9292
lst = lst[!duplicated(names(lst))]
93-
93+
9494
## handling duplicates
9595
## if a option is set multiple times, we consider the last one.
96-
96+
9797
## --- sequentially evaluae each configuration
9898
for(i in 1:length(lst)){
9999
## resolve ONLY when neccesary
@@ -105,7 +105,7 @@ parse_opts <- function(lst, envir){
105105

106106

107107
chk_conf <- function(x){
108-
path_pattern = c("path$|dir$|exe$")
108+
path_pattern = c("path$|dir$|exe$|file$")
109109
pths = grep(path_pattern, names(x))
110110
mis_pths = !(file.exists(as.character(x)[pths]))
111111
if(sum(mis_pths) > 0){

man/fix_names.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/params.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/parse_opts.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/read_sheet.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

params.Rproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SaveWorkspace: No
55
AlwaysSaveHistory: Default
66

77
EnableCodeIndexing: Yes
8-
UseSpacesForTab: No
8+
UseSpacesForTab: Yes
99
NumSpacesForTab: 2
1010
Encoding: UTF-8
1111

0 commit comments

Comments
 (0)