Skip to content

Commit

Permalink
Create ScriptingwithR.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ninascarpelli authored and atruskie committed May 8, 2020
1 parent 7852743 commit 3a47495
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/basics/ScriptingwithR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Scripting with R
Since most ecologists and biologists are more used to using R than shell, we provide here an R script to run *AP.exe*

## Set the directory containing the files
Assing using ``` <- ``` the folder where the audio files are located to the object ``` directory ```, like this:

``` directory <- "C:\\Temp\\Workshop" ```

## Storing the results
Assign (```<-```) now a directory (```base_output_directory```) to store the results when the analyses finish, like this:

``` base_output_directory <- "C:\\Temp\\Workshop\\BatchIndicesOutput" ```

## Listing the audio files inside the directory
Create a list with all the audio files inside the ```directory```. In this case, we indicated we want all the files with the extension *.wav* to be listed. If your files have a different extension, you'll need to indicate the right extension after ```pattern = ``` (and don't forget to put the extenstion between double quotes)

``` files <- list.files(directory, pattern = "*.wav", full.names = TRUE) ```

## Iterate through each file
Here we indicate we will do this whole process for each file in our list of files:
1. Get the filename
2. Create a folder for each file inside the ```base_output_directory```
3. Prepare the command, which means put together the bits that will form the command that R and the computer will understand
4. Execute the command - Run the analysis

```
for(file in files) {
message("Processing ", file)
# get just the name of the file
file_name <- basename(file)
# make a folder for results
output_directory <- normalizePath(file.path(base_output_directory, file_name))
dir.create(output_directory, recursive = TRUE)
# prepare command
command <- sprintf('audio2csv "%s" "Towsey.Acoustic.yml" "%s" ', file, output_directory)
# finally, execute the command
system2('C:\\AP\\AnalysisPrograms.exe', command)
}
```

0 comments on commit 3a47495

Please sign in to comment.