Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column break trick #89

Open
remlapmot opened this issue Jul 12, 2019 · 8 comments
Open

Column break trick #89

remlapmot opened this issue Jul 12, 2019 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@remlapmot
Copy link
Contributor

This isn't an issue but I thought I would post in case this is helpful for anyone else.

When using betterport I found that I needed a column break.

I got one by assigning a level 4 header to have a column break before it in my html template by adding the following CSS within the <style> tag:

.section h4 {
    break-before: column;
}

Then in my Rmd file I put a level 4 header #### where I want the column break.

Usually, in RMarkdown three or more asterisks or dashes indicate a horizontal rule or page break, so maybe it would have been better to assign the column break to that; but I didn't know how to do that and this seems to work.

PS. I have put my poster here:
https://github.com/remlapmot/mr2019-tom-palmer-poster

@brentthorne
Copy link
Owner

This is a great idea! I will probably implement it as a css clas to be added to whatever header or r code chunk a user wants. Something like:

.mybreak {
   break-before: column;
}

Then the user just needs to add the class within the .rmd like so:

# First Column

Some text.

# Second Column {.mybreak}

Some more text.

```{r, class.source="mybreak"}
plot(iris$Sepal.Length,
     iris$Sepal.Width,
     col=iris$Species,pch=19)
```

@remlapmot
Copy link
Contributor Author

That would be brilliant, thanks.

@brentthorne
Copy link
Owner

So far I have it working but am not sure how to get the class added to the <img> prduced by the chunk. For example when the chunk option echo=FALSE is set the output image of the iris plot doesn't retain the css class and does not break. Any chance you have ever added a class to a plot like this before? Maybe @RLesur or @yihui know a way 🤷‍♂️

@brentthorne brentthorne added the enhancement New feature or request label Aug 22, 2019
@brentthorne brentthorne self-assigned this Aug 22, 2019
@RLesur
Copy link

RLesur commented Aug 22, 2019

@remlapmot

Usually, in RMarkdown three or more asterisks or dashes indicate a horizontal rule or page break, so maybe it would have been better to assign the column break to that; but I didn't know how to do that and this seems to work.

These asterisks/dashes are transformed to <hr> elements. I think this declaration should do the trick:

hr {
  margin: 0;
  border: none;
  break-before: column;
}

@brentthorne

So far I have it working but am not sure how to get the class added to the <img> prduced by the chunk.

AFAIK, this is not possible. I have two ideas to add a class to the images.

1. Use knitr::hook_plot_html(). This hook adds two classes: rimage on the parent div and plot to the img element.

---
output: html_document
---

```{r setup, include=FALSE}
knitr::knit_hooks$set(plot = knitr::hook_plot_html)
```

2. This kind of plot hook to add a support for the class.output option. This is a draft I wrote quickly for you. I haven't performed a lot of tests:

---
output: html_document
---

```{r setup, include=FALSE}
knitr::knit_hooks$set(plot = function(x, options) {
  res <- knitr::hook_plot_md(x, options)
  if (is.null(options$class.output)) return(res)
  if (grepl('^<img', res)) {
    res <- sub('/>$', sprintf(' class="%s" />', options$class.output), res)
  }
  if (grepl('^!\\[', res)) {
    res <- sub(")", sprintf("){.%s}", options$class.output), res)
  }
  res
})
```

```{r, class.output="mybreak"}
plot(pressure)
```

@brentthorne
Copy link
Owner

So I tried it and the hook does work and ads the mybreak class to the <img> but I think the issue is that the <img> is wrapped inside of a classless <p> so I think the mybreak class would need to be added to that rather then the <img>, at least I think!

Thanks again for helping out so much @RLesur !

@RLesur
Copy link

RLesur commented Aug 23, 2019

You're welcome!
Maybe the laziest solution could be:

knitr::knit_hooks$set(plot = knitr::hook_plot_html)

and

.rimage {
   break-before: column;
}

@brentthorne
Copy link
Owner

brentthorne commented Aug 26, 2019

Tested and works 🚀 ! Thanks so much @RLesur ! I will be implementing this soon to the master branch. It will make poster manipulation much easier! 😄

And thank you @remlapmot for starting this conversation!

@brentthorne
Copy link
Owner

After playing around with this more I have realized that the automatic figure numbering is broken when this hook is used as well as inline references, ie @\ref(fig:myfigure) produces ?? in the document.

For now I will suggest that those who wish to have the break occur at begining of a code chunk which also prints out the code on the poster (echo=TRUE) they can add class.source="mybreak" to the code. If echo=FALSE then the can just add the <hr> like @RLesur suggested before (although this is not ideal for using child rmd files to auto generate posters).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants