# You can write code in chunks that look like this.
# This chunk uses the code plot(cars) to plot a data set.
# To run the code, click the Green play button at the
# top right of this chunk. Try it!
plot(cars)
Quarto
This is a Quarto file. It contains plain text interspersed with grey chunks of code. You can use the file to take notes and run code. For example, you can write your name on the line below. Try it:
Good job! The results of a code chunk will appear beneath the chunk. You can click the x above the results to make them go away, but let’s not do that.
You can open a new Quarto file by going to File > New File > Quarto Document…. Then click OK. But let’s not open a new file now—keep reading this one!
Adding chunks
To add a new code chunk, press Cmd+Option+I (Ctrl+Alt+I on Windows), or click the Insert button at the top of this document, then select R. Quarto will add a new, empty chunk at your cursor’s location.
Try making a code chunk below:
Good job! For today, you should place all of your R code inside of code chunks.
# Sometimes you might want to run only some of the code
# in a code chunk. To do that, highlight the code to
# run and then press Cmd + Enter (Control + Enter on
# Windows). If you do not highlight any code, R will
# run the line of code that your cursor is on.
# Try it now. Run mean(1:5) but not the line below.
mean(1:5)
[1] 3
warning("You shouldn't run this!")
Warning: You shouldn't run this!
# You can click the downward facing arrow to the left of the play button to run
# every chunk above the current code chunk. This is useful if the code in your
# chunk uses object that you made in previous chunks.
# Sys.Date()
Did you notice the green lines (if your RStudio theme is the default one) in the code chunk above? They are code comments, lines of text that R ignores when it runs the code. R will treat everything that appears after #
on a line as a code comment. As a result, if you run the chunk above, nothing will happen—it is all code comments (and that’s fine)!
Remove the #
on the last line of the chunk above and then rerun the chunk. Can you tell what Sys.Date()
does?
By the way, you only need to use code comments inside of code chunks. R knows not to try to run the text that you write outside of code chunks. You can press Cmd+Shift+C to comment or uncomment a line of code.
Text formatting
Have you noticed the funny highlighting that appears in this document? Quarto treats text surrounded by asterisks, double asterisks, and backticks
in special ways. It is Quarto’s way of saying that these words are in
- italics
- also italics
- bold, and
code font
*
, **
, and ` are signals used by a text editing format known as markdown
. Quarto uses markdown
to turn your plain looking .Rmd documents into polished reports. Let’s give that a try.
Reports
When you click the Render
button at the top of an Quarto file (like this one), Quarto generates a polished copy of your report. Quarto:
- Transforms all of your markdown cues into actual formatted text (e.g. bold text, italic text, etc.)
- Reruns all of your code chunks in a clean R session and appends the results to the finished report.
- Saves the finished report alongside your .Rmd file
Click the Render button at the top of this document or press Cmd+Shift+K (Ctrl+Shift+K on Windows) to render the finished report. The RStudio IDE will open the report so you can see its contents. For now, our reports will be HTML files. Try clicking Render now.
Good job! You’ll learn more about Quarto throughout the day!
R Packages
Here is one last code chunk that we will use in the next exercise. If you uncomment the code and try to run it, it won’t work. If you don’t believe me try!
# ggplot(data = diamonds) + geom_point(aes(x = carat, y = price))