Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

Creating dynamic reports

This chapter covers

Publishing results to the web

Incorporating R results into Microsoft Word or Open Document reports

Creating dynamic reports, where changing the data changes the report

Creating publication quality documents with R, Markdown, and LaTeX

Welcome to the final chapter! You’ve accessed your data, cleaned it up, described its characteristics, modeled the relationships, and visualized the results. The next step is to

ARelax and perhaps go to Disney World.

BCommunicate the results to others.

If you chose A, please take me with you. If you chose B, welcome to the real world. Research doesn’t end when the last statistical analysis or graph is finished. You’ll almost always have to communicate the results to others. This means incorporating

the analyses into a report of some kind.

513

514

CHAPTER 22 Creating dynamic reports

There are three common report scenarios. In the first, you create a report that includes your code and the results, so that you can remember what you did six months from now. It’s easier to reconstruct what was done from a single comprehensive document than from a set of related files.

In the second scenario, you have to generate a report for a teacher, a supervisor, a client, a government agency, an internet audience, or a journal editor. Clarity and attractiveness matter, and the report may only need to be created once.

In the third scenario, you need to generate a specific type of report on a regular basis. It may be a monthly report on product or resource usage, a weekly financial analysis, or a report on web traffic that’s updated hourly. In any case, the data changes, but the analyses and the structure of the report remain the same.

One approach to incorporating R output into a report involves running the analyses, cutting and pasting each graph and text table into a word-processing document, and reformatting the results. This approach is usually time consuming, inefficient, and frustrating. Although R creates state-of-the-art graphics, its text output is woefully retro— tables of monospaced text with columns lined up using spaces. Reformatting them is no easy task. And if the data changes, you have to go through the entire process again!

Given these limitations, you may feel that R won’t work for you. Have no fear. (OK, have a little fear—it’s an important survival mechanism.) R offers several elegant solutions for incorporating R code and results into reports. Additionally, the data can be tied to the report so that changing the data changes the report. These dynamic reports can be saved as

Web pages

Microsoft Word documents

Open Document files

Publication-ready PDF or PostScript documents

For example, say you’re using regression analysis to study the relationship between weight and height in a sample of women. R allows you to take the monospaced output generated by the lm() function

> lm(weight ~ height, data=women)

Call:

lm(formula = weight ~ height, data = women)

Residuals:

 

 

 

 

Min

1Q

Median

3Q

Max

-1.7333 -1.1333 -0.3833

0.7417

3.1167

Coefficients:

 

 

 

 

 

Estimate Std. Error t value Pr(>|t|)

(Intercept) -87.51667

5.93694

-14.74 1.71e-09 ***

height

3.45000

0.09114

37.85 1.09e-14 ***

---

 

 

 

 

Signif. codes:

0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error:

1.525 on 13 degrees of freedom

Multiple R-squared: 0.991,

Adjusted R-squared: 0.9903

F-statistic:

1433 on 1 and 13 DF, p-value: 1.091e-14

A template approach to reports

515

Figure 22.1 Regression analysis saved to a web page

and turn it into a web page like the one in figure 22.1. In this chapter, you’ll learn how.

Dynamic documents and reproducible research

There is a powerful movement growing within the academic community in support of reproducible research. The goal of reproducible research is to facilitate the replication of scientific findings by including the data and software code necessary to reproduce findings with the publications that report them. This allows readers to verify the findings for themselves and gives them an opportunity to build on the results more directly in their own work. The techniques described in this chapter, including the embedding of data and source code with documents, directly support this effort.

22.1 A template approach to reports

The majority of this chapter employs a template approach to report generation. A report starts with a template file. The template contains the report text, formatting syntax, and R code chunks.

The template file is processed, the R code is run, the formatting syntax is applied, and a report is generated. How R output is included in the report is controlled by

516

CHAPTER 22 Creating dynamic reports

example.html

library(rmarkdown) render(example.Rmd, "html_document")

example.Rmd

# Report

Here is some data.

```{r}

head(women)

```

## Plots Here is a plot.

```{r fig.width=4, fig.height=4} with(women, plot(weight, height))

```

Figure 22.2 Creating a web page from a text file that includes Markdown syntax, report text, and R code chunks

options. A simple example using an R Markdown template to create a web page is given in figure 22.2.

The template file (example.Rmd) is a plain text file containing three components:

Report text—Any explanatory phrases and text. Here, the report text is Report,

Here is some data, Plots, and Here is a plot.

Formatting syntax—The tags that control report formatting. In this file, Markdown tags are used to format the results. Markdown is a simple markup language than can be used to convert plain text files to structurally valid HTML or XHTML. The pound symbol # in the first line isn’t a comment. It produces a level-1 header. ## produces a level-2 header, and so on.

R code—R statements to be executed. In R Markdown documents, R code chunks are surrounded by ```{r} and ```. The first code chunk lists the first six rows of the dataset, and the second code chunk produces a scatter plot. In this example, both the code and the results are output to the report, but options allow you to control what’s printed on a chunk-by-chunk basis.

The template file is passed to the render() function in the rmarkdown package, and a web page named example.html is created. The web page contains both the report text and R results.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]