6

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) ) 
})
```
7
  • 1
    i would use for(one.possibility in carb.possibilities){, instead of lapply, and the nprint(ggplot(...))
    – user20650
    Sep 30, 2016 at 13:40
  • 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? Sep 30, 2016 at 13:44
  • 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. Sep 30, 2016 at 13:46
  • 1
    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
    Sep 30, 2016 at 13:46
  • i dont see how you could interleave tables while joining plots together with grid.arrange.
    – user20650
    Sep 30, 2016 at 13:54

1 Answer 1

1

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)

}
```
1
  • one way to change the heights is to have an earlier chunk with the heights defined (ie heights <- aggregate(gear ~ carb, mtcars, function(x) length(unique(x)))$gear ; heights <- paste0(2*heights, 'in')), and then pass this to the out.height argument in the next chunk with the plots (ie. out.height=heights). However, I don't know how to get consistent plot widths. (although if width was constant then the plots would look stretched)
    – user20650
    Sep 30, 2016 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.