How to use Rmarkdown

Hey all,

I just completed the first guided Project in the data analyst in R course. I wrote a fully functioning R script that answers all the questions. The issue I’m having now is using rmarkdown to create an html file using that script. I really don’t understand what to do now. Do I just open an rmarkdown file and copy/paste my R script into it? That seems very cumbersome and inefficient. Do I load the rmarkdown package at the end of my R script? Was I just supposed to do the project in an rmarkdown file? Any advice would really help.

Thank you
COVID_19_Project.R (4.1 KB)

1 Like

Assuming you are using RStudio

TLDR: Yes, just copy and paste your R script into R markdown and export to your desired format.

RMarkdown is not loaded as other packages in R are loaded (library(name)).
You just go to File -> New File -> RMarkdown and create a new file in Rmrkdown format.

I am assuming you want to write a program with an interface that is easy to understand and easy to share. If you were doing this in python, you would probably save it as a jupyter notebook because it is super easy to add text to your analysis and upload to github etc.

R markdown basically does something similar by implementing LaTex along with you code. You could work with this two ways:

  1. Write your code in R and copy into an R markdown
  2. Write everything in an R markdown file

I would argue 2 is better because you still have all of the code. It is just saved to a file .Rmd (instead of .r). From this .Rmd file you can still run blocks of code as you do in a classic .r file, but you can also output to pdf, html, or word. I would say if you were sending a small amount of code to someone (another dev), giving them a .r is just fine. But, if you were doing an analysis and preparing a report, exporting your .Rmd file is the move.

Note: In your Rmd file, you must surround your code as such:

I am going to execute the following code:
```{r}
Code in here
```

Hope this helps!

1 Like

R Markdown is the best format for Guided Projects because the projects are a mixture of code and writing.

Check out these Dataquest blog posts for everything you need to know about getting started with R Markdown:

Best,
-Casey

2 Likes

Okay thanks for the advice. I just have 2 more quick questions.

  1. Is there a keyboard shortcut to surround the already written code in a chunk. Control + shift+ I only creates a chunk below the code so it’s not very effective for using it on an already written R script

  2. Is there a way to convert and R script into an Rmarkdown file without just copy and pasting it over? I’ve seen suggestions of knitr and spin

I have always wanted to do this as well! But I do not know of a shortcut for this. I’ve looked a few times without luck.

You can render an r script to html with rmarkdown::render(), but that is the closest I’ve found. Here’s some info on that: https://rmarkdown.rstudio.com/articles_report_from_r_script.html

Best,
-Casey

1 Like