I'm starting to the terminal more on my machine and would like to create a workflow where I have greater control over more repetitive tasks. My question is, can I use the system
function, which takes a string and runs it through the terminal, within a loop? My hang up is looping over a string. My thought is to use the glue package, but I'm wondering if there is a base function for this or a better way than invoking an outside package?
library(glue)
setwd(path_to_files)
dir.create('pdf')
all_files <- list.files()
lapply(all_files, function(x){
system('sytsunoconv -f [format] pdf {x}.pptw')
})
Using system
within an iterative loop shouldn't be a problem. To create a vector of commands you can use base R's paste
or paste0
, although glue
can be useful when dealing with more complex strings.
Creating a vector of commands is as easy as paste("say", 1:5)
, which returns [1] "say 1" "say 2" "say 3" "say 4" "say 5"
. You can use it in a for
loop with system
to run each in sequence:
for (command in paste("say", 1:5)) {
system(command)
}
If your on a Mac you should hear the TTS pronouncing each number.
You should be able to use the same pattern for your example, e.g.:
for (command in paste0('sytsunoconv -f [format] pdf ', list.files(), ".pptw")) {
system(command)
}
Note that I use paste0
here to avoid unnecessary whitespace.