Hi everyone,
Several of you have e-mailed me to require access to the promised R Markdown example, so I thought I would share the example with the whole group. The instructions illustrate the production of a Word report from R Studio using R Markdown. The report can include text, graphs, tables - the usual output produced by a statistical analysis.
In a nutshell, the report creation involves creating an R Markdown file and converting it to a Word report by pressing the Knit Word button available in the R Studio.
The R Markdown file is a specialized R script which instructs R to include text, graphs and tables in a Word report by using very specific instructions written in the R Markdown language. The instructions are fairly simple but have to follow the rules of the R Markdown language.
Steps 1 - 7 below provide the phases you need to go through to create an empty R Markdown file. Step 8 requires you to populate the R Markdown file with your own instructions for producing a Word report. Once these instructions are entered into the R Markdown file and saved, the Word report can be produced by simply pressing the Knit Word button located right above the R Studio script window.
Step 1: Install the following R packages in R Studio:
install.packages("rmarkdown")
install.packages("knitr")
Step 2: Require the installed R packages for the current R Studio session:
require(rmarkdown)
require(knitr)
Step 3: Set the working directory for the current R Studio session:
Session ---> Set Working Directory ---> Choose Directory...
Step 4: Open a new R Markdown file from the R Studio menu:
File ---> New File ---> R Markdown...
Step 5: In the New R Markdown window:
- Select the type of the R Markdown file to be Document (in the left panel);
- Specify the title of the document as Report (in the right panel);
- Write down your name in the Author box (in the right panel);
- Specify the Default Output Format of the document as Word (in the right panel); [Other possibilities of Output Format include HTML or PDF.]
- Press the OK button to save all of the above changes.
Step 6: Select the default contents of the R Markdown file you have just created with Ctrl + A and delete everything you selected so that you start out with a fresh, empty file.
Step 7: Save the empty R Markdown file by pressing the keys Ctrl + S, entering the file name Report.Rmd and then pressing the Save button. [Note the .Rmd extension assigned to the R Markdown file.]
Step 8: Populate the R Markdown file with appropriate instructions. [See below for a specific example of instructions.] Keep saving the file at regular time intervals by pressing the keys Ctrl + S.
Step 9: Press the button Knit Word on the R Studio menu to produce a Word report from the R Markdown file. If all goes well, the Word report will pop-up on your screen.
~.~
So the only missing piece of information concerns the R Markdown instructions to be inserted in the R Markdown file in Step 8 above. Here is an example of R Markdown instructions to get you started - simply copy and paste these instructions in your R Markdown file as part of Step 8, save the file and then proceed with Step 9. [Whenever you launch a new R Markdown file, it is populated with some default commands, so you will need to delete all of those commands and start typing - or copying and pasting - the commands below instead. Note the special conventions used for styling the report text, as well as the use of ```{r} before you enter a block of R commands for producing tables or graphs, followed by the use of ``` at the end of that block. When working with R packages, you will need to require all packages needed to run the R code embedded in your R Markdown file. Additionally, you will need to read all of your data sets as part of the file.]
---
title: "Report"
author: "Isabella Ghement"
date: "May 10, 2016"
output: word_document
---
This is some text.
*This text is styled in italics.*
** This text is styled in bold.**
# First level header
## Second level header
### Third level header
#### Fourth level header
##### Fifth level header
###### Sixth level header
A horizontal rule can be implemented using three hyphens.
---
This is a reference line.
This is a new paragraph.
This is the current paragraph.\
This is a separate line in the current paragraph.
This is a bulleted list:
* Item a
* Item b
This is an ordered list:
1. Item 1
2. Item 2
3. Item 3
This is a bulleted list with a sub-list for one of the items:
* Item a
+ Sub-item 1
+ Sub-item 2
* Item b
This is an ordered list with a sub-list for one of the items:
1. Item 1
+ Sub-item 1
+ Sub-item 2
2. Item 2
3. Item 3
```{r, echo=FALSE, message=FALSE, fig.width=6, fig.height=5}
require(car)
require(ggplot2)
ggplot(data=Duncan, aes(x=income, y=prestige, group=type)) +
geom_point(aes(colour=type))
```
```{r, message=FALSE, kable}
require(knitr)
require(car)
model kable(summary(model)$coef, digits=2)
```
Let me know if you were able to produce the Word report after following the instructions in this e-mail.
You can also produce your Word report from the command line using the command:
rmarkdown::render("Report_Name.Rmd")
where Report_Name.Rmd is the name of your R Markdown file. In this case, the Word report will be dumped in your working directory for R Studio, so you can open it with:
browseURL("Report_Name.docx")
Note that, if you have white spaces in your R Markdown file name, R Studio will replace them with hyphens when creating the Word Report. For example, Report Name.Rmd will become Report_Name.docx.
For more details on producing reports with R Markdown, you can consult the following links:
http://rmarkdown.rstudio.com/
http://blog.rstudio.org/2014/08/01/the-r-markdown-cheat-sheet/
Best regards,
Isabella
Isabella R. Ghement, Ph.D.
Ghement Statistical Consulting Company Ltd.
301-7031 Blundell Road, Richmond, B.C., Canada, V6Y 1J5
Tel: 604-767-1250
Fax: 604-270-3922
E-mail: isabella@ghement.ca
Web: www.ghement.ca
------------------------------
Isabella Ghement
Ghement Statistical Consulting Company Ltd.
E-mail:
isabella@ghement.ca
Original Message:
Sent: 05-10-2016 12:09
From: Richard Leu
Subject: HTML output
From a data science perspective:
I think html, and web technology in general, is becoming a must. Not expert level but you should have enough knowledge to communicate with developers to create small applications and dashboards for clients. Shiny is a way to do this quickly without needing to know html but it may be hard to build a more professional application without some knowledge. However, if you do know html/css/js, you can greatly enhance your shiny applications.
The clients I worked with were demanding analytics and visual tools. While we still produced lengthy reports (in Word or latex), the applications were the things receiving the *wow* factor and getting us more work. I would say my experience is limited. I was doing mostly reliability engineering / survival analysis in the aerospace/defense and energy sector. I would also say that reproducing reports was not a priority in these cases; more important was doing the analysis repeatedly after new data streamed in.
------------------------------
Richard Leu