2016-04-23 5 views
2

私はMarkdownで小さな運動帳を書いています。私は列のプロットともう片方の文書テキストを使って最終的な出力を得たいと思います。同様の問題はherehereとなります。残念なことに、彼らは主に列ごとに1つの出力を望んでいます。私は列に出力し、もう片方にテキストを出力したいと思います。本当に興味深いのはDoccoですが、それはテキストでコード出力を見せてくれるものです。Markdownでテキストとコード/出力の列を分ける

可能な解決策はRPresマークダウン水平ルールです:***を使用すると、使いやすい2つの列が作成されます。しかし、私は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 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. 

Output

私はwidthLwidthRの正しい値はやや面倒です見つけるsetupは少し怖いように見えますが、FigureNextToTextが定義されると、それは例えば、非常に簡単に呼び出すことができます。:

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

ことを知っています。これは、その効果がセル内の文字の数、すなわちMDファイル内の画像のファイル名およびaltテキストにも依存するためである。

+0

このアプローチは完璧ではありません。たとえそれがちょっと "硬い"ものであっても、それは美しいものです。私は開発者のバージョンをインストールしようとしましたが、明らかにもう処分されていない 'pdfLatex'インストールが必要でした。私のインストールの重大な問題であるようです。とにかくあなたの努力のためにありがとう! – Worice

+0

@Worice 'include_graphics'を使わない拡張機能を追加しました。開発者のバージョンがなくても動作します。 –

+0

私はあなたの仕事と能力に驚き、本当にうまくいきます!あなたのコードをパッケージに組み込むことで、この回答をより広く利用できるようにする必要があると思いますか? – Worice

関連する問題