DSCI 325: Handout 28 – Introduction to R Markdown Spring 2017
Total Page:16
File Type:pdf, Size:1020Kb
DSCI 325: Handout 28 – Introduction to R Markdown Spring 2017 R Markdown provides a mechanism to create dynamic documents with embedded chunks of R code. This allows you to keep your R code, your outputs (e.g., summaries and/or graphs), and your discussion/comments all in the same document. Subsequently, you can produce nicely formatted output in html, pdf, or Microsoft Word. The following website gives more information about R Markdown: http://rmarkdown.rstudio.com/. CREATING A NEW R MARKDOWN DOCUMENT In R Studio, select File > New File > R Markdown. Tell R Studio that you want to create a new html Document, and fill in the Title and Author fields. When you click OK, a sample script containing R Markdown code appears in R studio. Select File > Save As… to save this script in your own desired directory. Next, select “Knit HTML” to create the html file. 1 Note that a webpage appears. This webpage includes the title and author information, the date, R code, R output, and comments/discussion. If you close this webpage, you can access it once again from the directory in which you saved your R Markdown (.Rmd) file. WRITING A NEW R MARKDOWN DOCUMENT Let’s start with the sample R script. Leave the metadata section that includes the title, author, date, and output information (you can modify these if you want); delete all other comments/code from the sample script. Next, write a line of text in the file, similar to the following. Adding R Code Chunks Suppose we wanted to create a plot with the dotplot() function from the lattice library. Try adding the following commands to your R Markdown script. 2 When you click on “Knit HTML” to create the webpage, the following is returned: Note that R treated the commands as text. To separate code from comments/text, you must use a code separator. The easiest way to accomplish this in R Studio is to “Insert a new code chunk” as shown below. This adds the following to your script. The three backticks followed by {r} separates R code from text. Once you have entered your R commands inside the code chunk, your script should appear as follows: 3 Now, when you click on “Knit to HTML” to create the webpage, the following is returned: Now, you can see that the html file contains all of the following: The title/author/date/information Text/comments from the author R commands (which are nicely highlighted) R output Next, suppose that one line of the R script was changed, as is shown below. 4 Now, when you click on “Knit HTML” you will see that messages normally sent to the R console are also included in the document produced by R Markdown. Suppressing Messages If you want to suppress messages from being sent to the final document, you can use the message=FALSE option in the code chunk that you want to suppress. ```{r message=FALSE} require(lattice) NutritionData = read.csv("T:/20165001682/ReadOnly/Data/NutritionData.csv") attach(NutritionData) dotplot(Location ~ SatFat,xlab="Saturated Fat") ``` Suppressing R Code to Display Only Output If you want to suppress the R code in your final report, use the echo=FALSE option. ```{r message=FALSE, echo=FALSE} require(lattice) NutritionData = read.csv("T:/20165001682/ReadOnly/Data/NutritionData.csv") attach(NutritionData) dotplot(Location ~ SatFat,xlab="Saturated Fat") ``` The html document now appears as follows. 5 Changing the Plot Size If you aren’t satisfied with the size of the plot relative to the text, you can use the fig.width and fig.height options. For example, consider the following. ```{r message=FALSE, echo=FALSE, fig.height=3.5, fig.width=4.5} require(lattice) NutritionData = read.csv("T:/20165001682/ReadOnly/Data/NutritionData.csv") attach(NutritionData) dotplot(Location ~ SatFat,xlab="Saturated Fat") ``` 6 Writing Inline Code Instead of inserting a code chunk, you can also surround code with back ticks (start the code with the letter r). R will replace the inline code with its results when the final document is created. The overall mean sodium level of the menu items from McDonald's was `r mean(NutritionData[Location=="McDonalds","Sodium"])`. R returns the following in the html document: The Basics of Text Formatting in R Markdown Note how the following R Markdown syntax was used to format text in the document. Headers (Could also be used to create sections and subsections) Markdown Syntax Formatted Text # Header 1 ## Header 2 ### Header 3 Block Quotes Markdown Syntax Formatted Text This is a test to see block quotes. > Block quote is shown here. Bulleted Lists Markdown Syntax Formatted Text * Item 1 * Item 1a * Item 1b * Item 2 * Item 2a * Item 2b 7 Italics Markdown Syntax Formatted Text Use the *asterisk* to italicize text. Boldface Markdown Syntax Formatted Text Use two asterisks to get **boldface**. Typewriter Form Markdown Syntax Formatted Text Use the `backtick` for typewriter face text. Subcripts/Superscripts Markdown Syntax Formatted Text $Subcript_1$ $Superscript^1$ New paragraphs (end a line with two spaces to start a new paragraph). Markdown Syntax Formatted Text Test 1 Test 2 Test 1 Test 2 Hyperlinks Markdown Syntax Formatted Text [link](https://www.rstudio.com/) 8 Inline Equations Markdown Syntax Formatted Text The area of a circle is $A = \pi*r^{2}$ Consider the following script to see how some of the formatting works. --- title: "Fast Food Nutrition Data" author: "Tisha Hooks" date: "April 25, 2016" output: html_document --- ```{r warning=FALSE, message=FALSE, echo=FALSE} library(lattice) library(dplyr) NutritionData = read.csv("T:/20175002183/ReadOnly/Data/NutritionData.csv") attach(NutritionData) ``` This report summarizes nutrition data from several fast food restaurants. The data can be found on our course storage space in the file *NutritionData.csv*. The following response variables will be considered. * Calories * Saturated Fat * Cholesterol * Sodium For each response variable, summary statistics will be computed **by restaurant** using the `by()` and `summary()` functions. A dotplot will also be created so that comparisons can be made across restuarants. ## Summary of Calories by Restaurant ```{r fig.height = 3, fig.width=6} by(Calories, Location, summary) dotplot(Location ~ Calories, xlab="Calories") ``` ## Summary of Saturated Fat by Restaurant ```{r, echo=FALSE, fig.height = 3, fig.width=6} by(SatFat, Location, summary) dotplot(Location ~ SatFat, xlab="Saturated Fat") ``` ## Summary of Cholesterol by Restaurant ```{r, echo=FALSE, fig.height = 3, fig.width=6} by(Cholesterol, Location, summary) dotplot(Location ~ Cholesterol, xlab="Cholesterol") ``` 9 ## Summary of Sodium by Restaurant ```{r, echo=FALSE} by(Sodium,Location,summary) dotplot(Location ~ Sodium,xlab="Sodium") Rendering Your Report The easiest way to render the report is to click the “Knit to HTML” button at the top of the R Studio scripts pane. When you render the report, R executes the code and builds a new version of your report in the output file type you requested (you can change this to “Knit to PDF,” for example). R will also open a preview of the output file in the viewer pane and save the output file in your working directory. 10 .