This post has been written byย Gunnvant Singh- Jigsaw Academy Faculty
The usual data science project involves following stages:
Data preparation and exploration
Data analysis and model building
Report making and presentation
Usually, the report making part of the whole exercise is conducted at the end of data analysis and is always divorced from the model building and data analysis process. The reports are created painstakingly using Microsoft Power Point and Word. A lot of copy pasting of statistical results takes place from either a SAS or an R console. If God forbid, any changes are required in the report one goes back to running the R or SAS code again, copy pasting the results back into the presentation and word document!!! Can’t there be an easy way to deal with this painfully mundane exercise? Turns out there is a way, one can use ‘knitr’ package available in R.
knitr provides two broad report generation frameworks: One can either prepare reports using LaTeX or using Markdown. In this post I will illustrate how reports along with embedded R code can be generated using both Markdown and LaTeX
Generating a LaTeX report:
In order to generate dynamic reports one needs to install the package ‘knitr’ from cran and also needs to install LaTeX engine. Miktex is the most popular LaTeX distribution and can be downloaded from https://miktex.org/download.
Once this is done one needs to setup the RStudio to use knitr and Miktex as the default report generating tools. Go to ‘Tools’ then ‘Global Options’ and then in the pane select ‘Sweave’. In the dialogue box that appears, make sure that for the option โWeave Rnw files usingโ โknitrโ is chosen and for the option โTypeset LaTeX into pdf usingโ โpdfLaTeXโ is chosen
Once this is done, one can begin the process of creating a LaTeX report. The good thing about using LaTeX is that the output is always in a .pdf format and if your report contains a lot of mathematical formulae, then LaTeX has a very good support for that too.
In order to create a LaTeX report, go to โFileโ then โNew Fileโ and select โR Sweaveโ. A new console window will appear.
Let’s make a sample report, type in the following code:
After typing this code click on โCompile PDFโ option, and you will see your first report. If you look carefully the code looks a bit different from a vanilla R code. Infact the only recognizable R code is in the grey box. This grey box is called a โchunkโ. The R code will always go in the chunk portion the text will go inside the normal LaTeX code. One can have a lots of control as far as the formatting of a report are concerned. If the option โechoโ is given as โfalseโ in the chunk, then one will not see any R code in the pdf generated, if the โevalโ option is given as โtrueโ in the chunk option, then one can view the actual output of the R code. The upside of doing all the analysis in this way is that one can change the code and expect to see the change in report itself, no more separate running of code and then copy pasting of results. Reports can also be edited very fast.
Generating a Markdown Report:
To generate a Markdown report go to โFileโ, โNew Fileโ and select โR Markdownโ. A new console window appears. Write down the following code:
Click on the โKnit HTMLโ option and you will see the HTML output. The R code in this case is written within a chunk which starts with three backticks “` and also ends with three back ticks “`. The options for controlling the R output is same as was discussed above, one can use โechoโ and โevalโ options to control if code and output are to be shown in the report or not.
The upside of using Markdown reports is that unlike LaTeX reports you don’t need to know anything about what tags and headers. The downside is you can’t really control the formatting of your text in the same way as you can with LaTeX report.
knitr is not limited to R only, infact it can be used with Python as well as SAS. So the next time you start an analysis don’t forget to use knitr to ease the workflow.
Related Posts:
How to use PROC R in SAS Using Confidence Intervals Confidently!!! The Power of R โ And Why itโs an Essential Skill for Data Analysts
Fill in the details to know more
Important Artificial Intelligence Tools
October 31, 2022
Top 28 Data Analytics Tools For Data Analysts | UNext
September 27, 2022
Stringi Package in R
May 5, 2022
Best Frameworks In Java You Should Know In 2021
May 5, 2021
Lean Management Tools: An Ultimate Overview For 2021
May 4, 2021
Talend ETL: An Interesting Guide In 4 Points
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile