pakzs pakzs - 1 year ago 63
R Question

Error in file with R

I am new to R and I have some issue with my script and I don't know why. The aim is to use all the script that are in a folder (default) or the ones definied by the user in arguments.

If I type the default:

RScript.exe Detection.r --detection ALL

The result looks like (no problem):

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

But if I manually define the scripts to use:

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

The result looks like this:

[1] "algo1" "algo2"

And I have this error:

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

I don't know why it does not work so any help is appreciated.

By the way, the code where is handle this:

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

Thanks in advance

Answer Source

The problem here is that strsplit does not return a parsed vector of items, but a list containing that parsed vector. This is because strplit can also deal with a list or vector as input (e.g. c('file1,file2,file3', 'file4,file5,file6')). In this case you do not need that functionality.

You can use unlist to cast the vector inside a list to only a vector. This makes the result identical to the output of list.files, which in turn should allow your code to work. For example:

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

You can also create a custom function:

simple_strsplit = function(...) {

which essentially passes on all its arguments ... directly to strplit, but calls unclass before returning the result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download