Reputation: 463
I have a function which generates a plot:
function(x, y,"name")
I would like to generate as many plots as "names" present in character z
z
[1] "Bzw1" "Dnajc1" "Ppig" "Prex1" "Dpm1" "Prpf38b"
[7] "Snrnp70" "Spty2d1" "Cbl" "Anxa2" "Ggnbp2" "Cltc"
and save all of them in a working directory.
Upvotes: 0
Views: 62
Reputation: 513
Voted for above answer. However this is one particular place where a for loop is not a bad choice either. As in:
z <- c("Bronx","Staten","Tribeca")
func <- function(i,z){
filename <- paste0("plot_",i,".png")
png(filename)
plot(1:30,rep(which(i==z),30)) # just getting 3 simple plots to print
dev.off()}
for (i in z) func(i,z)
# You will find the plots as .png files in your working directory
# with "plot_" prepended to each name
Upvotes: 1
Reputation: 1656
Suppose your function is custom_plot
. Then, try with this:
lapply(X = z,
FUN = function(name)
{
png(filename = paste0(name, ".png"))
custom_plot(x, y, name)
dev.off()
})
Upvotes: 1