Markdown Files in R


What is an R markdown file? R Markdown is a file format for making dynamic documents with R. R markdown is a syntax for formatting plain text. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code. You can convert your .Rmd file into an HTML, PDF, or Microsoft Word file. You can even turn the file into an HTML5 or PDF slideshow. rmarkdown will preserve the text, code results, and formatting contained in your original .Rmd file.

Using the output of markdown files is a good way to share your work with others. They can reproduce it on their own because your R code is included, as well as the results of running that code.

To create an R Markdown report, open a plain text file and save it with the extension .Rmd. R Markdown also includes an interactive option called an R Notebook that lets users run your code and show the graphs and charts that visualize the code. Any R Markdown document can be used as a notebook. R Markdown lets you convert your markdown files into lots of different formats including HTML, PDF, and Word documents, or you can convert to a slide presentation or dashboard.

At the heart of an Rmd file is the R code. Code added to an RMD File is usually called a code chunk.

For more information, in RStudio, click on Help in the menu at the top. Choose Markdown Quick Reference. There is a lot in that reference. Just like HTML, you can use bold, italic, headers, lists that are ordered or unordered, links, images, blockquotes, LaTeX equations, subscripts, superscripts, strikethrough and R code.

Header

You can use two hashtags for a header. The more hashtags you use, the smaller the text.

Bullets

Use asterisks to make bullets.

Hyperlinks

You can use angled brackets < > to make a hyperlink. Insert the URL between the brackets like this . Another way is to say click here[link](https://begincodingnow.com)

Images

You can embed an image into your markdown file. The syntax for adding an image with a caption that says example 1 is ![example 1](r-project.org/logo/Rlogo.png)

Learning with YouTube

Here’s a 20-minute video called Rmarkdown – Introduction and Basics. Below that is the modified “source code”. You can take this code and copy it and modify it. I changed it a little bit from the video. This simple text would be in a file with an .Rmd extension. In RStudio you would open the file. Click on Knit.

---
output: html_document
---
# Analysis of the cars data set in R
### by Jalayer Academy (YouTube video) and Begin Coding Now
**Part 1**
```{r}
data(cars)
str(cars)
summary(cars)
plot(cars)
```
**Part 2**
```{r}
hist(cars$speed)
boxplot(cars$dist)
```
The mean speed of cars was **`r mean(cars$speed)`**.
This is an example of LaTeX. You can write formulas in this format $x^2$
# The End

Code Chunks

In the above code you can see some code chunks, delimited by ` ` `{r} and ` ` `. Those are three backticks, that are found to the left of the 1 key on the keyboard. The echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the results. echo = FALSE is often used to embed plots. The include = FALSE code prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks.

```{r type what you want then a comma, echo=FALSE}

Creating a Markdown File

In RStudio, click File, New File, R Markdown.

Cheat Sheets from Posit

R Markdown is a good candidate for a cheat sheet, among others. Get your own cheat sheet! In RStudio, click on the Help menu at the top. Click on Cheat Sheets, then R Markdown Cheat Sheet.

The R for Data Science online book has information on R Markdowm.

R markdown the Definitive Guide. You can check out Chapter 3 Documents for the output options like HTL, PDF and Word.

The correct syntax for adding an image with the caption example 1 to an RMarkdown file is ![example 1](r-project.org/logo/Rlogo.png). The syntax is an exclamation mark followed by the caption in brackets and the image URL or pathway in parentheses.

Here’s a second way to add a link. [click here!](www.rstudio.com). The first way was to use angle brackets around the URL.

When you Knit a file in RStudio what part of code chunks are shown by default?

Leave a Reply