Reputation: 3
I have an RScript file, which consist of 28 sections, which each one define seven variables named identically. Example:
# SECTION 1 ######
variable1 <- numericalvalue1
variable2 <- numericalvalue2
. . . (etc)
variable7 <- numericalvalue7
# SECTION 2 ######
variable1 <- someothernumericalvalue1
variable2 <- someothernumericalvalue2
. . . (etc)
variable7 <- someothernumericalvalue7
. . . (etc etc etc)
# SECTION 28 ######
variable1 <- someothernumericalvalueX
variable2 <- someothernumericalvalueY
. . . (etc)
variable7 <- someothernumericalvalueZ
After these different setups for the values of the variables, each section needs to be run with a part that is common for all of them. This last part basically draws a ggplot using the values set in each of the sections, which will mean that it produces 28 plots.
my_plot <- ggplot() +
geom_point() +
geom_circle(aes(x0=2, y0=2, r=2), fill=variable1) +
geom_circle(aes(x0=2, y0=2, r=2), fill=variable2) +
geom_circle(aes(x0=2, y0=2, r=2), fill=variable3) +
coord_fixed()
png(filename="MyPlot_iteration1.png")
my_plot
graphics.off()
The thing is that it can get burdensome to manually highlight each section, and then run the rest of the code, plus also manually naming each saved file with the section number. Especially if there would be 280 sections instead of the 28 that I have now.
So my question is: Is there a way to create a for loop that could iterate through the sections in the same Rscript file?
In my python-oriented mind it should be something like:
#this is just something to illustrate the idea
for x in range(1,29):
for section x:
run section x
run the common plotting part
save with a name that has the format MyPlot_x.png
Is this achievable with R, preferrably Base R?
Thank you very much in advance!
Upvotes: 0
Views: 39