In my function, I either load a table from a file with fread()
, or save a table to a file with write.table()
. Those two functions have some overlapping parameter names (sep
etc) while others are specific to a single function. Is there any way to pass correct parameters from my function call to those functions?
# fnDT is filename to seek, inDT is a function call to build a table
loadOrBuild <- function (fnDT, inDT, ...){
if (file.exists(fnDT)){ # if file is found, then inDT is not evaluated
cat('File found:', fnDT, '; will load it instead of building a new table.\n');
return(loadDT(fnDT, ...)); # loadDT() is my wrapper for fread()
} else {
cat('File not found:', fnDT, '; we\'ll build new table and then save it.\n');
save_DT(inDT, fnDT, row.names=F, ...); # save_DT() is my wrapper for write.table()
return(inDT);
}
}
build.dt <- function(n=10){
return(data.table(test=rep('A',n)))
}
my.dt <- loadOrBuild('myfile.txt', build.dt(20), sep='\t') # this works correctly for both loading and saving
my.dt <- loadOrBuild('myfile.txt', build.dt(20), nrows=10) # this works correctly for loading but raises an error for saving because `nrows` is not an argument for `write.table()`