我正在和Markdown一起写一本小练习册。我想在列上绘制最终输出,在另一列上创建文档文本。 here和here解决了类似的问题。不幸的是,他们主要希望每列一个输出。我想在列上生成输出,在另一列上生成文本。真有意思的是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.
答案 0 :(得分:3)
当您要求列时,我的回答是:表格。
使用pipe_tables
,图形和文字可以彼此相邻。然而,这是有代价的:
管道表的单元格不能包含段落和列表等块元素,也不能跨越多行。
如果此限制可以接受,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.
虽然无法省略列标题,但如果需要,可以将它们留空。
请注意,我最初会抑制情节(fig.show = "hide"
),然后使用include_graphics
将其包含在内。否则,在情节之后会有一个新行,这会扰乱表格。
(在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 = " ", headerR = " ", # 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.")
找到widthL
和widthR
的正确值有些麻烦。这是因为它们的效果取决于单元格中的字符数,即MD文件中的图像文件名和alt
文本。