I am just learning R and I am really baffled by the outcome of these lines in my R script:
myfunction <- function(pdbid,t,d,tabledir) {
command <- sprintf("sh ../addMeta.sh %s/tool%d-*-data%d-*/%s*.stats-table | tail -n +2",tabledir,t,d,pdbid)
print(command)
result <- read.table(pipe(command))
}
The above function is run for multiple instances of the parameter "pdbid" like this:
t3 <- apply(pdbids, 2, function(x) myfunction(as.character(x),t,d,as.character("somedironmymachine")))
When I run the R script, I get an error:
Error in pipe(command) : invalid 'description' argument
Googling this message lead me to two very different problems on Stack Overflow, and no enlightenment about what this message means. However, I thought it must be something with the pipe, took a look at the command in the pipe, that's why I print it above, and it looks good
[1] "sh ../5_runs/addMeta.sh ../5_runs/10-subopts-layers-alltools//tool2-*-data4-*/1DK1*.stats-table | tail -n +2"
[2] "sh ../5_runs/addMeta.sh ../5_runs/10-subopts-layers-alltools//tool2-*-data4-*/1ET4*.stats-table | tail -n +2"
[3] "sh ../5_runs/addMeta.sh ../5_runs/10-subopts-layers-alltools//tool2-*-data4-*/1F1T*.stats-table | tail -n +2"
[4] "sh ../5_runs/addMeta.sh ../5_runs/10-subopts-layers-alltools//tool2-*-data4-*/1I6U*.stats-table | tail -n +2"
[..]
and each of these commands runs without problems on the commandline outside R.
$ sh ../5_runs/addMeta.sh ../5_runs/10-subopts-layers-alltools//tool2-*-data4-*/2GDI*.stats-table | tail -n +2
-> correct result table from addMeta.sh
Even inside R it runs fine if I call it with system(command)
- so what's wrong for pipe
?
Also, in the past I used the pipe(command)
approach for very similar (special characters, chained commands) commands before and it worked as expected, though I never used it calling repeatedly via apply
.
How can I further debug this problem? Am I using apply
wrongly? If so, why is the command output correctly and runs without errors? Could it be some conversion/data format issue? If so, why does it work with system()
, but not pipe()
? Is there a way to use the output of system()
to read in data as a workaround? Are there restrictions for pipe()
in R that I am not aware of?
What does the mysterious error message invalid 'description' argument
mean exactly?