1+3
[1] 4
Want to code along?
Quarto is an open-source publishing suite - a tool-suite that supports workflows for reproducible scholarly writing and publishing.
Quarto documents often begin with a YAML header
demarcated by three dashes (---
) which specifies things about the document. This includes what type of documents to render (compile) to e.g. HTML, PDF, WORD and whether it should be published to a website project. You can also add information on project title, author, default editor, etc.
Quarto works with markup language
. A markup language is a text-coding system which specifies the structure and formatting of a document and the relationships among its parts. Markup languages control how the content of a document is displayed.
Pandoc Markdown
is the markup language utilized by Quarto - another classic example of a markup language is LaTex
.
Lets see how the pandoc Pandoc Markdown
works:
Headers are marked with hashtags. More hashtags equals smaller title.
This is normal text. Yes, it is larger than the smallest header. A Quarto document works similarly to a Word document where you can select tools from the toolbar to write in bold or italic and insert thing like a table:
My friends | Their favorite drink | Their favorite food |
---|---|---|
Micheal | Beer | Burger |
Jane | Wine | Lasagne |
Robert | Water | Salad |
β¦ a picture:
We can also make a list of things we like:
Coffee
Cake
Water
Fruit
There are two modes of Quarto: Source and Visual. In the left part of the panel you can change between the two modes.
Some features can only be added when you are in Source mode. E.g write blue text is coded like this in the source code [write blue text]{style="color:blue"}
.
Code chunks are where the code is added to the document.
Click the green button +c
and a grey code chunk will appear with '{r}'
in the beginning. This means that it is an R code chunk. It is also possible to insert code chunks of other coding language.
For executing the code, press the Run
button in the top right of the chunk to evaluate the code.
some executable code in an R code chunk.
Below is a code chunk with a comment. A comment is a line that starts with a hashtag. Comments can be useful in longer code chunks and will often describe the code.
You can add comments above or to the right of the code. This will not influence the executing of the code.
Control whether code is executed.
eval=FALSE not execute the code and eval=TRUE will execute the code.
The code is shown, but the result is not shown ({r, echo=TRUE, eval=FALSE}
):
Show or hide code. echo=FALSE
will hide the code and echo=TRUE
will show the code. Default is TRUE.
The code is not shown, but the result is shown ({r, echo=FALSE, eval=TRUE}
):
[1] 4
Control messages, warnings and errors. Maybe you have a code chunk that you know will produce one of the three and you often donβt want to see it in the compiled document. N.B! It is not a good idea to hide these statements (especially the errors) before you know what they are.
Warning is not printed ({r, message=FALSE, warning=FALSE, error=TRUE}
):
Warning is printed ({r message=TRUE, warning=TRUE, error=TRUE}
):
In the panel there is a blue arrow and the word Render. Open the rendered html file in your browser and admire your work.
Letβs get to coding!
The term path
refers to the trajectory you need to follow from the place you are βlocatedβ on your computer to the place you want to work from. When working with Quarto your working directory (wd)
is always the same locations as your Quarto document (is not true for .R scripts!). The wd becomes important when we start loading data in from other places (presentation 2).
Where am I now? getwd()
Set working directory, setwd()
The working directory can be changed BUT when working with Quarto this only influences individual code chunks. In contrast, a changing of wd within an .R script
affects the document globally - we will not cover .R scripts
in this course.
There are two type of paths, absolute paths (from root to desired location) and relative paths (from current location to desired location):
Absolute path:
Relative path:
Navigate up in the directory tree (..)
Pointing to a dataset
In R we use an arrow for variable assignment. You may call your almost variables whatever you like. DO NOT: use special characters in variable names, i.e. &, ), $ etc. or make spaces in naming.
The first two variables we create is βaβ and βbβ
Now we print βaβ and see what value it has:
We add a and b without reassignment and get the result printed:
If we want to save the result we have to reassign it to a new variable:
A vector of numbers named num1
Find the mean of the vector
Function are chunks of code wrapped in a way which makes the code inside reusable. A function takes an input(s) (arguments) and returns an output(s). You can make your own function but in this course you will only use the functions that are already available in the R packages.
Letβs look at the mean() function
Taking the mean of a vector
Functions makes code reusable
[1] 4.9
Find length of vector
Summary statistics is information such as number of items, mean, median, standard deviation and sum.
Summary statistics of a vector
R packages are collections of functions written by R developers and super users and they make our lives much easier. Functions used in the same type of R analysis/pipeline are bundled and organized in packages. There is a help page for each package to tell us which functions it contains and which arguments go into these. In order to use a package we need to download and install it on our computer. Most R packages are stored and maintained on the CRAN[https://cran.r-project.org/mirrors.html%5D repository.
Install a package
Load packages
Query package
Query function from package
In the example below we will make two vectors into a tibble. Tibbles are the R object types you will mainly be working with in this course. We will try to convert between data types and structures using the collection of βas.β functions.
A vector of characters
people <- c("Anders", "Diana", "Tugce", "Henrike", "Chelsea", "Valentina", "Thile", "Helene")
people
[1] "Anders" "Diana" "Tugce" "Henrike" "Chelsea" "Valentina"
[7] "Thile" "Helene"
A vector of numeric values
[1] 2019 2020 2020 2021 2023 2022 2020 2024
Access data type or structure with the class() function
Convert joined_year to character values
[1] "2019" "2020" "2020" "2021" "2023" "2022" "2020" "2024"
[1] "character"
Convert joined_year back to numeric values
Convert classes with the βas.β functions
Letβs make a tibble from two vectors
# A tibble: 8 Γ 2
name joined_year
<chr> <dbl>
1 Anders 2019
2 Diana 2020
3 Tugce 2020
4 Henrike 2021
5 Chelsea 2023
6 Valentina 2022
7 Thile 2020
8 Helene 2024
[1] "tbl_df" "tbl" "data.frame"
Just like you can convert between different data types, you can convert between data structures/objects.
Convert tibble to dataframe
Convert classes with the βas.β functions
You can inspect an R objects in different ways:
1. Simply call it and it will be printed to the console. 2. With large object it is preferable to use `head()` or `tail()` to only see the first or last part. 3. To see the data in a tabular excel style format you can use `view()`
Remove something:
Look at the βheadβ of an object:
# A tibble: 4 Γ 2
name joined_year
<chr> <dbl>
1 Anders 2019
2 Diana 2020
3 Tugce 2020
4 Henrike 2021
Open up tibble as a table (Excel style):
dim(), short for dimensions, which returns the number of rows and columns of an R object:
Look at a single column from a tibble using the β$β symbol: