I'm making a long script to analyze some MS data by dividing it in some main functions. Each of it consist in a step in my pipeline and have some possible sub functions to execute. Most of them don't need much additional arguments in the main function.
However, I'm trying to design it to be as flexible and reusable as possible to use it in future analysis, and therefore some fine tuning arguments will be needed sometimes. I don't want a never ending list of arguments in the main function, so I want to use additional arguments (...) in the main function and the sub functions. This generates two problems: conflicting names (easily avoidable, my code is not that complex) and passing the additional arguments.
I made this dummy code so far while trying:
main_function <- function(main_arg,
conditional1 = T,
conditional2 = T,
conditional3 = T,
...
){
#some stuff before
if(conditional1){
sub_function_1(main_arg, ... = ...)
}
if(conditional2){
sub_function_2(main_arg, ... = ...)
}
if(conditional3){
sub_function_3(main_arg, ... = ...)
}
}
sub_function_1 <- function(main_arg, ...){
if(...length()!=0){
for(i in seq(...length())){
assign(
x = ...names()[i],
value = ...elt(i)
)
}
}
print(main_arg+optional_arg_1)
}
sub_function_2 <- function(...){
if(...length()!=0){
for(i in seq(...length())){
assign(
x = ...names()[i],
value = ...elt(i)
)
}
}
print(main_arg+optional_arg_2)
}
sub_function_3 <- function(main_arg, ...){
print(main_arg+optional_arg_3)
}
main_function(main_arg = 1,
optional_arg_1 = 3,
optional_arg_2 = 2,
optional_arg_3 = 4)
sub_function_2(main_arg = 3, optional_arg_2 = 2)
sub_function_3(main_arg = 1, optional_arg_3 = 4)
Sub functions 1 and 2 work by themself, but not sub function 3, neither the main function. I only get to run the sub function 1 in the main function, but I have the following error in sub function 2:
Error in assign(x = ...names()[i], value = ...elt(i)) : attempt to use zero-length variable name Called from: assign(x = ...names()[i], value = ...elt(i))
Probably is a very dumb mistake, this is my longest and more complex script so far, and my first time using the ... arguments, either in a stand alone or a nested fashion.