How do you write beautiful reports using R Markdown?

0
How do you write beautiful reports using R Markdown?

You’ve spent hours cleaning, visualizing and building complex machine learning models with your data but it does not mean a lot if you cannot communicate these results effectively with your managers, CEOs or your clients. This guide will teach you how you can create beautiful yet meaningful reports using R Markdown.

In order to open R Markdown you need to first open RStudio and then click the drop down menu as shown in the image below:

Once you click the R Markdown option you are going to get a pop-up box that let’s you name the report and choose the final format that you want it published in.

If you want to publish your report in any of the formats listed above we need to install “TeX” on your device. We have provided the appropriate links to download “TeX” based on your operating system below:

Once you have downloaded “TeX” we can proceed to actually writing the report.

Before starting any report we need to supply the R text Editor in markdown with some fundamental information with respect to our report. We can do this using the code shown below:

Note that every line of code mentioned from this point has to be written in the R Script box that is generated after clicking the R Markdown option in the drop down that was shown above.

One of the best aspects about R Markdown is that you can use it to generate reports with the language that you used to build your project even though you’re on RStudio.

For example we can write code using R in markdown using the code shown below:

Now if we built our project using python, we can implement that in Markdown using the syntax shown below:

Next we might want to customize the fonts in our report. We can do this using the code displayed below:

The resulting output is displayed below:

If we wanted to insert a link into our report we would use the code shown below:

The resulting output is shown below: 

You insert the name that you want your link to appear in the [] while the actual link is inserted into ().

If we want to create effective headers we can use the code shown below:

The output of the code should look like this: 

We can make ordered lists using the code shown below:

The output of the code shown above looks like this:

You can write equations in R markdown using the code shown below:

The output for which is shown below:

Sometimes chunks of code can generate warning and error messages. When we write our report we might want markdown to ignore these. We can do so using the code shown below:

Sometimes lines of code generate messages that we might want to ignore. We can do this using the lines of code shown below:

If we only want to display the results of our code and not the code itself we can do so using the code shown below:

If you only want to display the code but not the results you can do that by using the code shown below:

Setting ‘eval’ to FALSE will ensure that markdown does not run the code.

If we wanted to run the code but hide the results of the code we can do so using the code shown below:

The format that we want to publish our report can take many forms and we can modify it to our liking. In order to publish your report you would need to do the following:

Click on the Knit option on top and click on the appropriate format that you want to display your output in. In this example we have converted the output to a word document after clicking on the “Knit to Word” option in the drop down menu shown above. The resulting document is a word document that looks like this: 

There are many other formats that you can publish your final report in. You can modify the format in the the code shown below:

These lines of code are usually found at the start of your R Markdown file. At the present moment the “output” section is a “html_document”. The various formats that you can publish in are:

  • pdf_document – PDF
  • word_document – Microsoft Word
  • md_document – R Markdown Document
  • beamer_presentation – Beamer presentation
  • ioslides_presentation – ioslides HTML slideshow
  • slidy_presentation – slidy HTML slideshow

You can also make you documents follow a certain theme using the “theme” option as implemented in the code shown below:

With the tools that you have learnt above you are now ready to make beautiful reports using R Markdown. Reporting and documentation are very important to any data professional as it is the only way you can have a record of your work and communicate it effetely with your fellow co-workers and the management.

Reports are also a useful way to document your portfolio so that you can share this with potential employers who you might be interested in working for.

With this in mind, Happy Reporting!

LEAVE A REPLY