plot(cars) #cars is a built-in dataset available in R.
Introduction to Quarto
1 Introduction
This is a Quarto file. When you execute code within a Quarto file, the results appear beneath the code. Within R Studio, try executing the following chunk by clicking the Run button within the chunk (the green triangle on the right-hand side) or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
You can add a new chunk by clicking the Insert Chunk button on the toolbar (the green box with the c) or by pressing Ctrl+Alt+I.
To produce a complete report containing all commentary, code and output, click the Render button in the toolbar across the top of R Studio (look for the blue arrow). This will create a HTML file, which should open automatically and also appear in the folder where you have saved this Quarto file.
1.1 How to Format Text
The visual editor in RStudio provides a WYSIWYM interface for adjusting the content of your report. This includes formatting your text similar to how you would in MS Word, i.e. you can underline text, put words in bold font or italics, add bullet points, insert images, etc… While the visual editor displays your content with your desired formatting, if you choose the source editor, then you can see your content in plain Markdown language. You can switch back and forth between the visual and source editors to view and edit your content using either tool.
If you are editing text in the source editor, then the following summarises how to apply various formatting:
Putting the hashtag symbol “#” followed by a space creates a new section. Use “##” for a second level heading and “###” for a third level heading.
Putting a pair of asterisks around a word causes the word to be printed in italics.
Putting a pair of double asterisks around a word causes the word to be printed in bold.
You can use single dashes to create bullet points, for example:
- This is the first bullet point.
- This is the second bullet point.
If you want to use numbered bullet points, then you use the following:
- This is the first numbered bullet point.
- This is the second numbered bullet point.
If you wish to include a hyperlink with a name as opposed to the actual website address, then write what you want to call the hyperlink inside square brackets and then include the link in round brackets directly after, for example RTE News.
You can also include .png image files in your Quarto file. This is similar to how you include a hyperlink, except you put an exclamation point at the start. The caption for the image goes in the square brackets and then the name of the .png file goes in the round brackets. Note that the .png file must be saved in the same location as where you have saved this Quarto file.
2 Combining Code, Output and Commentary
Quarto is very popular with data analysts because you can combine your R code, output and commentary into one document. In this example, we will import the airbnb.csv dataset, create some graphs and add our commentary.
2.1 Import Data
First we will use the code in the following chunk to load the tidyverse package and then import our dataset. Note that the airbnb.csv file must be saved in the same location as where you have saved this Quarto file.
library(tidyverse)
<- read_csv("airbnb.csv") air
Within R Studio, look at the Quarto file and notice that we used message = F in the chunk option area. Knit the file with and without message = F to see what it does. With Quarto, it is good practice to prevent messages and warnings from appearing in the output HTML document. This is usually achieved by adding message = F and warning = F to the R chunk option area. See Section 29.5.2 Chunk Options in the R for Data Science book for other chunk options.
2.2 Histogram
Now we will create a histogram of the daily price variable using the following chunk. We can see that the daily price of a property is skewed with most properties costing between €50-€200.
ggplot(data = air) +
geom_histogram(mapping = aes(x = daily_price))
2.3 Barchart
The following R chunk produces a barchart showing the number of different room types for each property type. We can see that the most common airbnb rental is an entire apartment, whereas the least common is a shared room condominium.
ggplot(data = air) +
geom_bar(mapping = aes(x = property_type, fill = room_type), position = "dodge")
2.4 Table
The following chunk creates a table showing the mean daily price for each property type. We can see that Houses have the cheapest mean daily price, whereas “Other” properties are the most expensive.
<- air %>%
table1 group_by(property_type) %>%
summarise(mean_daily_price = mean(daily_price)) %>%
arrange(mean_daily_price)
table1
# A tibble: 4 × 2
property_type mean_daily_price
<chr> <dbl>
1 House 120.
2 Apartment 170.
3 Condominium 174.
4 Other 176.
Notice that table1 appears in the HTML file in the same format as when it appears in the console window. However, we can make the table look a lot nicer by using a function called kable() from the knitr package. We will learn more about formatting tables later.
#Here we use "::" to tell R that we want to use the kable() function from the knitr package.
#This is sometimes needed if there are two functions with the same name.
::kable(table1) knitr
property_type | mean_daily_price |
---|---|
House | 119.9022 |
Apartment | 169.8888 |
Condominium | 174.4395 |
Other | 176.0656 |
3 Publishing Your Report
There are many ways to publish documents created using Quarto, which are discussed at this link. The following gives instructions on how to publish your report using two options: RPubs and Quarto Pub.
3.1 RPubs
- Create a free account with RPubs and make sure you are logged into your RPubs account.
- Open the Quarto file that you want to publish within RStudio.
- Across the top of R Studio, look for a blue icon beside the green “Add Chunk” and “Run” buttons.
- Click the drop-down and select “Publish Document”.
- A box will appear asking where you want to publish your report. Choose “RPubs”. Then click “Publish”.
- Your default internet browser will open a page, asking you to confirm various details such as the title and description of your report. After entering these details, click “Continue”.
- Your report should now be available to view within RPubs.
3.2 Quarto Pub
- Create a free account with Quarto Pub.
- Open the Quarto file that you want to publish within RStudio.
- In the bottom left of RStudio, click the Terminal tab and copy and paste in the following code: quarto publish quarto-pub
- You will be asked to authorise your account and to choose the name you want to use for the published file.
- RStudio will then render your file and make it available within your Quarto Pub account. You will then be able to share a hyperlink to your file with relevant stakeholders.
4 Further Reading
To learn more about Quarto, please see the following resources:
- Quarto Chapter in the R for Data Science book by Wickham and Grolemund.
- Quarto formats Chapter in the R for Data Science book by Wickham and Grolemund.
- Quarto Website.