2017-04-21 39 views
1

我想列出大量符合模式的文件,我可以列出一個列表,然後使用lapply。在該R它與list.files很慢,但digging around我發現使用系統調用快速列出文件

system(find "\path\" -name "pattern") 

使之加快5倍左右。

的問題是,上面只能在終端,而R中它只返回我在做什麼錯

了「1單」?

+1

我認爲'system'需要命令是一個字符串,即'系統( 「找到 '路徑' -name '模式'」)' –

回答

1

你真的應該把報價圍繞整個命令,也如果你想將輸出保存到一個R對象/矢量,您可以使用一個選項叫做實習生與「真」值象下面這樣:

system('find "/Users/pradeepkumar" -name "*.R"',intern=T) 

輸出接收到的:

> system('find "/Users/pradeepkumar" -name "*.R"',intern=T) 
[1] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-10.R"                             
[2] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-2.R"                             
[3] "/Users/pradeepkumar/Desktop/company doc/g/code/SIPC-3.R" 
+0

由於, 一世噸工作! ...但是,只有我輸入全名纔有效。使用定義的變量(用於一般功能)將其分開,例如, 'system('find my_path -name my_pattern',intern = TRUE)'。我不知道如何管理單引號。也許一些'paste()'技巧? –

+1

你可以使用:'x < - 「*。R」'; '系統(sprintf('find「/ Users/pradeepkumar」-name「%s」',x))' – PKumar

+0

好吧,現在我設法用'raw_list < - system(sprintf('找到「%s」-name「%s」', path, pattern, intern = TRUE))'。 R將列出大量文件,但我無法以任何格式存儲所有這些文件。其他人的解決方案是使用'list.files()',但這種方式讓我回到了原點。 –