How to run same code on multiple files, or all files in directory

旧街凉风 提交于 2021-01-29 09:14:50

问题


so I am very new to coding and recently wrote a little program that involved R and sox. It looked like this

file <- "test.mp3"

testSox = paste("sox ",file," -n spectrogram -o ",file,".png stats",sep='')
sox = system(testSox, intern = TRUE)
print(sox)

Now, instead of assigning the one file manually within the code, I would just like to have this code read through all the mp3s in a folder automatically. Is this possible? Any help would be greatly appreciated. Thanks!

EDIT: Actually, I should add that I tried list.files, but when it comes to running the system() command, I get

"Error in system(command, as.integer(flag), f, stdout, stderr) : character string expected as first argument"

Here's the list.files code I tried:

> temp = list.files(path = ".", pattern=".mp3")
> 
> file <- temp
> 
> firstSox = paste("sox ",file," -n spectrogram -o ",file,".png stats",sep='')
> sox = system(firstSox, intern = TRUE)
Error in system(command, as.integer(flag), f, stdout, stderr) : 
  character string expected as first argument
> print(sox)

I'm guessing this is not the correct route to go? Because I basically need to replace 'file' in the firstSox line with each mp3 that's in the temp array. So instead of running:

file <- "test.mp3"

...I would just like to have it re-assign each time for every file in the folder..., so it runs through as test.mp3, then 1.mp3, then 2.mp3, then 3.mp, etc.

I've scoured the net, and just feel like I've hit a brick wall. As stated in the comments, I've read up on loops, but for some reason I can't wrap my head around how to incorporate it into what I have written. I feel like I just need someone to show me at least the way, or maybe even write me an example so I can wrap my head around it. Would greatly appreciate help and any tips on what I'm doing wrong and could correct. Thanks.


回答1:


Try the below code. I am using dir() instead of list.files, just because I find it easier. Remember there are many ways to do the same thing in R.

files <- dir(path = ".",pattern = ".mp3") #Get all the mp3 files

for(f in files) {     #Loop over the mp3 files one at a time

    firstSox = paste("sox ",f," -n spectrogram -o ",f,".png stats",sep='')
    sox = system(firstSox, intern = TRUE)
    print(sox)

}



回答2:


Your firstSox variable will be a vector of commands to run (paste will generate a vector, one string for each element of file). So now you just need to run each command through system

One way to do this and capture the output is to use the lapply or sapply function:

sox <- lapply( firstSox, function(x) system(x, intern=TRUE) )

In this code lapply will run the function for each element of firstSox one at a time, the function just takes the current element (in x) and passes that to system. Then lapply gathers all the outputs together and combines them into a list that it puts into sox.

If the results of each run give the same shape of results (single number or vector of same length) then you can use sapply instead and it will simplify the return into a vector or matrix.



来源:https://stackoverflow.com/questions/23745964/how-to-run-same-code-on-multiple-files-or-all-files-in-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!