2013-11-03 100 views
1

也許我使用了錯誤的searchterms,或者這是不可能的:獲取對象ddplyed的名稱

我使用ddply來打印出大量圖。在這樣做的函數中,我想創建一個子目錄,其中所有這些圖都以此結束。

這本身沒有問題。但是我不想將目錄名稱作爲額外的輸入參數傳遞,我希望函數以某種方式讀取我在ddplyed上的數據框的名稱。

到目前爲止,我用這個辦法:

myplotfunction <- function(x,df_name){ 
     p=ggplot (...) 
     ggsave(file.path(getwd,df_name,paste0(x$name,".png"),plot=p) 
} 

ddply(mydfIddplyUpon,.(name),myplotfunction,df_name="mydfIddplyUpon") 

但是,如果我能得到的功能,找出DF,這是由plyr分裂的名字,這將是方便多了。

回答

3

你可以做一個包裝函數,

nddply <- function(x, var, fun, ...){ 
    xn <- deparse(substitute(x)) 
    plyr::ddply(x, var, fun, ..., directory_name = xn) 
} 

# where fun is your function that needs a directory_name 
fun <- function(a, directory_name, ...) 
    paste(nrow(a), directory_name, ...) 

nddply(iris, "Species", fun, sep=", from: ") 

編輯:這裏有一個例子節省地塊

nd_ply <- function(x, var, fun, ...){ 
    xn <- deparse(substitute(x)) 
    plyr::d_ply(x, var, fun, ..., directory_name = xn) 
} 

fun <- function(a, directory_name, ...) 
    ggsave(paste0(directory_name, unique(a$Species), ".pdf"), 
     ggplot(a, aes_string(x="Sepal.Length", y="Sepal.Width", ...))+ geom_point()) 

nd_ply(iris, "Species", fun, colour="Sepal.Length") 
+0

謝謝!但是我不確定,如果我真的明白。生成劇情的命令會去哪裏?有趣嗎?到目前爲止,我沒有得到它的工作... – TobiO

+0

經過一些修補和通常的試驗和錯誤,我明白了。其實你解釋得不錯,我昨天似乎已經累了。再次感謝 – TobiO