[英]Converting the argument name of a function into string
我已經開發了一個 function 它將獲取文件列表並將進行一些統計測試並生成 excel 文件。 在 function(返回對象)的最后一行中,我想要 function 將返回一個與輸入文件名同名的 excel 文件。 在我的示例中,它將給出 list_file.xlsx。 如果我輸入另一個文件,假設 tslist_file 它應該自動返回 tslist_file.xlsx。 function 工作正常。 建議我如何編碼 function 的最后一行,以便我可以概括它。
newey<-function(list_files){
tsmom<-do.call(cbind,lapply(list_files,function(x) read_excel(x)[,2]))
tsmom<-xts(tsmom[,1:5],order.by = seq(as.Date("2005-02-01"),length=183,by="months")-1)
names(tsmom)<-c("tsmom121","tsmom123","tsmom126","tsmom129","tsmom1212")
## newey west
newey_west<-function(x){
model<-lm(x~1)
newey_west<-coeftest(model,vcov=NeweyWest(model,verbose=T))
newey_west[c(1,3,4)]
}
## running newey west
cs_nw_full<-do.call(cbind,lapply(tsmom,newey_west))
library(gtools)
p_values<-cs_nw_full[3,]
cs_nw_full[2,]<-paste0(cs_nw_full[2,],stars.pval(p_values))
write.xlsx(cs_nw_full,"list_file.xlsx")
}
嘗試:
write.xlsx(cs_nw_full, paste0(eval(substitute(list_files)), ".xlsx"))
編輯: @jeetkamal 是絕對正確的 - 你需要使用
write.xlsx(cs_nw_full, paste0(deparse(substitute(list_files)), ".xlsx"))
這里。
我為這個錯誤道歉。 eval
僅在 list_files 是文件名而不是列表list_files
時才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.