2016-10-03 29 views

回答

4

我們可能會使用substitute在函數內部

f1 <- function(dat) { 
      name <- deparse(substitute(dat)) 
      #or 
      #name <- as.character(match.call()[-1]) 

      dat %>% 
       mutate(fk_table = name) 
} 

f1(dfB) 
# _ID0 fk_table 
#1 z1  dfB 
#2 z2  dfB 
#3 z3  dfB 
+0

好mentionning是'DFB%>%f1'不會在這裏工作。 – agenis

+0

該解決方案完全符合我的要求,但現在我想在'dplyr'命令鏈中使用它。有沒有辦法做到這一點? 'dfB%>%f1()' – Bobby

+0

謝謝@agenis,這正是我在測試你寫評論時的情況! – Bobby