There are a number of different file types you can create in R Studio
which will all run your analyses. These include basic .r
files, R Markdown files and R Notebooks (both end .Rmd
),
and now Quarto files (ending in .qmd
). While many people
may prefer to run base .r
files for various reasons, there
are many benefits to using the other file types. The main reason is
because all of the .Rmd
and .qmd
files allow
you to combine Markdown and code in a single file.
Markdown (i.e., the md
part of the .Rmd
and
.qmd
file extensions) is a simple text markup format.
Basically, markdown lets you include some text but also style the text,
such as bolding, italicising, creating
headers, and so on. Markdown converts seamlessly to other formats,
allowing documents to be created in html (for websites), but also as PDF
or even Word formats. For example, you can easily convert your R
Markdown to custom styled
Word documents.
Markdown is thus not specific to R or R notebooks, it is instead a more universal method for rendering text that is employed in various different applications. That being said, R Markdown does have its own set of formatting guidelines.
R Markdown is a solid choice if you are only concerned with executing R code. There is very little difference between and R Markdown and an R Notebook (in fact they are basically the same thing). The Quarto format is newer and is designed to be language agnostic, meaning it can work with Python, Julia, Javascript, and other programming langauges. Quarto also plays nice with R Studio’s visual editor. However, there is no need or requirement to use Quarto if all you want to do is run some R.
To keep things simple, all of the R files in this workshop will be
basic R Markdown files (.Rmd
). If you like to use Quarto
instead, you should be able to easily convert between the two.
This file will look different depending on how you view it. If you
use the .Rmd
version and open in R Studio, you will see the
code editor. If you use the .html
version and open it with
a web browser (or are viewing this on the web!), you should see all the
rendered code and markdown.
This text that you are reading is markdown text. Below is a code
cell. Code cells are separated from markdown using three backticks above
and below the code cell. The first three backticks (```)
allow you to define the language within the cell, and also give the cell
a name (both optional).
In the code cell below, I use the print()
function to
print the string 'hello world'
. Note that the code output
appears below the code cell, within the notebook. This allows us to keep
code and notes in one place - useful for your own analyses as well as
sharing with collaborators (or your supervisors!)
## [1] "hello world"
You can use the hashtag/pound sign headings to organise your notebook. One hashtag is a level 1 heading, two is a level 2, and so on. Click the “Outline” button near the top right of this window to see the headers, allowing you to quickly jump among different markdown and code cells sections.
Using headings (as well as code cell names) is a great way to keep an R Markdown document organised.