Ed G
Ed G

Reputation: 822

Using loops to print output into Knitr

Apologies for this question - I'm sure the answer is v simple

I have a couple of lists containing objects. I'm running them into HTML through knitr and want to control the order of the output.

This code prints all the list1 output, then all the list1 plots, then all the list2 output then all list2 plots

lapply(1:4, function(i){
  my.list1[[i]]
  plot(my.list1[[i]])
  my.list2[[i]]
  plot(my.list2[[i]])
})

I want it to print the first list1 output, then the first list1 plot then the first list2 output, then the first list2 plot. Then move onto the second interation of the loop.

Put another way, I want the function to complete all commands before iterating, rather than completing all iterations before moving to the next command.

Can this be done?

Thanks

Upvotes: 1

Views: 905

Answers (1)

agstudy
agstudy

Reputation: 121568

You should Adding a plot.new before/after calling the plot command to not plot in the same place and call print to force the display when you call it.

Try this for example:

lapply(1:4, function(i){
  print(my.list1[[i]])
  plot(my.list1[[i]])
  plot.new()
  print(my.list2[[i]])
  plot(my.list2[[i]])
  plot.new()
  })

Upvotes: 2

Related Questions