1

I love Yihui's knitr package, and I know from the tutorials that it can put R into html documents. I want to go the other way -- I want to put some html into a grid graphics object. Is that possible in knitr, or any other library? Specifically, I am looking for a way to:

  • Style text using HTML (basic css and tables, mostly)

  • Wrap it up in a grob

  • Arrange/insert the grob using grid graphics, combining it with other grobs (ggplot charts, etc.)

I've searched on the knitr list and looked into grid graphics. It has a textGrob function but formatting options aren't robust enough for what I am looking for. There's a SO question that's in the ballpark but doesn't cover what I'm looking for. Here's a silly reproducible example that imitates the structure of my real document.

library(gridExtra)    
#minimal valid object
rect = rectGrob()

text1 <- textGrob(
  label = "textGrobs let you write text"
 ,x = unit(0.5, "npc"), y = unit(0.5, "npc")
 ,just = "centre"
 ,gp = gpar(col = "black", fontsize = 12)
)

text2 <- textGrob(
  label = "but is there a graphics object that takes HTML?"
 ,x = unit(0.5, "npc"), y = unit(0.5, "npc")
 ,just = "centre"
 ,gp = gpar(col = "black", fontsize = 12)
)

#first split the box
top_box <- arrangeGrob(text1, text2, ncol = 2)

#put that together into a column
left_column <- arrangeGrob(
  top_box, rect, rect
 ,nrow = 3
 ,heights = c(1, 2, 2)
)

grid.arrange(left_column, rect, widths = c(4/5, 1/5), ncol = 2)

I'd like to replace one of those empty rectangular grobs below with some text with HTML.

chart_grob

Thanks!

Community
  • 1
  • 1
Andrew
  • 9,090
  • 8
  • 46
  • 59

1 Answers1

3

It's not clear what you mean by "an HTML grob". There is no HTML formatted object in your code. Certainly there are functions that can produce HTML and you could send that text to a grid.text call and display the HTML. I worry, however, that you intend to have some package display the HTML browser-like as a formatted table. The html function in Hmisc sends its output to a real browser. latex in Hmisc is designed for display. The tableGrob function in 'gridExtra' won't take HTML as a formatting specification. You might get further by examining the code and experimenting with xtable and print.xtable in the xtable package, since it is capable of delivering HTML output. You can also explore the latexTabular function in 'Hmisc' or the tabular function in the 'tables' package.

IRTFM
  • 258,963
  • 21
  • 364
  • 487