利用R-Markdown和Knitr创建动态报告(第二部分)

在上篇文章中,我们介绍了如何创建一个R-Markdown文档,并在文档中嵌入Plotly图。在本篇文章,我们将介绍如何通过chunk options来控制代码的输出。

像之前那篇文章所讲,如果想嵌入R代码做计算,需要在代码块内编辑。如下所示:

```{r Code Chunk, chunk options here...}
# R code here...
```

CHUNK OPTIONS

Knitr提供了很多方法来控制文档的最终输出。我们将着重介绍几个常见的。

ECHO

echo控制R代码块是否可见。设置为FALSE表示隐藏R代码,但会运行代码块并输出结果到文档。如果你只是为了显示而不需要运行一个代码块,设置eval = FALSE即可。

```{r Code chunk, echo = FALSE}
# R code will be evaluated but not shown in the final HTML document
# Userful when only plots / charts / text / output needs to be shown 
# and that generated it...
```

MESSAGE

message参数用于设置是否显示控制台输出的信息,message = FALSE表示不显示。

```{r Code Chunk, message = FALSE}
# R code 
# Messages from R-Console will not show up in final document
```

message = TRUE

library(plotly)

## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:graphics':
## 
##     layout

message = FALSE

library(plotly)

RESULTS

在代码块执行完后,控制结果的输出形式。包括文本、图表和图形的输出。有四个取值(更多细节参考http://yihui.name/knitr/options/):

  1. markup―标记显示

  2. asis―文本显示

  3. hold―末尾显示

  4. hide―隐藏

```{r Code Chunk, results = 'markup'}
# R code will be evaluated but not shown in the final HTML document
# Useful when only plots / charts / text output needs to be shown and not the
# code that generated it...
```

results = 'markup'

str(mtcars)

## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

results = 'asis'

 str(mtcars)

‘data.frame’: 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 … $ cyl : num 6 6 4 6 8 6 8 4 4 6 … $ disp: num 160 160 108 258 360 … $ hp : num 110 110 93 110 175 105 245 62 95 123 … $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 … $ wt : num 2.62 2.88 2.32 3.21 3.44 … $ qsec: num 16.5 17 18.6 19.4 17 … $ vs : num 0 0 1 1 0 1 0 1 1 1 … $ am : num 1 1 1 0 0 0 0 0 0 0 … $ gear: num 4 4 4 3 3 3 3 4 4 4 … $ carb: num 4 4 1 1 2 1 4 2 2 4 …

results = 'hide'

str(mtcars)

图相关选项

fig.*设置用来控制生成的文档中图表的显示情况。下面举例说明。更多细节查看http://yihui.name/knitr/options/

宽度和高度

宽度和高度分别由fig.widthfig.heigh参数控制,默认单位为英寸。如下(示例中用到的diamondsplotly包自带的数据集):

  1. 小图

```{r Code Chunk, fig.width = 2, fig.height = 2}
library(plotly)
library(ggplot2)
set.seed(100)
df <- diamonds[sample(1:nrow(diamonds), size = 2000), ]

plot_ly(df, x = x, y = price, mode = "markers", color = cut, size = z) %>% 
layout(title = "Diamonds"))
```
大图
```{r Code Chunk, fig.width = 8, fig.height = 8}
library(plotly)
library(ggplot2)
set.seed(100)
df <- diamonds[sample(1:nrow(diamonds), size = 2000), ]

plot_ly(df, x = x, y = price, mode = "markers", color = cut, size = z) %>% 
layout(title = "Diamonds"))
```

对齐方式

由graphics和ggplot2得到的图形对齐方式可通过fig.align = 'left'/'right'/'center'分别设置成左对齐,右对齐,居中。下图设置图形居中(默认是左对齐)

```{r Code Chunk, fig.align='center'}
library(ggplot2)
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
                 y = rnorm(30))
ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))

ggplot(df, aes(x = gp, y = y)) +
   geom_point() +
   geom_point(data = ds, aes(y = mean),
              colour = 'red', size = 3)
```

如果图形是由plot_ly()函数得到的,需要使用<div>标签。下图得到的文档支持图形在右侧(各个chunk options所代表的意义请参照上面)

```{r Code Chunk1, eval  = FALSE}
library(plotly)
library(ggplot2)
set.seed(100)
df <- diamonds[sample(1:nrow(diamonds), size = 2000), ]

plot_ly(df, x = x, y = price, mode = "markers", color = cut, size = z) %>% 
layout(title = "Diamonds"))
```
<div style="float: right">
```{r Code Chunk2, echo = FALSE, message = FALSE}
library(plotly)
library(ggplot2)
set.seed(100)
df <- diamonds[sample(1:nrow(diamonds), size = 2000), ]

plot_ly(df, x = x, y = price, mode = "markers", color = cut, size = z) %>% 
layout(title = "Diamonds"))
```
</div>

IFRAME

通过<iframe>标签可以将你Plotly账户中的图表嵌入到R―markdown文档里。只需指定src参数为图表的嵌入链接。更多详情可参考http://help.plot.ly/embed-graphs-in-websites/

本文由雪晴数据网负责翻译整理,原文请参考R-MARKDOWN AND KNITR TUTORIAL (PART 2)。转载请注明原文链接http://www.xueqing.cc/cms/article/101


你可能感兴趣的:(R-Markdown)