R Markdown中的2列部分

bry*_*anR 42 html r r-markdown

我是R Markdown的新手,我正在为我的工作中的一些新R用户组建一个R Markdown HTML页面,给他们一个介绍,并通​​过一些简单的演示.在展示像head和之类的东西的同时tail,它看起来很混乱,因为它会逐个打印出每个输出.只要我的.Rmd的其他部分被分成两列,我希望它们.在我的研究中,我遇到了这个问题:R Markdown中的2列报告 - 将HTML放在数据框之外.有一些关于HTML代码的变通方法的讨论,但我不是在HTML或CSS中的那个级别.我确实试过包括

<div class="columns-2">
</div>

从官方rmarkdown文档,但它没有任何影响

当我准备放弃时,@Molx对Stack Overflow问题发表评论说你可以将列分开***,但没有给出任何进一步的解释.我在几个方面试了一下:我***把R代码块中间包含在内,我将R代码块分开并放在两者***之间.当我做后者时,它***只是成为一个横向规则而对列没有任何作用.

我希望尽可能避免使用表格和CSS.如果有人对此有任何想法,我会很感激.

raw*_*awr 51

rmarkdown文件:

#### Put in your css file or directly in rmarkdown

<style>
  .col2 {
    columns: 2 200px;         /* number of columns and width in pixels*/
    -webkit-columns: 2 200px; /* chrome, safari */
    -moz-columns: 2 200px;    /* firefox */
  }
  .col3 {
    columns: 3 100px;
    -webkit-columns: 3 100px;
    -moz-columns: 3 100px;
  }
</style>

#### This section will have three columns

<div class="col3">
**1** one  
**2** two  
**3** three  
**4** four  
**5** five  
**6** six  
**7** seven  
**8** eight  
**9** nine  
</div>

#### This section will have two columns

<div class="col2">
```{r}
head(mtcars)
tail(mtcars)
```
</div>
Run Code Online (Sandbox Code Playgroud)

给我这个

在此输入图像描述


编辑

为了更精确地使用列元素,您可以为每组元素使用div:

Rmd文件

<style>
.column-left{
  float: left;
  width: 33%;
  text-align: left;
}
.column-center{
  display: inline-block;
  width: 33%;
  text-align: center;
}
.column-right{
  float: right;
  width: 33%;
  text-align: right;
}
</style>

#### This section will have three columns

<div class="column-left">
**1** one  
**2** two  
</div>
<div class="column-center">
**3** three  
**4** four  
**5** five  
**6** six  
</div>
<div class="column-right">
**7** seven  
**8** eight  
**9** nine  
</div>
Run Code Online (Sandbox Code Playgroud)

给我

在此输入图像描述

  • @RockScience已更新,为您的问题提供了更好的解决方案 (2认同)

Err*_*ard 18

来自rawr的自定义css解决方案很好,但如果您想要更多自定义并完全避免使用显式css,还有另一种方法.由于markdown使用Bootstrap-layout,我们可以使用Bootstraps网格布局进行详细设计:

唯一的缺点是额外的几个html标签

例:

---
title: "test"
author: "Testperson"
output:
  html_document
---

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

<div class = "row">
<div class = "col-md-6">
```{r cars,  warning = FALSE, echo = FALSE, dev=c('svg')}
plot(pressure)
```
</div>
<div class = "col-md-6">
```{r pressure, warning = FALSE, echo=FALSE, dev=c('svg')}
plot(pressure)
```
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @ktyagi ofc:https://getbootstrap.com/docs/4.1/getting-started/introduction/ (2认同)

小智 8

如果要导出为pdf,可以在带有包含的标题中执行此操作.

在不使用css文件的情况下,我使用以下方法创建了一个双列环境.

1st:我创建了文件header.tex.header.tex包括以下声明:

\usepackage{multicol}
\newcommand{\btwocol}{\begin{multicols}{2}}
\newcommand{\etwocol}{\end{multicols}}
Run Code Online (Sandbox Code Playgroud)

第二:我将以下命令放入我的文档标题中

---
title: "My title"
author: "My name"
date: "Today"
output:
    beamer_presentation:
        highlight: haddock
    includes:
        in_header: header.tex
        keep_tex: yes
---
Run Code Online (Sandbox Code Playgroud)

这是一个正文的例子和两列输出的图片.

***********
\btwocol
```{r, results="asis"}
print("test")
```

Here is some text that also is in two column environment.
\etwocol

Now only one column
Run Code Online (Sandbox Code Playgroud)

这是幻灯片的样子: 在此输入图像描述

  • 你能展示如何把这个输出变成pdf吗?我无法让它工作。 (2认同)

alg*_*ant 5

用于创建多列的CSS解决方案不允许控制列中断发生的位置.似乎会自动插入列分隔符以在列之间均匀分布内容,这并不总是您想要的.
markdown和rmarkdown中的"***"符号插入水平换行符,而不是新列.

除了用于幻灯片演示的.Rmd格式外,Rstudio还提供.Rpres幻灯片演示格式(Rpresentations).Rpresentations使用略微不同的markdown风格,其中"***"符号插入新列.

以下是RStudio对Rpresentations介绍的链接:
两列布局
创作R演示文稿

下面是与您的类似的StackOverflow问题的链接:
RStudio中的两列布局Markdown中的
两列布局

Rpresentation格式的最大缺点是它不支持用于交互式可视化的嵌入式闪亮应用程序.但是Rpresentation确实支持交互式webgl图.下面是一个简单的例子.您可以将其保存到.Rpres文件中,在RStudio中打开它,然后将其编译为HTML幻灯片演示文稿.请注意最后一张幻灯片中的交互式webgl图,您可以使用鼠标进行操作.

Simple R Presentation
========================================================
title: "Simple R Presentation"
author: John Doe
date: `r format(Sys.time(), "%m/%d/%Y")`
width: 1900
height: 1000
```{r setup, include=FALSE}
# This is an R setup chunk, containing default options applied to all other chunks
library(knitr)
# This sets the chunk default options
opts_chunk$set(cache=TRUE, collapse=TRUE, error=FALSE, prompt=TRUE)
# This sets the chunk display theme
thm <- knit_theme$get("acid")
knit_theme$set(thm)
# This sets some display options
options(digits=3)
options(width=80)
```


My First Slide
========================================================
Hello World!  
Creating Rpresentations isn't difficult at all!  

<img src="https://community.filemaker.com/servlet/JiveServlet/showImage/2-180549-7694/staples-easy-button.png" width="500" height="500" />


***

The Cauchy-Schwarz Inequality:  

$$
\left( \sum_{k=1}^n a_k b_k \right)^2 
\leq 
\left( \sum_{k=1}^n a_k^2 \right) 
\left( \sum_{k=1}^n b_k^2 \right) 
$$



Slide With R Code Chunk and Output in Two Columns
========================================================

First column contains simple R code that returns the summary of the cars data frame:  
```{r, summ_cars, eval=FALSE, echo=TRUE, results="hold", size="tiny"}
summary(cars)
```
***
Second column contains the output of the code in the first column:  
```{r, summ_cars, eval=TRUE, echo=FALSE, size="tiny"}
```



Slide With Plot
========================================================

First column with R code:  
```{r, plot_cars, eval=TRUE, echo=(-(1:1)), fig.show="hide"}
par(cex.lab=1.5, cex.axis=1.5, cex.main=1.5, cex.sub=1.5)
plot(cars)
```

***

Second column with plot:  
```{r, plot_cars, eval=TRUE, echo=FALSE, fig.width=10, fig.height=8}
```



Slide with Interactive 3d Surface Plot
========================================================

First column with R code:  
```{r, rgl_surf3d, eval=FALSE, echo=TRUE, webgl=TRUE, fig.show="hide"}
library(rgl)  # load rgl
knit_hooks$set(webgl=hook_webgl)
# define function of two variables
foo <- function(x, y) y*sin(x)
# draw 3d surface plot of function
persp3d(x=foo, xlim=c(-5, 5), ylim=c(-5, 5), col="green", axes=FALSE)
```

***

Second column with plot:  
```{r, rgl_surf3d, eval=TRUE, echo=FALSE, webgl=TRUE, fig.width=10, fig.height=8}
```
Run Code Online (Sandbox Code Playgroud)


Gra*_*ant 5

此主题逾期的贡献,但只是想指出,你可以结合@rawr和@艾莉森的答案启用多列部分 HTML和PDF。Rmarkdown / knitr非常聪明,可以根据所需的输出格式仅解析相关命令。我发现我经常将同一文档编织成多种格式,因此非常方便。

这是一个充实的示例:https : //github.com/grantmcdermott/two-col-test