Part 09: Making a report

What’s on this page?

  1. Reports and RMarkdown
  2. How to generate a report
  3. Some basic Markdown
  4. Generate the report
  5. To do on your own
  6. Quiz

What to do

Work through the examples to learn how create and generate reports for your homework within R Commander. Although you can copy/paste to a word document, insert images, edit, then save to a pdf, this is an inefficient and error-prone process. Far better to struggle a bit to learn RMarkdown, the benefits later with more challenging homework will be worth the effort.

How to do it

For these exercises, you may work

  • within the Rcmdr script window
  • RStudio

Choose one way. The quickest way is to just work with R Commander, but RStudio and RMarkdown work well together.

Let’s begin.

1. Reports and RMarkdown

An important component of your work in this class is to make reports. Your Homework will be submitted as pdf reports. PDF files have the advantage that they maintain formatting so that pdf documents appear the same on any device. Moreover, pdf documents are harder to manipulate compared to Word documents, which maintains the integrity of homework. Thus, it is an important skill to learn, how to generate pdf reports from your work in statistics. Although it is simple enough to copy and paste your work from R into a word document, then generate a pdf — and you have likely done this many times before — I want you to learn the advantages of Markdown when creating technical reports.

R Commander (and RStudio) use RMarkdown, which does a great job of capturing and presenting your work with a minimum of effort. RMarkdown is a version of Markdown, a markup language to help you create formatted text using a plain-text editor (see Wikipedia). Note that while you can copy and paste your work from R output to Word document, which is something we used to do, the advantage of RMarkdown is it preserves your code. In practice then, you can run code from a Markdown Report — an advantage for producing reproducible code. RMarkdown helps us avoid this copy/paste step to a different app, and does much better at preserving the work we do in R, as you will quickly learn if you try and rely on the copy/paste route!

In R Commander, once you are ready to generate a report, click on the R Markdown tab (blue arrow) to bring up the Markdown text window (Fig. 1).

RMarkdown in Rcmdr

Figure 1. R Markdown text window

2. How to generate a report

Good practice tip: When you first open R Commander, click on the R Markdown tab and replace title: and author: with a descriptive title for your report and your name (leave the quotes) (Fig. 1).

----
title: "My report"
author: "Mike Dohm"

Then, click on the R Script tab and proceed with work on your R project.

At the conclusion of your work, click on the R Markdown tab and scroll through your text. You can delete or add text as you please. To retain R commands and output, just don’t edit anywhere between ```{r} and ```

```{r}
[some R command]
```

making changes to the markdown template (e.g., adding additional text to answer questions, etc.), then click Generate report (Fig. 1, red arrow)

3. Some basic Markdown

While you can copy/paste from R output to Google Doc or MS Word, I want you to use RMarkdown to craft your responses to homework. RMarkdown captures your script and R output in one form; it is simple to insert your own text. The result is a single organized document. I do expect you to remove “mistakes” (incorrect function calls because of typos, etc.,) from your R Markdown text before you generate a report. Your work will look better if you apply a bit of Markdown syntax. Alternatively, you can use html syntax — Markdown is just simple text. While you can do all of the edits you need within the Rcmdr editor, many useful Markdown editors available (e.g., PanWriter, MarkText), which makes working with Markdown files simple once you get used to looking at the markdown language.

Note: RMarkdown includes formatting not part of the basic Markdown language; neither PanWriter nor MarkText perserve RMarkdown edits.

Really, to make an elegant report, all the “markdown” you need is how to include headings. In html-CSS, headings are coded with tags (Table 1).

Table 1. html-css tags with markdown equivalents

html tag markdown description
<h1> some text </h1> # some text

primary (main) heading 1

<h2> some text </h2> ## some text

subheading 2

<h3> some text </h3> ### some text

subheading 3

<h4> some text </h4> #### some text

subheading 4

<h5> some text </h5> ##### some text
subheading 5
<h6> some text </h6> ###### some text
subheading 6

the basics you need to know are

 

We’ll improve with markdown as the semester unfolds, but I recommend you start with one of many free online tutorials, e.g., https://www.markdowntutorial.com/

Here, I’ll step you through a mock homework. The mock homework questions

Question 1. When you exit R and Rcmdr, you are prompted to save a number of files. Make two lists: files exit from Rcmdr, files exit from R

Question 2. Which file(s) capture all of your work, including mistakes?

Hint: See Part 10: Exiting R and Rcmdr

Question 3. Load the dataset DNase (package dataset) and make a scatterplot of density by conc, include by groups

Here’s my markdown script. Text I entered into the markdown script file marked in yellow.


---
title: "Part 09: Making a report"
author: "Mike Dohm"
date: "`r Sys.Date()`" # Uses current date
---


```{r echo=FALSE, message=FALSE}
# include this code chunk as-is to set options
knitr::opts_chunk$set(comment=NA, prompt=TRUE)
library(Rcmdr)
library(car)
library(RcmdrMisc)
```


```{r echo=FALSE}
# include this code chunk as-is to enable 3D graphs
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)
```

### Question 1

#### Exit Rcmdr only

1. script file
2. R Markdown file
3. Output file

#### Exit R
1. Workspace image

### Question 2

Workspace image. Available in .Rhistory

### Question 3

```{r}
data(DNase, package="datasets")
```

```{r}
scatterplot(density~conc | Run, regLine=FALSE, smooth=FALSE, boxplots=FALSE,
by.groups=TRUE, data=DNase)
```

### End of report

The rendered document from this markdown script is shown in Figure 3. Note I used html (<h3>, <p>) and Markdown (ordered list). Note the simple way to hide R function calls, just a little markdown code

```{r echo=FALSE}

above the command hides the command, whereas

```{r}

ensures that the commands are printed.

Note that it helps to add space before and after ### and text blocks so that they render properly. Results look like Figure 3 below.

I do encourage you to explore Markdown — it’s really handy and simple to learn. You can use the editor included in Rcmdr (or RStudio, which is really nice for generating markdown reports), or you can copy the script into your favorite text editor (e.g., Notepad, TextEdit), make your changes, then copy back to Rcmdr before rendering. See https://en.wikipedia.org/wiki/Markdown#Example for some suggestions. A nice tutorial is available at https://www.markdowntutorial.com/.

4. Generate the report

Submit homework in BI311 as pdf files. The expectation is that you work with RMarkdown.

For full function, pandoc and LaTeX installed on your computer is recommended. Check by clicking on Rcmdr: Tools > and look for a submenu choice: Install other software (if you don’t see this option it means you already have pandoc and LaTeX available on your computer).

Figure 2. Screenshot of format options available for a computer with working LaTex and pandoc.

Figure 2. Screenshot of format options available for a computer with working LaTex and pandoc.

Note: This message is for those of you who have successfully installed R and are running R Commander, but may be having problems installing pandoc or LaTeX. While there’s some advantage to getting pandoc etc working, it is not essential for BI311 work.  Assuming you have Rcmdr and RcmdrMisc installed, and if you have started Rcmdr and have it up and running, then we can skip pandoc and LaTeX installation and use features of your browser to save to pdf.

R Markdown by default will print to a web page (an html document called RcmdrMarkdown.html) and display it in your default browser. To meet requirements of BI311 — you submit pdf files — we can print the html document generated from “Generate Report” in R Commander to a pdf.

  • Chrome browser, right click in the web page, from the popup menu select Print, then change destination to Save as pdf.
  • Safari browser, right click then select Print page (or if an option, Save page as pdf), then find at lower left find PDF and option to Save as PDF.

screenshot rendered markdown file mock homework

Figure 3. Screenshot of rendered RMarkdown script for the mock homework.

5. To do on your own

Repeat my mock homework to confirm you can generate a markdown file that looks like mine.

Select work done from any one (or more) of the lessons on R and R Commander, make an R Markdown report, and export as a pdf file.

Part 03. Explore use of R

Part 04. Create functions in R

Part 05. R packages: Making R do more

Part 06. Work with an included dataset

Part 07. Working with your own data

Part 08. Data exploration

6. Page quiz


draft, more questions soon

Reports

Five questions from this page