-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_inputs.R
71 lines (66 loc) · 2.93 KB
/
get_inputs.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
#' @title Parse input from python
#'
#' @description When calling R from python using subprocess, one can pass argument. A standard have been defined as sending a list
#' of argument with the format arg_name=arg_value. This function parse it and create object in R RAM.
#' @param myArgs list of parameters obtained using \code{commandArgs(trailingOnly = TRUE)}
#' @param wanted_inputs is a list of character containing the name of expected inputs. (default to \code{list()})
#' @param verbose should the function talk (logical, default to TRUE).
#' @details
#' To simulate inputs from python, one can feed a vector respecting the expected format for myArgs (see examples). \cr
#' If you ask for some unprovided inputs, function will stop and raise un error.
#' @return list of asked inputs
#' @export
#' @examples
#' myArgs = c("code_path=E:/CA-SA/DADGP/scripts/",
#' "raw_data_path=E:/CA-SA/DADGP/data/raw_data/",
#' "reports_path=E:/CA-SA/DADGP/data/reports/",
#' "list_cr=['810', '847']")
#'
#' get_inputs(myArgs, wanted_inputs = list("list_cr", "raw_data_path"), verbose = FALSE)
get_inputs <- function(myArgs, wanted_inputs = list(), verbose = TRUE){
## Working environment
function_name <- "get_inputs"
## Initialization # Make inputs as vector
wanted_inputs <- unlist(wanted_inputs)
myArgs <- unlist(myArgs)
myArgs <- sapply(myArgs, URLdecode)
## Identify unprovided inputs
provided_inputs <- sapply(strsplit(myArgs, '='), function(x)x[1])
unprovided_inputs <- unlist(setdiff(wanted_inputs, provided_inputs))
if (length(unprovided_inputs) > 0){
stop(paste0(function_name, ": you asked for some inputs that weren't provided. Here is the list: ",
paste(unprovided_inputs, collapse = ", ")))
}
## Edit those which where fed in my Args #To-do change code with get
for (arg in myArgs){
argDecode <- unlist(strsplit(arg,'='))
if (argDecode[[1]] %in% wanted_inputs){
if ((length(argDecode) == 3 & argDecode[3] == "list") || grepl('\\[*\\]', argDecode[2])){ # Parse for list
# Remove quotes in char (because python might throw too much quotes)
argDecode[2] <- gsub("\"", "",gsub("\'", "", argDecode[2]))
# remove braces
sub_decode <- substr(argDecode[2], 2, nchar(argDecode[2]) -1)
# split
argDecode[2] <- list(strsplit(sub_decode, ", ")[[1]])
}
if (! is.na(argDecode[3]) & ! is.null(argDecode[3]) & exists(paste0("as.", argDecode[3]))){
assign(argDecode[[1]], get(paste0("as.", argDecode[3]))(argDecode[2]))
}
else{
assign(argDecode[[1]], argDecode[2])
}
}
}
## Return list of args
result <- list()
for (arg in wanted_inputs){
result <- c(result, get(arg))
}
names(result) <- wanted_inputs
## Wrapp-up
if (verbose){
print(paste0(function_name, ": I will use the following inputs"))
print(result)
}
return(result)
}