考虑以下函数,该函数有6个可以传递的参数,并且有2 ^ 6 = 64个可能的结果。随着参数列表的增长,这会迅速增长到无法管理的数字。
expl<-function(patt=NULL,yearwise=T,monthwise=F,for_year=NULL,bankwise=T,categ=NULL,df=allst)
代码目前看起来像这样(我只粘贴了前几个if-then语句来提出想法)。
if(!is.null(for_year) && is.null(patt)){
if(bankwise && monthwise)
allst %>% mutate(yr=year(date),mth=month(date,abbr = T)) %>% group_by(yr,mth,bank) %>% summarise(counttx=n(),sumcredit=sum(credit,na.rm = T), sumdebit=sum(debit,na.rm = T)) %>% filter(yr==for_year) else
if(bankwise && !monthwise)
allst %>% mutate(yr=year(date)) %>% group_by(yr,bank) %>% summarise(counttx=n(),sumcredit=sum(credit,na.rm = T), sumdebit=sum(debit,na.rm = T)) %>% filter(yr==for_year) else
if(monthwise && !bankwise)
allst %>% mutate(yr=year(date),mth=month(date,abbr = T)) %>% group_by(yr,mth) %>% summarise(counttx=n(),sumcredit=sum(credit,na.rm = T), sumdebit=sum(debit,na.rm = T)) %>% filter(yr==for_year) else
if(!monthwise && !bankwise) message("You have to select at least one of the two as TRUE: monthwise or bankwise")
} else .....
我确信有一种更好的方法,比如使用开关,但即使是开关也很笨重。我们是否有任何软件包或更好的方法来处理这些不断增加的参数列表,这些参数只需要在dplyr管道中进行增量更改?