2016-11-07 25 views
0

目標是使用文件夾中的所有腳本(默認)或用戶在參數中定義的腳本。帶R的文件中的錯誤

如果我輸入默認:

RScript.exe Detection.r --detection ALL 

結果看起來像(沒問題):

[1] "script1"    "script2" 
[3] "script3" 

但是如果我手動定義腳本中使用:

RScript.exe Detection.r --detection algo1,algo2 

結果如下所示:

[[1]] 
[1] "algo1" "algo2" 

而且我有這樣的錯誤:

Error in file(filename, "r", encoding = encoding) : 
argument 'description' incorrect 

我不知道爲什麼它不工作。

順便說一句,這裏是代碼處理這個問題:

if(opt$detectionMethods =='ALL') { 
    detectionMethods <- list.files(paste(projectBasePath, '/modules/detections', sep='')) 
    detectionMethods <- gsub("\\.r", "", detectionMethods) 
} else { 
    detectionMethods <- strsplit(opt$detectionMethods, ",") 
} 

回答

0

的這裏的問題是,strsplit不返回項的解析向量,但包含解析的載體列表。這是因爲strplit也可以處理列表或向量作爲輸入(例如c('file1,file2,file3', 'file4,file5,file6'))。在這種情況下,您不需要該功能。

您可以使用unlist將列表中的矢量僅轉換爲矢量。這使得結果與list.files的輸出相同,這反過來應該允許你的代碼工作。例如:

unlist(strsplit('file1,file2,file3', split = ',')) 
[1] "file1" "file2" "file3" 

您還可以創建自定義函數:

simple_strsplit = function(...) { 
    return(unlist(strsplit(...))) 
} 

基本上通過其所有參數...直接strplit,但返回結果之前調用unclass

+0

'unlist'在我的情況下就像一個魅力,不知道那是如此簡單。非常感謝 ! – pakzs

+0

與R中的許多事情一樣,一旦你知道它很容易;)。 –