From 7181c54844f7d15b29c3160ced8d1ccbba95fff9 Mon Sep 17 00:00:00 2001 From: Martin van Rongen Date: Tue, 18 Jun 2024 15:20:09 +0100 Subject: [PATCH] 1805 ex update --- .../execute-results/html.json | 4 ++-- .../execute-results/html.json | 4 ++-- materials/_no-render/template.qmd | 2 +- materials/functions-and-objects.qmd | 10 +++++----- materials/intro-to-programming.qmd | 19 +++++++++++-------- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/_freeze/materials/functions-and-objects/execute-results/html.json b/_freeze/materials/functions-and-objects/execute-results/html.json index 5c5425b..d59fa2c 100644 --- a/_freeze/materials/functions-and-objects/execute-results/html.json +++ b/_freeze/materials/functions-and-objects/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "03d394e5f92ebacc2aadec78a36153fc", + "hash": "b0fb62fec325107d6c16a4071207504f", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Functions and objects\"\n---\n\n::: {.cell}\n\n:::\n\n::: {.cell}\n\n:::\n\n\n::: {.callout-tip}\n## Learning outcomes\n\n- Be able to use functions\n- Be able to assign values to objects\n\n:::\n\n## Purpose and aim\n\nIn this section we'll focus on functions and objects. We'll learn how to use functions and how to create and access objects.\n\n## Functions\n\nFunctions perform specific operations. A function usually gets one or more inputs called arguments and returns a value. You can see it as a predefined script.\n\nAn example is:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsqrt()\n```\n:::\n\n\nThis function returns the square root of a number. As such, it can only have a number as input, for example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsqrt(9)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 3\n```\n\n\n:::\n:::\n\n\n:::\n\nFunctions can take different **arguments**. In the example above there was only one, but many functions allow you to specify more than one input. For example, let's say we wanted to round a number.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nWe can use the `round()` function:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nround(10.232)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 10\n```\n\n\n:::\n:::\n\n\nThis returns a whole number. But what if we wanted to round it to one decimal? The `round()` function has an argument called `digits` that allows you to do just that. We separate the input and the argument with a comma.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nround(10.232, digits = 1)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 10.2\n```\n\n\n:::\n:::\n\n\n:::\n\n## Objects\n\nOften, you want to save the output of an operation for later use. In those cases we need to store that information somewhere. These are called **objects**. What happens is that we **assign** the output of the operation to an object.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nTo create an object, we need to give it a name followed by the assignment operator `<-`, and the value we want to give to it.\n\nFor example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage <- 21\n```\n:::\n\n\nWe can read the code as: the value 21 is assigned to the object `age`. When you run this line of code the object you just created appears in your `Environment` tab (top-right panel).\n\nWhen assigning a value to an object, R does not print anything on the console. You can print the value by typing the object name in the console or by running it from within your script.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 21\n```\n\n\n:::\n:::\n\n\n:::\n\n### Using objects\n\nThe nice thing about storing values in objects is that you can use them for further operations. Look at the following example.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nLet's say we wanted to calculate double the `age`:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage * 2\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 42\n```\n\n\n:::\n:::\n\n\nWe can also perform operations between variables:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nphd_length <- 4\n\nage + phd_length\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 25\n```\n\n\n:::\n:::\n\n\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_objects].\n:::\n\n### Object types\n\nIn the example above we only used numbers - these are very useful, since we can do calculations with them.\n\nNumbers are just one type of data you may encounter. Although there are quite a few different types, the main ones include:\n\n1. numbers (e.g. `62`, `55`, `-27`)\n2. text (e.g. `\"bunny\"`, `\"greenhouse\"`, `\"binder\"`)\n3. logical (`TRUE` or `FALSE`)\n4. missing values (`NA`)\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nYou might have noticed that the text values are in quotes (`\" \"`). R requires all text to be within quotation marks. That's the way it recognises it as text (also sometimes referred to as a *string*).\n\nThe logical values are binary: they're either true or false. In R these true/false designations are represented by `TRUE` and `FALSE`. Note that they are case-sensitive.\n\nMissing values are specifically encoded as such in R. You'll find that this is a really useful feature, because it makes missing values explicit. They are encoded with `NA`.\n\n:::{.callout-note}\n## Special meanining\n\nYou will notice that, in RStudio, the `TRUE`, `FALSE` and `NA` values are coloured light blue. This is because they have special meaning to R.\n\nThis also means that we shouldn't use these in different context. For example, it's a bad idea to create an object named `TRUE`, since it would really confuse R.\n\nThere are other names that have special meaning, but don't worry too much about it for now. Generally, if you accidentally choose a name for an object that has special meaning, it'll quickly becomes clear because your code might stop working.\n\n:::\n:::\n\n### Vectors\n\nVectors are the building block of most programming languages. They are containers for a sequence of data elements. That may sound a bit cryptic, so let's illustrate this with some examples.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nIn the examples above we stored a single value in an object. But quite often we work with more than just one data point. The way that we group these together into a vector is by using the `c()` function.\n\nThe `c()` or concatenate / combine function does what it says on the tin: it combines multiple values together. Have a look at the following set of examples:\n\n**Numbers:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_num <- c(12, 22, 98, 61)\n\nvec_num\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 12 22 98 61\n```\n\n\n:::\n:::\n\n\n**Text:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_text <- c(\"felsic\", \"intermediate\", \"mafic\", \"ultramafic\")\n\nvec_text\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"felsic\" \"intermediate\" \"mafic\" \"ultramafic\" \n```\n\n\n:::\n:::\n\n\nIn case you are wondering, these are [different types of lava](https://en.wikipedia.org/wiki/Lava#:~:text=Because%20of%20the%20role%20of,intermediate%2C%20mafic%2C%20and%20ultramafic.).\n\n**Mixed types:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_mixed <- c(\"tree\", \"leaf\", 31, NA, 22)\n\nvec_mixed\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"tree\" \"leaf\" \"31\" NA \"22\" \n```\n\n\n:::\n:::\n\n:::\n\nYou can also combine vectors together, for example:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nc(vec_num, vec_mixed)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"12\" \"22\" \"98\" \"61\" \"tree\" \"leaf\" \"31\" NA \"22\" \n```\n\n\n:::\n:::\n\n\n:::\n\n:::{.callout-note}\n## Preferential treatment\nOften, not all data types are equal. We won't go into too much detail here, but it's important to keep in mind that:\n\n* presence of text in a vector often leads to *all* the elements being converted to text\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_vectors].\n:::\n\n## Exercises\n\n### Dealing with objects {#sec-exr_objects}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\n* Create an object `day_temp` containing the current temperature (yes, you can guess!)\n* Create an object `weather` containing the values `raining`, `cloudy`, `sunny`\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nday_temp <- 21\n\nweather <- c(\"raining\", \"cloudy\", \"sunny\")\n```\n:::\n\n\n:::\n:::\n:::\n\n### Vectors {#sec-exr_vectors}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nCreate the following vectors:\n\n1. A vector `vec_1` containing 3 numbers\n2. A vector `vec_2` with two numbers and two words\n3. A vector `vec_3` with two numbers, a missing value, two words and a TRUE/FALSE outcome \n\nLook at the content of the vectors. Is there anything you notice?\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_1 <- c(31, 8, 92)\n\nvec_2 <- c(77, \"hedgehog\", \"cloud\", 33)\n\nvec_3 <- c(23, 66, NA, \"bob\", \"jeff\", FALSE)\n```\n:::\n\n\nYou might have noticed that in `vec_2` and `vec_3` every value is now within quotes. That's because as soon as there is any text in a vector, R automatically converts *all* elements in the vector to text.\n:::\n:::\n:::\n\n## Summary\n\n::: {.callout-tip}\n#### Key points\n\n- Functions perform a specific set of operations\n- Objects allow you to store value that can be accessed later\n:::\n", + "markdown": "---\ntitle: \"Functions and objects\"\n---\n\n::: {.cell}\n\n:::\n\n::: {.cell}\n\n:::\n\n\n::: {.callout-tip}\n## Learning outcomes\n\n- Be able to use functions\n- Be able to assign values to objects\n\n:::\n\n## Purpose and aim\n\nIn this section we'll focus on functions and objects. We'll learn how to use functions and how to create and access objects.\n\n## Functions\n\nFunctions perform specific operations. A function usually gets one or more inputs called arguments and returns a value. You can see it as a predefined script.\n\nAn example is:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsqrt()\n```\n:::\n\n\nThis function returns the square root of a number. As such, it can only have a number as input, for example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nsqrt(9)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 3\n```\n\n\n:::\n:::\n\n\n:::\n\nFunctions can take different **arguments**. In the example above there was only one, but many functions allow you to specify more than one input. For example, let's say we wanted to round a number.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nWe can use the `round()` function:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nround(10.232)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 10\n```\n\n\n:::\n:::\n\n\nThis returns a whole number. But what if we wanted to round it to one decimal? The `round()` function has an argument called `digits` that allows you to do just that. We separate the input and the argument with a comma.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nround(10.232, digits = 1)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 10.2\n```\n\n\n:::\n:::\n\n\n:::\n\n## Objects\n\nOften, you want to save the output of an operation for later use. In those cases we need to store that information somewhere. These are called **objects**. What happens is that we **assign** the output of the operation to an object.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nTo create an object, we need to give it a name followed by the assignment operator `<-`, and the value we want to give to it.\n\nFor example:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage <- 21\n```\n:::\n\n\nWe can read the code as: the value 21 is assigned to the object `age`. When you run this line of code the object you just created appears in your `Environment` tab (top-right panel).\n\nWhen assigning a value to an object, R does not print anything on the console. You can print the value by typing the object name in the console or by running it from within your script.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 21\n```\n\n\n:::\n:::\n\n\n:::\n\n### Using objects\n\nThe nice thing about storing values in objects is that you can use them for further operations. Look at the following example.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nLet's say we wanted to calculate double the `age`:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nage * 2\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 42\n```\n\n\n:::\n:::\n\n\nWe can also perform operations between variables:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nphd_length <- 4\n\nage + phd_length\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 25\n```\n\n\n:::\n:::\n\n\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_objects].\n:::\n\n### Object types\n\nIn the example above we only used numbers - these are very useful, since we can do calculations with them.\n\nNumbers are just one type of data you may encounter. Although there are quite a few different types, the main ones include:\n\n1. numbers (e.g. `62`, `55`, `-27`)\n2. text (e.g. `\"bunny\"`, `\"greenhouse\"`, `\"binder\"`)\n3. logical (`TRUE` or `FALSE`)\n4. missing values (`NA`)\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nYou might have noticed that the text values are in quotes (`\" \"`). R requires all text to be within quotation marks. That's the way it recognises it as text (also sometimes referred to as a *string*).\n\nThe logical values are binary: they're either true or false. In R these true/false designations are represented by `TRUE` and `FALSE`. Note that they are case-sensitive.\n\nMissing values are specifically encoded as such in R. You'll find that this is a really useful feature, because it makes missing values explicit. They are encoded with `NA`.\n\n:::{.callout-note}\n## Special meanining\n\nYou will notice that, in RStudio, the `TRUE`, `FALSE` and `NA` values are coloured light blue. This is because they have special meaning to R.\n\nThis also means that we shouldn't use these in different context. For example, it's a bad idea to create an object named `TRUE`, since it would really confuse R.\n\nThere are other names that have special meaning, but don't worry too much about it for now. Generally, if you accidentally choose a name for an object that has special meaning, it'll quickly becomes clear because your code might stop working.\n\n:::\n:::\n\n### Vectors\n\nVectors are the building block of most programming languages. They are containers for a sequence of data elements. That may sound a bit cryptic, so let's illustrate this with some examples.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nIn the examples above we stored a single value in an object. But quite often we work with more than just one data point. The way that we group these together into a vector is by using the `c()` function.\n\nThe `c()` or concatenate / combine function does what it says on the tin: it combines multiple values together. Have a look at the following set of examples:\n\n**Numbers:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_num <- c(12, 22, 98, 61)\n\nvec_num\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 12 22 98 61\n```\n\n\n:::\n:::\n\n\n**Text:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_text <- c(\"felsic\", \"intermediate\", \"mafic\", \"ultramafic\")\n\nvec_text\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"felsic\" \"intermediate\" \"mafic\" \"ultramafic\" \n```\n\n\n:::\n:::\n\n\nIn case you are wondering, these are [different types of lava](https://en.wikipedia.org/wiki/Lava#:~:text=Because%20of%20the%20role%20of,intermediate%2C%20mafic%2C%20and%20ultramafic.).\n\n**Mixed types:**\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_mixed <- c(\"tree\", \"leaf\", 31, NA, 22)\n\nvec_mixed\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"tree\" \"leaf\" \"31\" NA \"22\" \n```\n\n\n:::\n:::\n\n:::\n\nYou can also combine vectors together, for example:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nc(vec_num, vec_mixed)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"12\" \"22\" \"98\" \"61\" \"tree\" \"leaf\" \"31\" NA \"22\" \n```\n\n\n:::\n:::\n\n\n:::\n\n:::{.callout-warning}\n## Preferential treatment of data types\nOften, not all data types are equal. We won't go into too much detail here, but it's important to keep in mind that:\n\nthe presence of text in a vector leads to *all* the elements being converted to text!\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_vectors].\n:::\n\n## Exercises\n\n### Dealing with objects {#sec-exr_objects}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\n* Create an object `day_temp` containing the current temperature (yes, you can guess!)\n* Create an object `weather` containing the values `raining`, `cloudy`, `sunny`\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nday_temp <- 21\n\nweather <- c(\"raining\", \"cloudy\", \"sunny\")\n```\n:::\n\n\n:::\n:::\n:::\n\n### Vectors {#sec-exr_vectors}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nCreate the following vectors:\n\n1. A vector `vec_1` containing 3 numbers\n2. A vector `vec_2` with two numbers and two words\n3. A vector `vec_3` with two numbers, a missing value, two words and a TRUE/FALSE outcome \n\nLook at the content of the vectors. Is there anything you notice?\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvec_1 <- c(31, 8, 92)\n\nvec_2 <- c(77, \"hedgehog\", \"cloud\", 33)\n\nvec_3 <- c(23, 66, NA, \"bob\", \"jeff\", FALSE)\n```\n:::\n\n\nYou might have noticed that in `vec_2` and `vec_3` every value is now within quotes. That's because as soon as there is any text in a vector, R automatically converts *all* elements in the vector to text.\n:::\n:::\n:::\n\n## Summary\n\n::: {.callout-tip}\n#### Key points\n\n- Functions perform a specific set of operations\n- Objects allow you to store value that can be accessed later\n:::\n", "supporting": [ "functions-and-objects_files" ], diff --git a/_freeze/materials/intro-to-programming/execute-results/html.json b/_freeze/materials/intro-to-programming/execute-results/html.json index 5836225..159075f 100644 --- a/_freeze/materials/intro-to-programming/execute-results/html.json +++ b/_freeze/materials/intro-to-programming/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "762555d04085aec6c8ad54e2c6568d41", + "hash": "5b6f3e7c7a2173afe55d9a97eb210923", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Getting started\"\n---\n\n::: {.cell}\n\n:::\n\n::: {.cell}\n\n:::\n\n\n::: {.callout-tip}\n## Learning outcomes\n\n- Learn basic programming techniques\n\n:::\n\n## Purpose and aim\n\nUsing a programming language to analyse, visualise and communicate your data has many advantages over point-and-click programmes.\n\n* it documents analysis steps with code, aiding reproducibility\n* allows scaling to large data\n* generates high quality graphics that can be adjusted\n\n## Introduction\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nThe term \"**R**\" is used to refer to both the programming language and the\nsoftware that interprets the scripts written using it.\n\n**RStudio** is an additional software that makes it easier to \ninteract with R by providing tools that make programming easier. \nTo function correctly, RStudio needs R and therefore both need to be installed on your computer.\n\nSome advantages of using R for your data analysis include:\n\n- Analysis steps are documented with code, allowing for greater reproducibility. \n- There are thousands of packages (extensions) available, making R a very flexible \n and powerful tool, for a wide range of applications. \n- Analysis can be scaled to large data.\n- Can generate a wide range of high-quality graphics for data visualisation.\n- There is a large community of contributors.\n- It's free and open source.\n\n\n### The RStudio Interface \n\nRStudio is divided into four \"panes\", illustrated below. \nThe default layout is:\n\n- Top Left - **Source**: this is where you edit your R scripts \n (this panel might not appear until you create a script, which we demonstrate below).\n- Bottom Left - **Console**: where R will execute commands and print results.\n- Top Right - **Environment**: this will show you which objects you create \n while working with R.\n- Bottom Right - **Files**/**Plots**/**Packages**/**Help**: several tabs that allow \n you to navigate your files, view plots, view installed packages and search help files. \n\n\n![](images/00-RStudio_screen.png)\n\n:::\n\n## Getting set up\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nIt is good practice to keep a set of related data, analyses, and text\nself-contained in a single folder called the **working directory** (usually a folder \non your computer where you have all the files relating to a project you're working on). \n\nThe working directory is an important concept to understand. It is the place\nwhere R will look for and save files.\n\nAll of the scripts within this folder can then use *relative paths* to files. \nRelative paths indicate where inside the project a file is located (as opposed to \nabsolute paths, which point to where a file is on a specific computer). \nWorking this way makes it a lot easier to move your project around on your computer \nand share it with others without having to directly modify file paths in the individual \nscripts.\n\nRStudio provides a helpful set of tools to do this through its **Projects**\ninterface, which not only creates a working directory for you but also remembers\nits location (allowing you to quickly navigate to it). The interface also \npreserves custom settings and open files to make it easier to resume work after \na break. \n\n\n### Creating a new project\n\nUsually, you will already have a folder on your computer for your project, for \nexample with some data you collected or downloaded from the web. \n\nTo create an _R Project_ within the `r-workshop` directory:\n\n- From the upper menu on RStudio click: File > New project > Existing directory.\n- Click the browse... button and navigate and open your `r-workshop` folder. \n- Click on Create project. This will initiate a fresh R session.\n\nFrom now on, whenever you want to work on this project, open the the `Rproj` file \nthat was created in your `r-workshop` folder.\n\nThis will ensure your working directory is automatically set correctly. This also means \nthat you can move the project folder to a different location or even different \ncomputer. As long as you open the `Rproj` file, your working directory will be set correctly. \n\nIf you need to check your working directory, you can run `getwd()` on the console. \nIf for some reason your working directory is not what it should be, you can change it in the RStudio interface by navigating in the file browser (bottom-right panel) to where your working directory should be, clicking on the blue gear icon \nMore > Set As Working Directory.\n\nAlternatively, you can run `setwd(\"/path/to/working/directory\")` on the console to \nreset your working directory. However, your scripts should not include this line, \nbecause it will fail on someone else's computer.\n\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_project].\n:::\n\n## Writing code\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nNow that we have a project, let's run our first commands in R.\n\nOn the _console_ panel, type:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n1 + 100\n```\n:::\n\n\nAnd R will print out the answer, with a preceding `[1]`. Don't worry about\nthis for now, we'll explain that later. For now think of it as indicating\noutput.\n\nAny time you hit return and the console shows a \"`+`\" instead of a \"`>`\", it\nmeans it's waiting for you to complete the command. If you want to cancel a\ncommand you can hit Esc and RStudio will give you back the `>` prompt.\n\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_calculations].\n:::\n\n## Creating scripts\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nSo far, we've been typing these commands directly in the R console. However, if we \nclosed RStudio and later wanted to recreate these operations, there would be no \nrecord of them anywhere. \n\nIn practice, we should always write our code in a **script**, which is a plain text \ndocument with our commands written in it. \nTo create a new R script go to File > New File > R Script.\n\nThis will open a panel on the top-left. This is a text editor, which in RStudio \ndoes some syntax highlighting (it colours the code) to help read the code. \n\nAs you're adding code to the script, you can run it interactively on the console \nby pressing the shortcut Ctrl+Enter. \n\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_scripts].\n:::\n\n## Installing and loading packages\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nAdditional packages can be installed to extend the functionality of R. \nMost packages are available in a central repository called CRAN and can be \ninstalled from within R using the `install.packages()` function.\n\nFor example, to install (or update) the `tidyverse` package, you would run the \nfollowing command on the console:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"tidyverse\")\n```\n:::\n\n\nBecause the install process accesses the CRAN repository, you will need an Internet \nconnection to install packages.\n\nAfter this, you can then load the package to use it in your analysis. For the example above, we would do that as follows with the `library()` function:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\n```\n:::\n\n\n:::\n\n:::{.callout-important}\n## Exercise\nComplete [Exercise -@sec-exr_packages].\n:::\n\n:::{.callout-important}\nRemember, we only need to **install** a library/package *once*. However, we need to **load** it every time we start an analysis.\n:::\n\n\n\n## Exercises\n\n### Setting up a project {#sec-exr_project}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nSet up a project and make sure it's set as your working directory.\n\n:::\n\n### Calculations {#sec-exr_calculations}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nRun the following calculations:\n\n* `2 + 23`\n* `23 * 4`\n* `314 - 82`\n* `(12 - 4) * (6 + 2)`\n* `3 ^ 2`\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\n2 + 23\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 25\n```\n\n\n:::\n\n```{.r .cell-code}\n23 * 4\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 92\n```\n\n\n:::\n\n```{.r .cell-code}\n314 - 82\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 232\n```\n\n\n:::\n\n```{.r .cell-code}\n(12 - 4) * (6 + 2)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 64\n```\n\n\n:::\n\n```{.r .cell-code}\n3 ^ 2\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 9\n```\n\n\n:::\n:::\n\n\n:::\n:::\n:::\n\n### Creating scripts {#sec-exr_scripts}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nPlease do the following:\n\n1. Create a script called `session_01` in your working directory.\n2. Re-run the calculations from [Exercise -@sec-exr_calculations].\n3. Save the changes to the script.\n\n:::\n\n### Adding functionality {#sec-exr_packages}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nIt's important that you are comfortable with adding functionality.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nPlease install the `tidyverse` package *using the console*.\n\nThen, in the script you created in [Exercise -@sec-exr_scripts] load it into R.\n:::\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nWe can install the package as follows:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"tidyverse\")\n```\n:::\n\n\nNote that the title of the package needs to be in quotes (`\" \"`).\n\nWe load the package by running the following line of code from our script:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\n```\n:::\n\n\nNote that, rather inconsistently, we do *not* use quotes around the package name when loading it.\n:::\n:::\n:::\n\n\n## Summary\n\n::: {.callout-tip}\n#### Key points\n\n- We use a working directory to organise our projects\n- Using scripts we're able to keep a record of our code\n- Packages or libraries give additional functionality\n:::\n", + "markdown": "---\ntitle: \"Getting started\"\n---\n\n::: {.cell}\n\n:::\n\n::: {.cell}\n\n:::\n\n\n::: {.callout-tip}\n## Learning outcomes\n\n- Learn basic programming techniques\n\n:::\n\n## Purpose and aim\n\nUsing a programming language to analyse, visualise and communicate your data has many advantages over point-and-click programmes.\n\n* it documents analysis steps with code, aiding reproducibility\n* allows scaling to large data\n* generates high quality graphics that can be adjusted\n\n## Introduction\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nThe term \"**R**\" is used to refer to both the programming language and the\nsoftware that interprets the scripts written using it.\n\n**RStudio** is an additional software that makes it easier to \ninteract with R by providing tools that make programming easier. \nTo function correctly, RStudio needs R and therefore both need to be installed on your computer.\n\nSome advantages of using R for your data analysis include:\n\n- Analysis steps are documented with code, allowing for greater reproducibility. \n- There are thousands of packages (extensions) available, making R a very flexible \n and powerful tool, for a wide range of applications. \n- Analysis can be scaled to large data.\n- Can generate a wide range of high-quality graphics for data visualisation.\n- There is a large community of contributors.\n- It's free and open source.\n\n\n### The RStudio Interface \n\nRStudio is divided into four \"panes\", illustrated below. \nThe default layout is:\n\n- Top Left - **Source**: this is where you edit your R scripts \n (this panel might not appear until you create a script, which we demonstrate below).\n- Bottom Left - **Console**: where R will execute commands and print results.\n- Top Right - **Environment**: this will show you which objects you create \n while working with R.\n- Bottom Right - **Files**/**Plots**/**Packages**/**Help**: several tabs that allow \n you to navigate your files, view plots, view installed packages and search help files. \n\n\n![](images/00-RStudio_screen.png)\n\n:::\n\n## Getting set up\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nIt is good practice to keep a set of related data, analyses, and text\nself-contained in a single folder called the **working directory** (usually a folder \non your computer where you have all the files relating to a project you're working on). \n\nThe working directory is an important concept to understand. It is the place\nwhere R will look for and save files.\n\nAll of the scripts within this folder can then use *relative paths* to files. \nRelative paths indicate where inside the project a file is located (as opposed to \nabsolute paths, which point to where a file is on a specific computer). \nWorking this way makes it a lot easier to move your project around on your computer \nand share it with others without having to directly modify file paths in the individual \nscripts.\n\nRStudio provides a helpful set of tools to do this through its **Projects**\ninterface, which not only creates a working directory for you but also remembers\nits location (allowing you to quickly navigate to it). The interface also \npreserves custom settings and open files to make it easier to resume work after \na break. \n\n\n### Creating a new project\n\nUsually, you will already have a folder on your computer for your project, for \nexample with some data you collected or downloaded from the web. \n\nTo create an _R Project_ within the `r-workshop` directory:\n\n- From the upper menu on RStudio click: File > New project > Existing directory.\n- Click the browse... button and navigate and open your `r-workshop` folder. \n- Click on Create project. This will initiate a fresh R session.\n\nFrom now on, whenever you want to work on this project, open the the `Rproj` file \nthat was created in your `r-workshop` folder.\n\nThis will ensure your working directory is automatically set correctly. This also means \nthat you can move the project folder to a different location or even different \ncomputer. As long as you open the `Rproj` file, your working directory will be set correctly. \n\nIf you need to check your working directory, you can run `getwd()` on the console. \nIf for some reason your working directory is not what it should be, you can change it in the RStudio interface by navigating in the file browser (bottom-right panel) to where your working directory should be, clicking on the blue gear icon \nMore > Set As Working Directory.\n\nAlternatively, you can run `setwd(\"/path/to/working/directory\")` on the console to \nreset your working directory. However, your scripts should not include this line, \nbecause it will fail on someone else's computer.\n\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_project].\n:::\n\n## Writing code\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nNow that we have a project, let's run our first commands in R.\n\nOn the _console_ panel, type:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n1 + 100\n```\n:::\n\n\nAnd R will print out the answer, with a preceding `[1]`. Don't worry about\nthis for now, we'll explain that later. For now think of it as indicating\noutput.\n\nAny time you hit return and the console shows a \"`+`\" instead of a \"`>`\", it\nmeans it's waiting for you to complete the command. If you want to cancel a\ncommand you can hit Esc and RStudio will give you back the `>` prompt.\n\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_calculations].\n:::\n\n## Creating scripts\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nSo far, we've been typing these commands directly in the R console. However, if we \nclosed RStudio and later wanted to recreate these operations, there would be no \nrecord of them anywhere. \n\nIn practice, we should always write our code in a **script**, which is a plain text \ndocument with our commands written in it. \nTo create a new R script go to File > New File > R Script.\n\nThis will open a panel on the top-left. This is a text editor, which in RStudio \ndoes some syntax highlighting (it colours the code) to help read the code. \n\nAs you're adding code to the script, you can run it interactively on the console \nby pressing the shortcut Ctrl+Enter. \n\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_scripts].\n:::\n\n## Installing and loading packages\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nAdditional packages can be installed to extend the functionality of R. \nMost packages are available in a central repository called CRAN and can be \ninstalled from within R using the `install.packages()` function.\n\nFor example, to install (or update) the `tidyverse` package, you would run the \nfollowing command on the console:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"tidyverse\")\n```\n:::\n\n\nBecause the install process accesses the CRAN repository, you will need an Internet \nconnection to install packages.\n\nAfter this, you can then load the package to use it in your analysis. For the example above, we would do that as follows with the `library()` function:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\n```\n:::\n\n\n:::\n\n:::{.callout-warning}\n## Loading vs installing\nInstallation of library/package: you only need to do this once.\n\nLoading of library/package: every time you start an analysis.\n:::\n\n:::{.callout-note}\n## Exercise\nComplete [Exercise -@sec-exr_packages].\n:::\n\n\n\n## Exercises\n\n### Setting up a project {#sec-exr_project}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nSet up a project and make sure it's set as your working directory.\n\n:::\n\n### Calculations {#sec-exr_calculations}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nRun the following calculations:\n\n* `2 + 23`\n* `23 * 4`\n* `314 - 82`\n* `(12 - 4) * (6 + 2)`\n* `3 ^ 2`\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\n2 + 23\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 25\n```\n\n\n:::\n\n```{.r .cell-code}\n23 * 4\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 92\n```\n\n\n:::\n\n```{.r .cell-code}\n314 - 82\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 232\n```\n\n\n:::\n\n```{.r .cell-code}\n(12 - 4) * (6 + 2)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 64\n```\n\n\n:::\n\n```{.r .cell-code}\n3 ^ 2\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 9\n```\n\n\n:::\n:::\n\n\n:::\n:::\n:::\n\n### Creating scripts {#sec-exr_scripts}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nPlease do the following:\n\n1. Create a script called `session_01` in your working directory.\n2. Re-run the calculations from [Exercise -@sec-exr_calculations].\n3. Save the changes to the script.\n\n:::\n\n### Adding functionality {#sec-exr_packages}\n\n:::{.callout-exercise}\n\n\n{{< level 1 >}}\n\n\n\nIt's important that you are comfortable with adding functionality.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nPlease install the `tidyverse` package *using the console*.\n\nThen, in the script you created in [Exercise -@sec-exr_scripts] load it into R.\n:::\n\n::: {.callout-answer collapse=true}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nWe can install the package as follows:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninstall.packages(\"tidyverse\")\n```\n:::\n\n\nNote that the title of the package needs to be in quotes (`\" \"`).\n\nWe load the package by running the following line of code from our script:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\n```\n:::\n\n\nNote that, rather inconsistently, we do *not* use quotes around the package name when loading it.\n:::\n:::\n:::\n\n\n## Summary\n\n::: {.callout-tip}\n#### Key points\n\n- We use a working directory to organise our projects\n- Using scripts we're able to keep a record of our code\n- Packages or libraries give additional functionality\n:::\n", "supporting": [ "intro-to-programming_files" ], diff --git a/materials/_no-render/template.qmd b/materials/_no-render/template.qmd index 64933f6..ed182da 100644 --- a/materials/_no-render/template.qmd +++ b/materials/_no-render/template.qmd @@ -62,7 +62,7 @@ Description ::: ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_title]. ::: diff --git a/materials/functions-and-objects.qmd b/materials/functions-and-objects.qmd index 877cf64..35441cb 100644 --- a/materials/functions-and-objects.qmd +++ b/materials/functions-and-objects.qmd @@ -117,7 +117,7 @@ age + phd_length ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_objects]. ::: @@ -203,14 +203,14 @@ c(vec_num, vec_mixed) ::: -:::{.callout-note} -## Preferential treatment +:::{.callout-warning} +## Preferential treatment of data types Often, not all data types are equal. We won't go into too much detail here, but it's important to keep in mind that: -* presence of text in a vector often leads to *all* the elements being converted to text +the presence of text in a vector leads to *all* the elements being converted to text! ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_vectors]. ::: diff --git a/materials/intro-to-programming.qmd b/materials/intro-to-programming.qmd index ce0d45e..3395700 100644 --- a/materials/intro-to-programming.qmd +++ b/materials/intro-to-programming.qmd @@ -126,7 +126,7 @@ because it will fail on someone else's computer. ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_project]. ::: @@ -155,7 +155,7 @@ command you can hit Esc and RStudio will give you back the `>` prompt ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_calculations]. ::: @@ -181,7 +181,7 @@ by pressing the shortcut Ctrl+Enter. ::: -:::{.callout-important} +:::{.callout-note} ## Exercise Complete [Exercise -@sec-exr_scripts]. ::: @@ -215,13 +215,16 @@ library(tidyverse) ::: -:::{.callout-important} -## Exercise -Complete [Exercise -@sec-exr_packages]. +:::{.callout-warning} +## Loading vs installing +Installation of library/package: you only need to do this once. + +Loading of library/package: every time you start an analysis. ::: -:::{.callout-important} -Remember, we only need to **install** a library/package *once*. However, we need to **load** it every time we start an analysis. +:::{.callout-note} +## Exercise +Complete [Exercise -@sec-exr_packages]. :::