Skip to content

Commit

Permalink
first update to presentation 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ThildeBT committed Oct 29, 2024
1 parent cc4c996 commit aa499b7
Show file tree
Hide file tree
Showing 5 changed files with 1,388 additions and 854 deletions.
1,860 changes: 1,179 additions & 681 deletions Teachers/Presentations/HTMLs/presentation1.html

Large diffs are not rendered by default.

91 changes: 46 additions & 45 deletions Teachers/Presentations/presentation1.qmd
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
---
title: "Presentation 1: Introduction"
title: "Presentation 1: R, Rstudio and Quarto"
format: html
project:
type: website
output-dir: ../docs
---

## Introduction
# The Quarto Way!

I. The Fundamentals of R (R codes for presentation)
[Quarto](https://quarto.org/) is an open-source publishing suite - a tool-suite that supports workflows for reproducible scholarly writing and publishing.

Center for Health Data Science, University of Copenhagen
Quarto documents begin with a `YAML header` which specifies things about the document, what type of documents to render (compile) to e.g. HTML, PDF, WORD, whether the document is published to a website project, document layout, and more.

These files are generated by the Quarto file presentation1.qmd
# Quarto uses a markup language

## The Quarto Way!
Quarto works with `markup language`. A [markup language](https://en.wikipedia.org/wiki/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`.

This section will go through how the Quarto format works and how to code in here.
Lets see how the pandoc `Pandoc Markdown` works:\

## This is the second largest header (Header 2)
### This is the third largest header (Header 3)

###### This is the smallest header (Header 6)

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 like a word document where we can write with **bold** or *italic* and insert thing like a table:

| My friends | Their favorite drink | Their favorite food |
Expand All @@ -44,13 +47,15 @@ We can also make a list of things we like:

- Fruit

## Modes of Quarto
## Modes of Quarto Document

There are two modes of Quarto: **Source and Visual**. The one that looks like Word is Visual. In the left part of the panel you can change to Source. If you have heard about Rmarkdown, the Source mode is essentially the same thing. Here you can see that headers are marked with hashtags. The more hashtags, the smaller the title. Some features can only be added when you are in Source mode. E.g [write blue text]{style="color:blue"} is coded like this in the source code `[write blue text]{style="color:blue"}`. We will mostly spend time in the Visual mode since it is the nicest to look at. Feel free to explore the Source mode at the end of the exercises!
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]{style="color:blue"} is coded like this in the source code `[write blue text]{style="color:blue"}`.\

## Code chunks and structure

Code chunks are where the coding come in. Go to the very right of the panel and press the green box with the C. A grey code chunk will appear with '{r}' in the beginning. This mean that it is an R code chunk. It is also possible to insert code chunks of other coding language, but we will stick to R since that is what we are here to learn. For executing the code, press the green button in the top right of the code chunk.
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 mean 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.

If you have ever worked with an R script, a code chunk is just like a part of an R script.

Expand Down Expand Up @@ -83,11 +88,9 @@ You can add comments above or to the right of the code. This will not influence
1+3 # or place a comment here
```

## Alter output of code chunks

You don't need this but it might be a good feature for your later R carrer.
## Output of code chunks

**Control whether code is executed.** eval=FALSE not execute the code and eval=TRUE will execute the code. Default is TRUE.
**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}`):

Expand All @@ -104,7 +107,7 @@ The code is not shown, but the result is shown (`{r, echo=FALSE, eval=TRUE}`):
1+3
```

**Control messages, warnings and errors.** Maybe you have a code chunk that you know will produce one of the three and you don't want to look at it. It is not a good idea to hide these statements (especially the errors) before you know what they are. But in a report, it might be nice to be able to mute the messages or the warnings.
**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]{.underline} printed (`{r, message=FALSE, warning=FALSE, error=TRUE}`):

Expand All @@ -120,8 +123,6 @@ log(-1)

## Shortcuts

Skip if you are overwhelmed!

If you want to look effortless while coding and impress you colleagues, have a look at the shortcuts. There are shortcuts for almost anything and you can find them (*Tools*\> *Keyboard Shortcuts Help*) or change them to your own liking (*Tools*\> *Modify Keyboard Shortcuts...*).

### Render: Making the report
Expand All @@ -130,79 +131,79 @@ In the panel there is a blue arrow and the word *Render*. Open the rendered html

[**Let's get to coding!**]{style="color:blue"}

------------------------------------------------------------------------

# Lets get started on R!

## Working directory

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 is always the same locations as your Quarto document. The working becomes important when we start loading data in from other places (presentation 2).
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?
**Where am I now? getwd()**

```{r}
getwd()
```

**Set working directory**
**Set working directory, setwd()**

The working directory can changed but when working with Quarto this only influences individual code chunks (which is mentioned in a warning when executing the code below). However it is really useful when working with raw R scripts which are taught in the upcoming R for data science (R part 2) course.
The working directory can changed BUT when working with Quarto this only influences individual code chunks. In opposition, a changing of wd within a classic .R script affects the document globally - we will not cover .R script 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):

```{r}
# setwd('../Exercises/')
getwd()
**Absolute path:**

```{r, eval=FALSE}
setwd("/Users/kgx936/Desktop/HeaDS/GitHub_repos/FromExceltoR")
```

**Relative path:**

```{r, eval=FALSE}
setwd('./Exercises/')
```

In another chuck, the working directory is the same as before.
**Navigate up in the directory tree (..)**

```{r}
getwd()
setwd('../Teachers')
```

## Variable assignment

In R we use an arrow for variable assignment. You may call your almost variables whatever you like. [DO NOT]{.underline}: use special characters in variable names, i.e. &, ), \$ etc. or make spaces in naming.

**The first variable we create is 'a'**
**The first two variables we create is 'a' and 'b'**

```{r}
a <- 1
b <- 3
```

**Now we print 'a' and see what value it has**
**Now we print 'a' and see what value it has:**

```{r}
print(a)
```

**We check the class of a**
**We check the class of a:**

```{r}
class(a)
```

**Let's create and other variable, b**

```{r}
b <- 3
```

**We add a and b [without]{.underline} reassignment** and get the result printed
**We add a and b [without]{.underline} reassignment and get the result printed:**

```{r}
a + b
```

If we want to save the result we have to reassign it to a new variable
**If we want to save the result we have to reassign it to a new variable:**

```{r}
c <- a + b
print(c)
```

... or overwrite an already existing variable. Here, a is "updated" to the sum of a and b. NB! Be careful with this.

```{r}
a <- a + b
print(a)
```

**A vector of numbers named num1**

```{r}
Expand Down
Loading

0 comments on commit aa499b7

Please sign in to comment.