Markdown 中文本和 code/output 的单独列

Separate columns for text and code/output in Markdown

我正在用 Markdown 写一本小练习册。我希望最终输出的一列是图表,另一列是文档文本。解决了类似的问题 here and here. Unfortunately, they mainly desire one output per column. I would like to produce the output on a column and the text on the other. Really interesting is Docco,但它显然显示了带有文本的代码输出。

一个可能的解决方案是 RPres 降价水平规则:使用 ***,它创建两个易于使用的列。但我确实在 Markdown 文档中找到了有关其实现的文档。

这是一张显示我到目前为止的结果的图片和我的代码示例:

```{r setoption, cache=TRUE, warning=FALSE, message=FALSE, fig.width=12}
knitr::opts_chunk$set(cache=TRUE, warning=FALSE, message=FALSE, fig.width=4,     echo = FALSE)
```

```{r, loadlibraries}
library(knitr)
library(lattice)
```

### Exercise 1 - 22/4/'16

 Is the data set shown in the following figure symmetric or skewed? How many modes does this data set have?

```{r 1.1}
e1 <- rep(seq(1, 6, 1), c(6, 4, 2, 2, 4, 6))
barchart(table(e1), horizontal = FALSE, xlab = "", ylab = "Frequency")
```
**Solution:**
The data set is symmetric. Furthermore, it has two modes.

### Exercise 2 - 22/4/'16

Describe the shape of the dataset shown in the following figure.
```{r 2.1}
e2 <- rep(seq(1, 9, 1), c(6, 5, 4, 3, 2, 1, 1, 1, 1))
barchart(table(e2), ylab = "Frequency", horizontal = FALSE)
```
**Solution:**
The dataset is right skewed, also said right skewed, with one mode.

正如您要求的专栏,我的回答是:table。

使用pipe_tables,图文可以并排显示。然而,这是有代价的:

The cells of pipe tables cannot contain block elements like paragraphs and lists, and cannot span multiple lines.

如果接受这个限制table,pipe_tables提供一个非常简单的解决方案:

```{r img, fig.show = "hide", echo = FALSE}
library(knitr)
hist(rnorm(1000))    
```

Figure|Explanation
-------------------------------|-------------------------
`r include_graphics(paste0(opts_chunk$get("fig.path"), "img-1.png"))`|Histogram of 1000 draws from a standard normal density.

虽然 headers 列不能省略,但您可以根据需要将其留空。

请注意,我最初抑制了情节 (fig.show = "hide"),然后使用 include_graphics 将其包括在内。否则,情节后会有一个换行符会破坏 table.

(在 knitr 1.12.3 中,include_graphics 似乎无法与内联代码块一起正常工作。但是,当前的 development version 1.12.25 工作正常。)

扩展

我一起破解了一个扩展,允许使用单个块来生成和显示绘图和一些更多功能:

```{r setup, echo = FALSE}
library(knitr)
FigureNextToText <- function(number, # number of plot in chunk
                             text, 
                             alt = "", # alternative text for image
                             label = opts_current$get("label"), # set explicitly when using inline!
                             ext = ".png",
                             headerL = "&nbsp;", headerR = "&nbsp;", # empty string confuses pandoc if only right header is set
                             widthL = 30, widthR = 30,
                             ...) {
  path <- fig_chunk(label = label, ext = ext, number = number, ...)
  template <- "%s|%s
%s|%s
![%s](%s)|%s\r\n\r\n"
  output <- sprintf(
    template,
    headerL, headerR,
    paste0(rep("-", widthL), collapse = ""), paste0(rep("-", widthR), collapse = ""),
    alt, path, text
    )
  return(asis_output(output))
}
```

```{r img, fig.show = "hide", echo = FALSE, results = "asis"}
library(knitr)
hist(rnorm(1000))
hist(runif(n = 1000, min = 0, max = 10))

FigureNextToText(1, text = "Histogram of draws from standard normal density.", widthL = 50, widthR = 10)
FigureNextToText(2, text = "Histogram of draws from uniform distribution.", headerR = "Explanation", alt = "Histogram 2.") 
```

Some text.

`r FigureNextToText(2, text = "The same plot, this time inline.", label = "img", headerR = "Explanation", alt = "Histogram 2.")`

Some more text.

我知道setup看起来有点吓人,但是一旦定义了FigureNextToText,就可以很简单地调用它,例如:

FigureNextToText(2, text = "Histogram of draws from uniform distribution.", headerR = "Explanation", alt = "Histogram 2.")

widthLwidthR 找到正确的值有些麻烦。这是因为它们的效果取决于单元格中的字符数,即 MD 文件中图像的文件名和 alt 文本。