Let me suggest some style improvements. These will help you not only write arguably nicer code but also help you write the code in the first place as each problem will be disassembled into smaller easily solvable problems.
Note that some of these might be little advanced solutions.
First, define a function that will generate file name:
make_filename = function(label, number){
# these can be easily turned into parameters
suffix = "40264464"
dir = "/Users/jwolo/Documents/section_images/"
# doing this, putting it all on a single line or using pipe %>%
# is just matter of style
filename = paste(label, number, suffix, sep="_")
filename = paste0(filename, ".csv")
filename = file.path(dir, filename)
filename
}
To get all combinations of two elements, consider expand.grid
. This can be used together with mapply
to make names:
combinations = expand.grid("label"=string_label, "number"=string_number)
filenames = mapply(make_filename, combinations$label, combinations$number)
With another mapply
or if you make another function, all can be done in a single mapply
call:
save_element = function(element, label, number){
filename = make_filename(label, number)
write.csv(element, filename,row.names=TRUE)
}
combinations = expand.grid("label"=string_label, "number"=string_number)
mapply(save_element, myelement, combinations$label, combinations$number)
By wrapping the logic of individual steps into self-contained functions, the result is more readable, less bug-prone, code.
Explanation:
All the magic is in two functions, expand.grid
which produce a combination of input vectors into a data.frame
:
expand.grid(c("a","b"), c(1,2))
# Var1 Var2
#1 a 1
#2 b 1
#3 a 2
#4 b 2
By specifying the names of the input vectors, the columns are then named so we can point at them instead of using res[,1]
and res[,2]
which is less readable compared to res$letters
and res$numbers
.
The second magic is mapply
, which is just a different name for Map
function that you might know from different programming languages (such as Python
).
While it looks terrifying, mapply
is really simple. It just calls functions with first element of its inputs, then second element and so on. It is essentially:
for(i in 1:n)
fun(input1[i], input2[i], ..., inputk[i])
Just a bit smarter.