I'm trying to execute a for-loop inside a r notebook chunk to print some messages and a plot interleaved, shown in the code below:
for(i in 1:2) {
print(paste0("before ", i))
plot(i, i)
print(paste0("after ", i))
}
However, the first plot appears where the second should have been and the second one after all messages have been printed. Looking for a solution I found this (Using loops to print output into Knitr)
for(i in 1:2) {
print(paste0("before ", i))
plot(i, i)
plot.new()
# This function (frame is an alias for plot.new) causes the completion of
# plotting in the current plot (if there is one) and an advance to a new
# graphics frame.
print(paste0("after ", i))
}
Which kinda fix it, but unfortunately empty plots are created for each valid plot. Does anyone know a method that causes the completion of plotting without creating a new graphic frame?