[R] It is possible to use a Shell command inside a R script?

Alberto Monteiro albmont at centroin.com.br
Fri Aug 24 15:12:20 CEST 2007


Gabor Grothendieck wrote:
>
> What OS was that on?
>
I suppose you are asking me :-)
 
>> BTW, I found that using things directly in R is _much_
>> slower than creating a batch file and then running it.
>>
>> For example, I had a directory with misnamed mp3 files,
>> and I wanted to use R to rename and copy them
>> to another directory. I tried to use file.copy, but it
>> took too much time. Writing a batch file and then running
>> it was much faster.

I was Windows, of course. In Linux, I would use a one-line
script with find, mv, sed, etc.

If you want to test, do this:

# create subdiretory source_dir with 800 files
# (for simplicity's sake, all files with decent names: no
# spaces, no non-ascii characters, etc)
# create empty subdiretory dest1_dir
# create empty subdiretory dest2_dir
#

arr <- list.files("source_dir")
t0 <- Sys.time()
# 1st test: slow
for (x in arr)
  file.copy(paste("source_dir/", x, sep=""), "dest1_dir")
t1 <- Sys.time()
t1 - t0
# 2nd test: fast
sink("batch_file.bat")
for (x in arr)
  cat("copy source_dir\\", x, " dest2_dir\\\n", sep="")
sink()
system("batch_file.bat")
t2 <- Sys.time()
t2 - t1

Hmmm.... Interesting... I did this test with 47 files, and I got:

t1 - t0
Time difference of 11.439 secs

t2 - t1
Time difference of 11.969 secs

Oops...

Alberto Monteiro



More information about the R-help mailing list