我构建了一个使用dt_aggregate
包版本的函数,我只更改了一个打印字符串。
当我在控制台中粘贴功能时,它可以正常工作,当我从包装中加载它时,它会重新生成错误。
我无法解决这个问题。
tabellacon777 <- function(x){
require(data.table)
require(dplyr)
source("C:/Users/Utente/Documents/prelievi/R/dt_aggregate.R")
require(gridExtra)
require(knitr)
require(kableExtra)
divper777 <- x %>% mutate(Tipo_Marchio = ifelse(Stagione!="P8", "Resi", ifelse(Magazzino.Logico!="77" & Magazzino.Fisico!="06", "FROM", ifelse(Magazzino.Logico=="77","Prima Linea 777","Prima Linea STD"))))
resultcon777 <- dt_aggregate(
divper777, by=.(Tipo_Marchio), by_all.(Tipo_Marchio),
NumCapi = sum(Quantità),
NumPrelievi = sum(complete.cases(Quantità)),
MedCapiPre = round(mean(Quantità),1),
OreDecimali = round(sum(ORE_DEC),1),
CapiOra = round((sum(Quantità)/sum(ORE_DEC)),0),
PrelieviOra = round((sum(complete.cases(Quantità))/sum(ORE_DEC)),0)
)
options(knitr.table.format = "html")
kable(resultcon777, "html")
resultcon777 %>%
kable("html") %>%
kable_styling(bootstrap_options = c("striped", "hover","condensed"),full_width = F) %>%
row_spec(5, bold = T, background = "#E3E3E3")
}
这是dt_aggregate
函数:
dt_aggregate=function(d, by, by_all,...){
library(data.table)
my_args=substitute(.(...))
my_dims=as.character(substitute(by))
my_dims=my_dims[2:length(my_dims)]
my_dims_d=as.character(substitute(by_all))
my_dims_d=my_dims_d[2:length(my_dims_d)]
print(my_dims)
print(my_dims_d)
funct_comb=function(d,num){unlist(lapply(seq(nrow(d)),function(x){paste(as.character(d[x,num]),collapse="")}))}
eg=lapply(1:(length(my_dims_d)),function(x){c(my_dims_d,"[blank]")})
comb=expand.grid(eg,stringsAsFactors = FALSE)
comb$x=funct_comb(comb,seq(length(my_dims_d)))
comb=comb[grepl("\\[blank\\]",comb$x),]
comb=as.data.frame(lapply(seq(length(my_dims_d)),function(x){dat=comb[,x];dat[dat %in% c("[blank]",my_dims_d[x])]}),stringsAsFactors=FALSE)
names(comb)=my_dims_d
comb=comb[!duplicated(funct_comb(comb,seq(length(my_dims_d)))),]
if(class(comb)=="character"){comb=data.frame(my_dims_d=comb,stringsAsFactors=FALSE);names(comb)=my_dims_d}
comb=rbind(my_dims_d,comb)
comb=comb[rev(order(funct_comb(comb,seq(length(my_dims_d))))),,drop=FALSE]
rownames(comb)=NULL
non_part=as.data.frame(lapply(my_dims[!(my_dims %in% my_dims_d)],function(x){rep(x,nrow(comb))}),stringsAsFactors=FALSE)
if(nrow(non_part)>0){
names(non_part)=non_part[1,]
comb=cbind(comb,non_part)
}
l=list()
d=as.data.table(d)
for(i in 1:nrow(comb)){
dims_to_run=as.character(comb[i,])
dims_to_run_i=dims_to_run[!is.na(match(names(comb),dims_to_run))]
dims_to_run_e=names(comb)[is.na(match(names(comb),dims_to_run))]
if(length(dims_to_run_i)>0){
dims_to_run_ev=paste0(".(",paste(dims_to_run_i,collapse=","),")")
print(dims_to_run_ev)
eval(parse(text=paste0("dd=d[,eval(my_args),by=",dims_to_run_ev,"]")))
}else{
dd=d[,eval(my_args)]
}
if(length(dims_to_run_e)>0){
all_df=as.data.frame(lapply(dims_to_run_e,function(x){rep(paste0("Totali"),nrow(dd))}),stringsAsFactors=FALSE)
names(all_df)=dims_to_run_e
dd=cbind.data.frame(dd,all_df)
}
l[[length(l)+1]]=dd
}
rbindlist(l, use.names=TRUE)
}
当我在控制台中粘贴tabellacon777
功能时,一切正常。
当我在里面加载带tabellacon777
函数的库时,运行它所说的函数:
[1] "Tipo_Marchio"
[1] "Tipo_Marchio"
[1] ".(Tipo_Marchio)"
Error in .(NumCapi = sum(Quantità), NumPrelievi = sum(complete.cases(Quantità)), :
could not find function "."