Exercise 1: Introduction to Quarto

Author

HeaDS Data Science Lab, University of Copenhagen

Getting started

  1. Make a new Quarto document. Go to the menu bar and click FileNew file…Quarto document…. Give the Quarto document a title that makes sense to you. NB! The document is not saved yet.

  2. Save your document in your Scripts directory. Go to the menu bar and click FileSave as….

Working with Quarto Documents

  1. Create code chunk. In the Quarto document you just made, create a code chuck by pressing the green box with the white ‘C’ in the right of the panel.

  2. Write code. Write a command in the code chunk, similar to one of those from exercise-0.

  3. Run code. Click on the green button in the top right of the code chunk - or simply type Ctrl + Enter/option + Enter. Then the command is transferred to the prompt in the console and executed, just as if you had written the command directly at the prompt. Try with a new command in a new line.

  4. Create a comment. Put a hashtag (#) before one of the commands in the code chunk and run it. Nothing happens! Hence, you can use hashtags for writing comments in your code chunks.

  5. Save your files often. It happens that you ask R for something so weird that it shuts down, and then it is a pity to have lost your work.

    Basic R commands

    We will now have a look at basic commands. We will use a built-in dataset of R called trees. Copy-paste the commands into your document or write them yourself to get a hang of the coding! Start thinking about the structure of you document and when to create new code chunks.

  6. To start with, load the tidyverse package.

    library(tidyverse)
  7. Now, load the trees dataset by copying and executing the following commands:

    data("trees")
    view(trees)
    ?trees
  8. Check what information is contained in the trees dataset by calling the summary function on it:

summary(trees)
  1. How many trees do we have data for?

  2. Now, extract the column called Volume from the dataset and assign it to a new variable called volume_col.

  3. Display the the first 10 values of volume_col.

  4. Find the minimum, maximum and mean volume from the column you just extracted. Does it match what was stated in the summary?

    Report here the median height of the trees.

  5. What class of data structure is trees? Make it into a tibble.

Making some graphics

Next, let’s also try to make some plots using the base graphics system in R (in Presentation III you will learn to make graphics using the ggplot2-package). We will continue to use the trees dataset.

  1. The dataset trees contains 31 observations of 3 variables (diameter, height and volume of black cherry trees). Insert the following two commands in a code chunk, and execute them to test out base R plots.
hist(trees$Volume, main = "Distribution of Volume", xlab = "Volume")
plot(Volume ~ Height, data = trees,
     main = "Raw scale",
     xlab = "Height", 
     ylab = "Volume")

Shut down R

  1. Close RStudio. If changes were made either to the Quarto document (shown in the upper-left window) or to the workspace (called ‘Global Environment’ in the upper-right window) you will be prompted if you like to save those. Answer Yes to that; in particular it is important that you save your documents as they contain all relevant commands to reproduce your output and plots.

  2. Reopen file. Locate the Quarto document that you saved. Start RStudio again, and open the file (via the File menu). Check that you can run it again.

Remark - You can also start RStudio by double clicking on a file with the .qmd extension (or other R-readable formats). One advantage of this is that the workspace and the R history will be saved to the same folder as the Quarto document when you later close RStudio, since the working directory will be set to the map where the file is located.

Wrapping up

  1. Imagine you need to send your code to a collaborator. Review your code to ensure it is clear and well-structured, so your collaborator can easily understand and follow your work. You might consider emphasizing important points by making text bold, underlined or blue.

  2. Render your Quarto document by clicking the Render button with the blue arrow in the toolbar. Once the HTML file is generated, open it in a web browser. Does the document look as you expected? Iterate on your document until it meets your desired format and appearance.

Extra

  1. Mac If you want to render your document to a PDF you need to install tinyex.

Go to https://quarto.org/docs/download/ and install Quarto.

In the top of your console window, switch to Terminal. (If the Terminal is not there go to ToolsTerminalNew Terminal.) Write quarto install tinytex in the terminal and press enter. When it has completed and gives the message Installation successful, change the YAML in the every top of your Quarto document from format: html to format: pdf. Save and render your Quarto document again and review the PDF file.

Lessons learnt

  • You must write your code in a document, like the Quarto document, such that you can save the work and return to it some other day.

  • Save your files often.

  • If you forget to save your files before you close RStudio, then RStudio will prompt you if you want to save your work.

  • Have structure in your document by using headers, text, and code chunks (maybe with comments).