R Markdown — Most amazing way to present your code
R is the first step of learning and applying statistical models when it comes to academic settings. Recently, I was reading about an excellent way of presenting R codes and I thought of trying my hands on R Markdown. I regret for having spent time writing assignment answers in Word and pasting code snippets and the output to submit my assignment in one file. I am writing this blog for all the students, so that they can do a better and neater job with less hassle and more wow !
The best part is that you dont need a whole new application to work on a ‘.rmd’ file. Once you open the RStudio, Go to New File-> New RMarkdown
You will then be prompted to save your work in ‘html’ or ‘pdf’ format. ‘html’ format is a good way to start. You can also choose any of the other methods.
Now, you get a script like body with the following top line :
title: "R_Skills_Test_Solutions_Pranidhi"
output: html_document
To write notes/explanation in the code, one can just type the code and it will appear as regular text. While using headings, the use of ‘#’ with a space is advised. The more the number of hashtags the smaller the heading level.
Below is a sample of a code with notes :
To add a piece of code, you need to start writing the code between``` {r} and ```. The code will have a background in grey. Each line can be run independently or you can also choose to run the chunk using the play button on the chunk on right handside (highlighted) :
Once we run the chunk, the output appears right below and in case of multiple outputs, they are placed next to each other and it can be clicked to see them in detail. Below is the output with a piece of code :
So, once you are done writing the code, putting the comments and running each chunk successfully, it is time to see the final output in the form of an ‘html’ .
Click on the option ‘knit’ and your file will be generated in an html format.
The above comments, code and the output table in Figure 4 appears as shown below :
That is how simple is using R Markdown file. You can send the ‘html’ file, the ‘.rmd’ file and the data files together as a submission. No more copy pasting R codes.