lundi 12 août 2019

Printing multiple plots using if statement

I'm trying to produce a report using r markdown that I can toggle between color and black and white. I've created a set of graphs using ggplot and saved them to objects, with a color and black and white version of each. To toggle the report I've created separate variables.

color <- 1
bw <- 0

In the code chunks for my report sections, I'm using if statements to check the values of the variables above and print the graphs.

if(color > 0) graph1_color
if(color > 0) graph2_color
if(bw > 0) graph1_bw
if(bw > 0) graph2_bw

When I knit my report, all of my intended graphs show up. If i set color and bw <- 1, all graphs. If just color <- 1, both color graphs, and if just bw <- 1, both bw graphs.

My issue is when I have 6 or seven graphs and I dont want to write if(color > 0) graph1_color for each of the graphs. Instead I used the following:

if(color > 0) {
graph1_color
graph2_color
}
if(bw > 0) {
graph1_bw
graph2_bw
}

But when I do this, only the last graph prints, eg graph2 prints but not graph1. What do I need to change in my if statement to get all of the graphs within the squiggle brackets to show?

Aucun commentaire:

Enregistrer un commentaire