2016-11-07 34 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中的许多事情一样,一旦你知道它很容易;)。 –