|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Running commands with R |
| 4 | +order: 3 |
| 5 | +session: 1 |
| 6 | +length: 20 |
| 7 | +toc: true |
| 8 | +adapted: true |
| 9 | +attrib_name: Programming with R |
| 10 | +attrib_link: https://github.com/swcarpentry/r-novice-inflammation |
| 11 | +attrib_copywrite: Software Carpentry |
| 12 | +attrib_license: CC-BY 4.0 |
| 13 | +attrib_license_link: https://creativecommons.org/licenses/by/4.0/ |
| 14 | +--- |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +```{r setup, include=FALSE} |
| 19 | +knitr::opts_chunk$set(echo = TRUE) |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +# Executing commands in R |
| 24 | + |
| 25 | +The console pane (in RStudio, the bottom left panel) is the place where R is |
| 26 | +waiting for you to tell it what to do, and where it will show the results of a |
| 27 | +command that has been executed. This console is the fundamental component of R and is available |
| 28 | +outside of RStudio. It is recognisable due to the blurb of text at the top (which provides details |
| 29 | +on which R version you are using) and the arrowhead cursor (>). |
| 30 | + |
| 31 | +  |
| 32 | + |
| 33 | + |
| 34 | +You can type commands directly into the console |
| 35 | +and press `Enter` to execute those commands. For example we can use R as a calculator: |
| 36 | + |
| 37 | +```{r echo=TRUE} |
| 38 | +2+2 |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +# Interacting with R |
| 43 | + |
| 44 | + |
| 45 | +There are two main ways of interacting with R: using the console or by using |
| 46 | +a script. A script is a text file that contains your code. R scripts are often saved with the file extension .r or .R. |
| 47 | + |
| 48 | +We want our code and workflow to be reproducible. In other words, we want to write code in a way that |
| 49 | +anyone (including our future selves) can easily replicate, such they will obtain the same results from our code |
| 50 | +on their computer. If we type our commands directly into the Console, they will be forgotten when you close the session. |
| 51 | +It is preferable to type the commands you want to run directly into the script editor, so that you can save the script. This way, |
| 52 | +you have a complete record of what you did, |
| 53 | +you can easily show others how you did it and you can do it again later on if needed. |
| 54 | + |
| 55 | +RStudio allows you to execute commands directly from the script editor by using |
| 56 | +the <kbd>`Ctrl`</kbd> + <kbd>`Enter`</kbd> shortcut. If no text is highlighted, the command on the current |
| 57 | +line in the script (i.e. where the line containing the cursor) will be |
| 58 | +sent to the console and executed. Alternatively you can select multiple lines to |
| 59 | +send to the console for execution when you press <kbd>`Ctrl`</kbd> + |
| 60 | +<kbd>`Enter`</kbd>. |
| 61 | + |
| 62 | + |
| 63 | +There may be times when you want to type directly in the console, for example a quick calcuation. |
| 64 | +RStudio provides the <kbd>`Ctrl`</kbd> + <kbd>`1`</kbd> and <kbd>`Ctrl`</kbd> + |
| 65 | +<kbd>`2`</kbd> shortcuts allow you to jump between the script and the console |
| 66 | +windows. |
| 67 | + |
| 68 | +If R is ready to accept commands, the R console shows a `>` prompt. If it |
| 69 | +receives a command (by typing, copy-pasting or sent from the script editor using |
| 70 | +<kbd>`Ctrl`</kbd> + <kbd>`Enter`</kbd>), R will try to execute it, and when |
| 71 | +ready, show the results and come back with a new `>`prompt to indicate it is ready for |
| 72 | +the next command. |
| 73 | + |
| 74 | +If R is still waiting for you to enter more text to complete the current command |
| 75 | +the console will show a `+` prompt. It means that you haven't finished entering |
| 76 | +a complete command. This is because it is expecting a specific character, that |
| 77 | +finishes a command you have already started. For example, if you open a parenthesis or |
| 78 | +quotation, it needs to be closed before the command can be processed. Another way of thinking about this |
| 79 | +is R expects for every left-parentheses you include, a right-parentheses to accompany it. Similarly, it expects an even |
| 80 | +number of quotation marks. This can be quite frustrating, as it might not be obvious what R is waiting for, but if |
| 81 | +you're in RStudio and this happens, click inside the console window and press |
| 82 | +`Esc`; this will cancel the incomplete command and return you to the `>` prompt. |
| 83 | + |
| 84 | + |
| 85 | +# Set the Working Directory |
| 86 | + |
| 87 | +When using R, often we want to analyse data stored in files on our computer and potentially save some output. |
| 88 | + |
| 89 | +It is important to note that we are always working in a folder or directory on our computer. There are times when this may not be |
| 90 | +important (for example running a simulation from given parameters). |
| 91 | +But if we want a specific file, we need to tell R where to find it. We do using by specifying the filepath. One common issue, when |
| 92 | +starting out with programming is understanding where a program (in this case R/Rstudio) is looking for the file(s). |
| 93 | + |
| 94 | +Theoretically, we can find any file from any location given the correct filepath. However, to make our code simpler, it can be helpful |
| 95 | +to change location, and move to a folder where the files we want to work on are located. The folder we are currently in is called the working directory. |
| 96 | + |
| 97 | +Let's create a folder on our computer, to store all the files we will work on for this series of workshops. On your computer, open up a |
| 98 | +file explorer window (outside of Rstudio) and create a folder called **R-workshop** somewhere memorable. For example on the Desktop. |
| 99 | + |
| 100 | +In Rstudio, we can change the working directory through the drop down menus by selecting options |
| 101 | + |
| 102 | +`Session` -> `Set Working Directory` -> `Choose Directory...` |
| 103 | + |
| 104 | +We then search for and select the folder we want. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +You will note that in the R console a line of code has appeared. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +This is the R command that is required to change the current working directory using the function `setwd()`. |
| 113 | +The text string inside the "()" is the filepath to the folder we want to change to. If you save this to your script, |
| 114 | +next time you won't need to click through the menus, you can execute this line of code instead. |
| 115 | + |
| 116 | +You should also see that in the File Pane (bottom-right), this has also changed to this new directory. We need to |
| 117 | +know where we are such that we can navigate R to find files and locations appropriately. If we ever get lost we can use `getwd()` to get the path |
| 118 | +of our current location. |
| 119 | + |
| 120 | +``` {r} |
| 121 | +getwd() |
| 122 | +``` |
| 123 | + |
| 124 | +<br> |
| 125 | +<details> |
| 126 | + <summary> Absolute vs Relative Filepaths </summary> |
| 127 | + <pre> |
| 128 | + <p> |
| 129 | + |
| 130 | +We can specify filepaths in two ways: |
| 131 | + |
| 132 | +1. Absolute - this is where we direct R how to get there from the root folder, which is the uppermost folder in the system and is denoted "/". |
| 133 | +Any file path that starts with "/" is an absolute filepath. |
| 134 | +2. Relative - this is where we direct R how to get to the new folder from where we currently are. For example to get to a sub-folder of the current |
| 135 | +folder we can just provide the name of the current folder. We can use shortcuts to go up a folder (".."). Relative file paths do not start with "/" |
| 136 | +</p> |
| 137 | +</pre> |
| 138 | +</details> |
| 139 | +<br> |
| 140 | + |
| 141 | +# Defining Variables |
| 142 | + |
| 143 | +A key concept in any programming language is that of a variable. A variable is just a name, such as `x`, `current_temperature`, or `subject_id` which |
| 144 | +represents some value. We use the word value here loosely as it could be a singular number, a matrix, or a more complex numerical structure. It also |
| 145 | +does not have to even be a number it could be a string of text. The idea though is it's value might change and we want to simplify how we process the value. |
| 146 | + |
| 147 | +In R, we can create a new variable simply by assigning a value to it using `<-` |
| 148 | + |
| 149 | +```{r} |
| 150 | +weight_kg <- 55 |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +Once a variable has a value, we can print it by typing the name of the variable and hitting `Enter` (or `return`). |
| 155 | +In general, R will print to the console any object returned by a function or operation *unless* we assign it to a variable. |
| 156 | + |
| 157 | +```{r} |
| 158 | +weight_kg |
| 159 | +``` |
| 160 | + |
| 161 | +We can do arithmetic with the variable: |
| 162 | + |
| 163 | +```{r} |
| 164 | +# weight in pounds: |
| 165 | +2.2 * weight_kg |
| 166 | +``` |
| 167 | + |
| 168 | +We can also change an object's value by assigning it a new value: |
| 169 | + |
| 170 | +```{r} |
| 171 | +weight_kg <- 57.5 |
| 172 | +# weight in kilograms is now |
| 173 | +weight_kg |
| 174 | +``` |
| 175 | + |
| 176 | +If we imagine the variable as a sticky note with a name written on it, |
| 177 | +assignment is like putting the sticky note on a particular value: |
| 178 | + |
| 179 | + |
| 180 | +<img src="../images/python-sticky-note-variables-01.svg" alt="Variables as Sticky Notes" /> |
| 181 | + |
| 182 | + |
| 183 | +This means that assigning a value to one object does not change the values of other variables. |
| 184 | +For example, let's store the subject's weight in pounds in a variable: |
| 185 | + |
| 186 | +```{r} |
| 187 | +weight_lb <- 2.2 * weight_kg |
| 188 | +# weight in kg... |
| 189 | +weight_kg |
| 190 | +# ...and in pounds |
| 191 | +weight_lb |
| 192 | +``` |
| 193 | + |
| 194 | +<img src="../images/python-sticky-note-variables-02.svg" alt="Creating Another Variable" /> |
| 195 | + |
| 196 | +and then change `weight_kg`: |
| 197 | + |
| 198 | +```{r} |
| 199 | +weight_kg <- 100.0 |
| 200 | +# weight in kg now... |
| 201 | +weight_kg |
| 202 | +# ...and weight in pounds still |
| 203 | +weight_lb |
| 204 | +``` |
| 205 | + |
| 206 | +<img src="../images/python-sticky-note-variables-03.svg" alt="Updating a Variable" /> |
| 207 | + |
| 208 | +Since `weight_lb` doesn't "remember" where its value came from, it isn't automatically updated when `weight_kg` changes. |
| 209 | +This is different from the way spreadsheets work. |
| 210 | + |
| 211 | + |
| 212 | +<details> |
| 213 | + <summary> Assignment operator </summary> |
| 214 | + <pre> |
| 215 | + |
| 216 | + `<-` is the assignment operator. It assigns values on the right to objects on |
| 217 | + the left. So, after executing `x <- 3`, the value of `x` is `3`. The arrow can |
| 218 | + be read as 3 **goes into** `x`. For historical reasons, you can also use `=` for assignments, |
| 219 | + but not in every context. Because of the (slight)[http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html] |
| 220 | + (differences)[https://web.archive.org/web/20130610005305/https://stat.ethz.ch/pipermail/r-help/2009-March/191462.html] in syntax, |
| 221 | + it is good practice to use always `<-` for assignments, except when specifying the values of |
| 222 | + arguments in functions, when only `=` should be used, see below. |
| 223 | + |
| 224 | + In RStudio, typing <kbd>Alt</kbd> + <kbd>-</kbd> (push <kbd>Alt</kbd> at the |
| 225 | + same time as the <kbd>-</kbd> key) will write ` <- ` in a single keystroke. |
| 226 | + </pre> |
| 227 | +</details> |
| 228 | + |
| 229 | + |
| 230 | +## Activity: Assigning Values to Variables |
| 231 | + |
| 232 | +Draw diagrams showing what variables refer to what values after each statement in the following program: |
| 233 | + |
| 234 | +~~~ |
| 235 | +mass <- 47.5 |
| 236 | +age <- 122 |
| 237 | +mass <- mass * 2.0 |
| 238 | +age <- age - 20 |
| 239 | +~~~ |
| 240 | + |
| 241 | +<details> |
| 242 | + <summary> Printing with Parentheses </summary> |
| 243 | + <pre> |
| 244 | + |
| 245 | + An alternative way to print the value of a variable is to use () around the |
| 246 | + assignment statement. |
| 247 | + |
| 248 | + As an example: |
| 249 | + |
| 250 | + `(total_weight <- weight_kg + weight_lb)` |
| 251 | + |
| 252 | + adds the values of `weight_kg` and `weight_lb`, |
| 253 | + assigns the result to the `total_weight`, |
| 254 | + and finally prints the assigned value of the variable `total_weight`. |
| 255 | + </pre> |
| 256 | +</details> |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | +## Naming objects in R |
| 262 | + |
| 263 | +You can use any combination of alphanumeric characters, along with dots and underscores, to name an R object. But there are a few ___exceptions___: |
| 264 | + |
| 265 | +* Names cannot start with a number; |
| 266 | +* Names cannot have spaces; |
| 267 | +* Names cannot be a standalone number such as ```12``` or ```0.34```; |
| 268 | +* Names cannot be a reserved word such as ```if```, ```else```, ```function```, ```TRUE```, ```FALSE``` and ```NULL``` just to name a few |
| 269 | +(to see the full list of reserved words, type ```?Reserved```). |
| 270 | + |
| 271 | +Examples of __*valid names*__ include ```a```, ```dat2```, ```cpi_index```, ```.tmp```, and even a standalone dot ```.``` (though a dot can |
| 272 | +make reading code difficult under certain circumstances). |
| 273 | + |
| 274 | +Examples of __*invalid names*__ include ```1dat```,``` dat 2``` (note the space between ```dat``` and ```2```), ```df-ver2``` (the dash is |
| 275 | +treated as a mathematical operator), and ```Inf``` (the latter is a reserved word listed in the ```?Reserved``` help document). |
| 276 | + |
| 277 | +You can mix cases, but use upper cases with caution since some letters look very much the same in both lower and upper cases (e.g. s and S). |
| 278 | + |
| 279 | +# Functions |
| 280 | + |
| 281 | +Functions are a core concept in programming, they are commands which tells R to perform a particular task. The utility of a function is that it |
| 282 | +will perform its given action again and again. R identifies functions by the presence of "()" following their name. You can customise how the function behaves |
| 283 | +by the use of arguments, this is additional information provide to the function to allow it to run). |
| 284 | + |
| 285 | +We have already used two functions in this workshop. First we used `setwd()`. We used this function with an argument, which was the path to the folder we |
| 286 | +wanted to change to. We can reuse this function to change to an unlimited number of different folders on our computer. Then we used `getwd()`, this is an example |
| 287 | +of a function that doesn't require any arguments. |
| 288 | + |
0 commit comments