Today, we’ll learn how to make reports in R. Rstudio gives us the ability to analyse our data, and write a report in the same file. The file can be updated and published online and distributed in different formats. You have been receving the past three labs manuals in this format. Now it’s time for you to learn how to create one of these reports.
The file format we use in R to create reports is called rmarkdown. To create a blank markdown file, go to File menu, select New File, and select R markdwon…. See the path below:
This new file opens up in the top left box. The file is not empty. It comes with some code and text that you can delete. Everything pas teh --- line is just there to provide examples.
The first time you try to save the file, it asks you to give it a name. So go ahead and call it Lab4-YourName. Once you’ve saved the file, you can click on the yarn button to knit the file. Knit “will run each chunk of R code in the document and append the results of the code to the document next to the code chunk.” What does this mean?
If you recall from previous labs, every report contains text, code, and the outputs of those codes. Knit goes through your markdown and puts everything in the right place. If it’s text, it formats it and places it in the text. If it’s code, the code is placed in a box, and if its code output, the output is included in the file too. Here’s is how you spereate code from text:
'''{r} and '''. The code between these signs will be run and the output will be shown next to it.So for example, look at the line of code below:
nc.data<-read.csv("./nc.csv")
In the markdown file, it is written like this:
Note that text and code in the new file are just there to giv you examples. For example, the multiple # behind a line of tezt can make a text size change. or, * surrounding a line of text make it italic, or ** around a text make it bold. You can find other formating options here: https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf
Tips
echo = FALSE ({r, echo = FALSE}).eval = FALSE ({r, eval = FALSE})R markdown gives you many options and choices to format your report (see more options here: https://rmarkdown.rstudio.com/index.html). Also, you can format your output into word documents, pdf, and many others.
Summary of what we have learned so far
Now we want to use our knowledge of R to create our first report. In the file in this section, you can see a file called Lab Exercise. Open that folder and get started.