Introduction to R Markdown
In this article, we will explore the concepts of Markdown in R Programming Language.
We will cover the following topics:
- Introduction to R Markdown
- Advantages Of Markdown
- What is R Markdown?
- Why use R Markdown?
- Getting started with R Markdown
Introduction to R Markdown
Markdown is a lightweight markup language and created by John Gruber in 2004. Nowadays markdown is one of the world’s most popular markup languages. The file extension of a Markdown file is .md or .markdown. To compile a markdown file user needs an application capable of processing markdown files like Microsoft Word, Dillinger, etc. These applications utilize a Markdown parser which converts a markdown file to printable HTML code.
Advantages Of Markdown
- It is mostly used to write formatted pages across several platforms like StackOverflow, Github, and many more.
- It’s not made just for programmers, because one can write E-Books with it using leanpub.
- Convertible to formats like PDF, HTML, docs etc.
- Markdown Files can be converted easily to web pages using tools like Github Pages, smallvictori.es, and blot.im.
What is R Markdown?
R Markdown is a way of generating fully reproducible documents, in which both text and code can be combined. That’s how things can be made as bullets, bold, italics, links, or run inline R codes. Despite these documents all starting as plain text, one can render them into HTML pages, or PDFs, or Word documents, or slides! The symbols that use to signal, for example, bold or italics is compatible with all of those formats.
Why use R Markdown?
- One of the main advantages is the reproducibility of using R Markdown. Since it can simply combine text and code pieces in one document, and can easily combine introductions, hypotheses, the code that is running, the results of that code, and the conclusions all in one document. Sharing what you did, why you did it, and how it turned out becomes so easy and that person you share it with can re-run your code and get the correct same answers you got. That’s what reproducibility means. But also, seldom you will be working on a project that takes many weeks to complete and you want to be able to see what you did a long time ago and you can see accurately what you ran and the results of that code and R Markdown documents permits you to do that.
- Another major advantage to R Markdown is that since it is plain text, it works very well with version control systems. It is simple to track what character changes happen between commits; unlike other formats that aren’t plain text.
- Another selfish advantage of R Markdown is it is very easy to use. Like everything in R, this extended functionality comes from an R package “rmarkdown.” All one needs to do is to install it by giving this command install.packages(“rmarkdown”).
Getting started with R Markdown
To create an R Markdown document, in R Studio, go to File > New File > R Markdown. The following window will appear:
As one can see, the above image is filled in a title and an author and switched the output format to a PDF. Explore around this window and the tabs along the left to see all the different formats that it can output to. When this is completed, click OK, and a new window should open with a little explanation on R Markdown files.
There are three important sections of an R Markdown document.
- Header: The first one is the header at the top, bounded by the three dashes. This is where the user specifies the details like the title, name, the date, and what kind of document you want to output. If the user has filled in the blanks in the window earlier, these should be already filled out.
- Text: Also on this page, one can see text sections, for example, one section starts with “## R Markdown” this section will render as text when the PDF of this file is produced and all of the formattings that the user will learn generally applies to this section.
- Code Chunk: And finally, the user will see code chunks. These are bounded by the triple backticks. These are pieces of R code chunks that can run right from within the document and the output of this code will be included in the PDF when created. The easiest way to see how each of these sections behave is to produce the PDF.
Knitting the document
When the user is done with a document, in R Markdown, then one has to “knit” his plain text and code into his final document. To do so, click on the Knit button which is present on the top of the source panel. When one does so, it will prompt him to save the document as an RMD file. Do so. A document like this can be seen:
So here one can see that the content of the header was rendered into a title, followed by his/her name and the date. The text chunks produced a section header called “R Markdown” which is followed by two paragraphs of text. Following this, one can see the R code summary(cars), importantly, followed by the output of running that code. This is one of the huge benefits of R Markdown that is rendering the results to code inline.
Please Login to comment...