How to Export Rmarkdown File to HTML Document with Two Columns

How make 2 column layout in R markdown when rendering pdf?

New pandoc version have made this easier since my original answer. According to pandoc's manual, you can now specify classoptions directly in the YAML front matter:

---
classoption:
- twocolumn
---

The new div notation also allows for inserting two column sections anywhere in the document, working for most formats

:::::::::::::: {.columns}
::: {.column width="40%"}

contents...

:::
::: {.column width="60%"}

contents...

:::
::::::::::::::

Original answer

You can use the article option twocolumn to format the whole document in two columns. Add this to your yaml front matter:

---
output:
pdf_document:
pandoc_args: [
"-V", "classoption=twocolumn"
]
---

2 Column Report in R Markdown - Render HTML aside Data Frame

Although this is not a perfect solution, it is a place to get started: Yihui recently added HTML templates to knitr, and docco is a example two-column page: http://cran.r-project.org/web/packages/knitr/vignettes/docco-classic.html .

You can see the template file used for that output here: https://github.com/yihui/knitr/blob/master/inst/misc/docco-template.html.

Alternatively, you can try placing inline HTML right in your R Markdown chunks, but this is terribly hacky and you might feel like a bad person for doing it. We use results='asis' so that the cated HTML is rendered properly, and out.extra='' to ensure that the HTML used to generate the figures is generated right away, rather than the Markdown language for image inclusion.

```{r two-column, results='asis', echo=FALSE, out.extra=''}
library(knitr)
cat("<table class='container'><tr>")
cat("<td>")
plot( rnorm(10) )
cat("</td>")
cat("<td>")
kable( rnorm(10), format="html" )
cat("</td>")
cat("</tr></table>")
```

Calling knit on that should produce a 2 column layout for that particular chunk (although without any nice styling for the table; you might add that in yourself with some CSS)

Print more columns before break in r markdown html

You were so close, you need cols.print NOT columns.print and we need an extra YAML argument to allow for paging, to allow the user to see more of the data columns, if they want. I used 3 columns in my example to easily see the results.

---
title: "Untitled"
author: "Daniel"
date: "6/17/2021"
output:
html_document:
df_print: paged
---

```{r, cols.print=3}
mtcars
```

Sample Image

I would suggest using df_print: paged as it allows for more dynamic tables for your document.

Have two columns in Markdown

You can't, at least not with pure Markdown as it doesn't have any concept of columns. As explained in the rules:

The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

In fact, the best way would be to have each code block wrapped in a <div> with the appropriate class assigned to each div. However, most Markdown parsers do not parse Markdown inside a raw HTML block. Therefore, you may need to also define the code block in raw HTML as well. Check your parser's list of features to find out. In the event you are not able to define your own CSS to use separately from the Markdown (to style the HTML), you will also need to define the styles inline in the HTML. This question includes a nice sample of what that might look like. Just replace the comments with the appropriate code blocks. If you have to define your code blocks in raw HTML, they would look like this:

<pre><code class="language-c">int foo (void) 
{
int i;
}
</code></pre>

So, the final document that is sure to work in all (most?) Markdown parsers would look like this:

# Rule 1
Description for rule 1.

<div style="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2; -webkit-column-rule: 1px dotted #e0e0e0; -moz-column-rule: 1px dotted #e0e0e0; column-rule: 1px dotted #e0e0e0;">
<div style="display: inline-block;">
<h2>Good</h2>
<pre><code class="language-c">int foo (void)
{
int i;
}
</code></pre>
</div>
<div style="display: inline-block;">
<h2>Bad</h2>
<pre><code class="language-c">int foo (void) {
int i;
}
</code></pre>
</div>
</div>

Note that that uses one of many different ways of defining columns in CSS. Different methods may or may not work in different browsers. YMMV.

two-column layouts in RStudio presentations/slidify/pandoc

I now have what I think is a reasonable solution that should apply at least to ioslides-based solutions, and maybe (?) to other HTML5-based formats. Starting here, I added

<style>
div#before-column p.forceBreak {
break-before: column;
}
div#after-column p.forceBreak {
break-after: column;
}
</style>

to the beginning of my document; then putting <p class="forceBreak"></p> within a slide with {.columns-2} breaks the column at that point, e.g.

## Latin hypercube sampling {.columns-2}

- sample evenly, randomly across (potentially many) uncertain parameters

<p class="forceBreak"></p>

![](LHScrop.png)
[User:Saittam, Wikipedia](https://commons.wikimedia.org/wiki/File:LHSsampling.png#/media/File:LHSsampling.png)

There may be an even better way, but this isn't too painful.

@ChrisMerkord points out in comments that

.forceBreak { -webkit-column-break-after: always; break-after: column; }

worked instead (I haven't tested ...)

How can I change the number of columns several times in an R Markdown pdf document?

What you can do is first, to add -- as you stated -- pandoc_args: ... into your YAML header. Second, there are a few LaTeX solutions around (like this or this one) which won't work for RMarkdown. The only way I found so far is to use \onyecolumn / \twocolumn -- just with the drawback of the page breaks. But perhaps you can live with it until there's a better solution.

---
title: "Test"
output:
pdf_document:
pandoc_args: [
"-V", "classoption=twocolumn"
]
html_document: default
header-includes:
- \usepackage{lipsum} # just used for producing example text in document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Two columns of text
\lipsum[1-7]
\onecolumn

## Once column section.
This part should be the whole page width
```{r plot}
plot(rnorm(20),rnorm(20))
```
\lipsum[1]
\twocolumn

## Now 2 columns again
This section should go back to two columns
\lipsum

\begin{table*}
This is nice, but won't work with R chunks or headers. And you'll have to format with LaTeX code (e.g. \textbf{Foo blaah}).
\lipsum[1]
\end{table*}
\lipsum

Rmarkdown in two columns with .pull-left[] does not work twice

It seems to work if you use css: "ninjutsu":

YAML header

---
title: "reprex-left.right"
author: "Ramon Gallego"
date: "4/10/2020"
output:
xaringan::moon_reader:
css: "ninjutsu"
---

Code chunks:

    ```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```

```{css echo=FALSE}
.pull-left {
float: left;
width: 44%;
}
.pull-right {
float: right;
width: 44%;
}
.pull-right ~ p {
clear: both;
}
```

.pull-left[This is <br> the first text block.]
.pull-right[This is <br> the second <br> text block.]

.pull-left[This <br> is <br>text 3.]
.pull-right[This <br> is <br> <br> text 4.]

.pull-left[
This is text 5.]

.pull-right[This is text 6.]

.pull-left[
```{r}
# code #1 (past 6)
y <- data.frame(
A = LETTERS[1:5],
B = 1:5,
C = sqrt(6:10))
```
]
.pull-right[This is text 7.]


.pull-right[.full-width[.content-box-yellow[
```{r}
# code #2 (past 7)
y <- data.frame(
A = LETTERS[1:5],
B = 1:5,
C = sqrt(6:10))
```
]]]
.pull-left[.full-width[.content-box-white[This is text 8.]]]


.pull-left[.full-width[.content-box-white[
```{r}
# code #3 (after 8)
y <- data.frame(
A = LETTERS[1:5],
B = 1:5,
C = sqrt(6:10))
```
]]]

.pull-right[.full-width[.content-box-white[
```{r}
# code #4 (after c3)
y <- data.frame(
A = LETTERS[1:5],
B = 1:5,
C = sqrt(6:10))
```
]]]

Result:

table

output is getting printed two times in the file generated in rmarkdown

In general foreach returns its values, which will be printed. To suppress that you can wrap the foreach loop in a call to the invisible function. e.g.

---
title: ''
output: html_document
---



```{r echo=TRUE}
library(foreach)

foreach (i=1) %do% print(i) #prints twice
invisible(foreach (i=1) %do% print(i)) #prints once


```


Related Topics



Leave a reply



Submit