R Markdown

R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code. Markdown is a simple formatting syntax for authoring HTML, PRESENTATIONS, PDF, and MS Word documents.

It has three types of content which include text, code chunks and  YAML metadata which build Rmarkdown. 

YAML metadata consists of information about the title, author, date, and output. It starts and ends with three dashes (---)

Code chunks can be run without interference from the text. Multiple code chunks of different programming languages like Python and SQL can also be included in the same document. A single code can also be run and output can be viewed for the corresponding code in the R console. A code should start with ```{r} and ends with ```, so that code is isolated from text and other codes. 

Text can be made as a header using space along with hashtag  #(Header 1), ##(Header 2) and  ###(Header 3). It can be italicized with *x* or bold with **x**

The result of the code run can also be seen in the form of output along with an explanation and code in the same document after clicking on the 'knit' option. 

Uses:

  • To explain decision-makers who prefer to concentrate on the results rather than the analysis's underlying logic.
  • Communication with data scientists who prefer both code and conclusions 
  • Helpful to share our thoughts with others

R markdown file

It can be generated by clicking R markdown in the new file option. A dialog box appears with Title, Author, Date and Default output Format to select HTML, PDF or Word.


An untitled file with YAML metadata, text and code chunks appear. When you click the **Knit** button a document will be generated that includes both contents as well as the output of any embedded R code chunks within the document. 

---
title: "Rmarkdown_example"
author: "Veda"
date: "2023-04-01"
output: html_document
---
# **R Markdown**
It has three types of content which include text, code chunks and  YAML metadata which build Rmarkdown. Code chunks can be run without interference from text. 
Multiple code chunks and of different programming languages like Python and SQL can also be included in the same document. The result of the code run can also be seen in the form of output along with an explanation in the same document. 
The R code and output from the following code chunks will normally appear in the produced content because the code chunk in this instance sets the default "echo" option to "TRUE." This code chunk may not be included in the output due to the 'include=FALSE' parameter.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## *Dataset* 'pressure'
The following code loads data of dataset 'pressure', summarizes the dataset and plots the relationship between temperature in degrees celsius and mercury pressure in mm of Hg.
```{r}
library(datasets)
data(pressure)
summary(pressure)
plot(pressure, xlab = "Temperature (deg C)",
     ylab = "Pressure (mm of Hg)",
     main = "pressure data: Vapor Pressure of Mercury")
plot(pressure, xlab = "Temperature (deg C)",  log = "y",
     ylab = "Pressure (mm of Hg)",
     main = "pressure data: Vapor Pressure of Mercury")
```
## *Dataset* 'quakes'
The following code loads data and creates a scatter plot of data of earthquakes in Fiji. 
```{r}
library(datasets)
data(quakes)
pairs(quakes, main = "Fiji Earthquakes, N = 1000",col=rainbow(12), cex.main = 1.5, pch = ".")
```
## Matrices
The matrices 'A' and 'B' are created and 'transpose' function is implemented to them. Two vectors 'a' and 'b' are created and multiplied with matrices.
```{r}
A = matrix(1:10, nrow=10)
B = matrix(1:10, nrow=10)
A
#Transpose A and B
t(A)
t(B)
#create two vectors (a and b)
a = c(1:5)
b = c(1:10)
#multiply matrices by vectors
X = a*A
Y = b*B
X
Y
```
## Python
The following code chunk is in Python language and it prints Hello, World from 1 to 10.
```{python}
for i in range(1,11):
print("Hello, World%d" % i)
```







After saving the file, select knit to HTML, which will result in an HTML document containing all the information of YAML metadata, text, code chunks and output which will make it easier to view the output of different kinds of datasets in one file. It can also be knit to PDF or Word which will is effortless to share information containing output along with code.

URL to git repo: https://github.com/VedaVangala/vedas-r-repo/tree/main/R12

References
Wickham, H. (2015) R Packages Chapters 4-6 pp. 33-69 and pp. 81-117.
Wickham, H. & Grolemund, G. (2023). R for Data Science. Chapter 27 R Markdown.

Comments

Popular posts from this blog

Input/Output, String manipulation and 'plyr' package in R

PACKAGE "ACCURACY"

Visualization of Graphics in R