2016-04-23 41 views
2

我正在用Markdown編寫一本小練習冊。我希望將最後的輸出與列上的圖表和文檔文本相關聯。類似的問題被解決herehere。不幸的是,他們主要希望每列有一個輸出。我想在一列上生成輸出,在另一列上生成文本。真正有趣的是Docco,但它顯示了文本的代碼輸出。Markdown中的文本和代碼/輸出的單獨列

一個可能的解決方案是RPres減價橫向規則:使用***,它創建了兩個易於使用的列。但是我確實在Markdown文檔中找到了有關它的實現的文檔。

這裏顯示到目前爲止我的結果中的圖片和我的代碼示例:

enter image description here

```{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

你可能想要[這個反向](http://yihui.name/knitr/demo/graphics/)? –

+0

是的,類似的東西。 – Worice

回答

2

至於你要求的列,我的回答將是:表。使用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. 

Result

雖然列標題不能省略,你可以將它們留空如果需要的話。

請注意,我最初禁止繪圖(fig.show = "hide")並使用include_graphics以後包含它。否則,會在破壞表格的情節之後出現換行符。

(在knitr 1.12.3,include_graphics似乎並不與內嵌代碼塊正常工作。不過,目前development version 25年12月1日效果很好。)

擴展

我砍死在一起的延伸它允許使用單個塊來生成並顯示圖形和一些更多的功能:

```{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. 

Output

我知道setup看起來有點嚇人,但一旦FigureNextToText定義,它可以很簡單地叫,例如:

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

找到了正確的值widthLwidthR是有點麻煩。這是因爲它們的效果取決於單元中字符的數量,即MD文件中圖像的文件名以及文本的文件名。

+0

這種方法並不完美。即使它有點「硬」,它甚至是美麗的。我試圖安裝開發者版本,但它需要一個'pdfLatex'安裝,顯然沒有更多的處理。這似乎是我安裝的一個嚴重問題。無論如何,謝謝你的努力! – Worice

+0

@Worice我添加了一個不使用'include_graphics'的擴展,並且應該在沒有開發者版本的情況下工作。 –

+0

我很驚訝你的工作和能力,它非常好!我想你應該讓這個答案更容易被訪問,也許通過將你的代碼包含在一個包中? – Worice