Tufte-style HTML with table in margin

I'm trying to make a Tufte-style HTML page with tables in the margin. Nothing seems to work. I've tried using a {marginfigure} chunk, an {r fig.margin=TRUE} chunk, and enclosing <div> and <span> with class .marginnote. None of these has put the table in the margin. I would like to use tint::tintHtml for output but I have also tried tufte::tufte_html.

Is this possible? If so, how?


1 Like

This would be much easier to help with if you produce a small, reproducible example of what isn't working for you. Since it will be in RMarkdown, please enclose the entire example in four backticks (```` at the beginning and end) to make it more readable.

Sure, thanks for the reminder! I guess I thought someone might just know :slight_smile:

title: "Tufte with table in margin"
output: tufte::tufte_html

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, message=FALSE)

I want this figure to be in the main text, and it is.

```{r fig.width=3.5, fig.height=3.5}
mtcars2 <- mtcars
mtcars2$am <- factor(
  mtcars$am, labels = c('automatic', 'manual')
ggplot(mtcars2, aes(hp, mpg, color = am)) +
  geom_point() + geom_smooth() +
  theme(legend.position = 'bottom')

I want this table to be in the margin next to the figure. 
Here are a few attempts.

This does not appear in the output at all.

mtcars2 %>% count(am) %>% knitr::kable()

This appears in the main body.

```{r fig.margin=TRUE}
mtcars2 %>% count(am) %>% knitr::kable()

I think you're getting outside of what tint and tufte can handle in the margins. You can get part of the way there by forcing html output on kable, but that produces extra <p> tags. Even if those are removed manually from the final HTML output, the "margin" gets way too wide, implying to me that the CSS isn't really set up to handle tables in the margin.

You might have better luck with a different table package, but I don't think there's going to be any straightforward way to handle it. @yihui would have the most definitive answer, though.

title: "Tint with table in margin"
    keep_md: yes
    self_contained: no

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, message=FALSE)

Here is an inline attempt:

`r tint::margin_note(knitr::kable(head(subset(mtcars, select = c("mpg", "cyl"))), format = "markdown"))`

And a second:

`r tint::margin_note(knitr::kable(head(subset(mtcars, select = c("mpg", "cyl"))), format = "html"))`

Did it work?

Not really.

OK, thanks for trying!