Hi I have a function called basic_plot(), which will generate a plot if the variable plot_function = TRUE else it return a NULL. it is as follows
plot_function = TRUE;
basic_plot = function() {
if(plot_function) {
par(mfrow = c(1,3))
plot(1:10,1:10,type = "o")
plot(10:1,1:10,type = "o")
plot(1:10,rep(5,10),type = "o")
} else {
NULL;
}
}
basic_plot();
should generate a plot with tree panels populated with some lines. This function along with the variable it depends on is embedded with in some other code. What I would like to know is how I can tell an if() statement if the plot has been drawn? for example
if(is.null(basic_plot())) {
print("I haven't drawn the plot yet")
} else {
print("I have drawn the plot and want to do more stuff.")
}
The problem with the above is if a function plots a graph it is considered a null. so this will never know when I draw the plot e.g
plot_function = TRUE;
is.null(basic_plot())
[1] TRUE
plot_function = FALSE;
is.null(basic_plot())
[1] TRUE
The true application for this is with in a shiny app but actually thought this could be a generic R query. I cannot return anything other than generate the plot in the basic_plot() function (avoiding the obvious return something after the plot is drawn). I am hoping for an alternative function to is.null() such as has this function does something or not?
Cheers, C