4
votes

I have a R Markdown/knittr document that generates pairs of plots and tables. The number of pairs is variable, so I create them inside of a loop. I would like the results to be interleaved in the output: table 1, plot 1, table 2, plot 2...

In the example below, all tables come first at the top of the document. I have tried various permutations of

  • pander or kable as my table function
  • wrapping the table function in print or leaving it bare
  • with or without results='asis'

EDIT: I found a solution and posted it below. Now I'm looking for one that's compatible with the great variable-height advice I received in custom R Markdown plot size within loop ?

```{r cars, echo=FALSE}
library(ggplot2)
library(knitr)

carb.possibilities <- sort(unique(as.character(mtcars$carb)))

filtereds <- lapply(carb.possibilities, function(carb.ct) {
  return(mtcars[ mtcars$carb == carb.ct , ])
})

carb.possibilities <- paste(carb.possibilities, ' Carburetors', sep = '')

names(filtereds) <- carb.possibilities

lapply(carb.possibilities, function(one.possibility) {

  current.possibility <- filtereds[[one.possibility]]

  print(kable(current.possibility))

  ggplot(current.possibility, aes(factor(gear), mpg)) + 
    coord_flip() + 
    labs(x = "Gears", title = one.possibility) +
    geom_point(position=position_jitter( width = 0.1, height = 0.1) ) 
})
```
1
i would use for(one.possibility in carb.possibilities){, instead of lapply, and the nprint(ggplot(...))user20650
Great, that definitely interleaves the tables and plots, as long as the table is wrapped in print. Unfortunately, that means that the tables lose their nice kable formatting. PS, why do you prefer a for loop instead of lappy?Mark Miller
Also, I don’t see how I could use this along with the outside-of-loop do.call(grid.arrange...) in the answer I linked.Mark Miller
I would prefer a loop as you are just printing rather than returning a value. Plus using lapply with print seems to return the ggplot object.user20650
i dont see how you could interleave tables while joining plots together with grid.arrange.user20650

1 Answers

1
votes

Using asis, wrapping both the table and plot in print() and cat'ing a linefeed solve the interleaving problem. I haven’t figured how to combine this with the variable height plots from custom R Markdown plot size within loop

See https://github.com/yihui/knitr/issues/886

```{r cars, echo=FALSE, results='asis'}
library(ggplot2)
library(knitr)

carb.possibilities <- sort(unique(as.character(mtcars$carb)))

filtereds <- lapply(carb.possibilities, function(carb.ct) {
  return(mtcars[ mtcars$carb == carb.ct , ])
})

carb.possibilities <- paste(carb.possibilities, ' Carburetors', sep = '')

names(filtereds) <- carb.possibilities

for(one.possibility in carb.possibilities){

  current.possibility <- filtereds[[one.possibility]]

  my.ggplot <- ggplot(current.possibility, aes(factor(gear), mpg)) +
    coord_flip() +
    labs(x = "Gears", title = one.possibility) +
    geom_point(position=position_jitter( width = 0.1, height = 0.1) )

  print(kable(current.possibility))

  cat('\n')

  print(my.ggplot)

}
```