Code
1 + 1
For this course we will be using R. R is a free software environment for statistical computing and graphics. To run the code blocks for this course on your own computer you will need to have installed R. This is available for Linux, MacOS and Windows.
IMPORTANT: Mac users, make sure you have installed the right version of R for your laptop’s processor (M1/M2 or Intel)
At the time of publishing, I am using R version 4.3.0 (2023-04-21).
RStudio is an integrated development environment (IDE) that can make programming and other tasks easier in R. An open source edition is available to download and install.
You need to install R before you install R Studio.
Once R and R Studio are installed, open R Studio on your computer and type the following in the Console to the left or bottom left of the screen, alongside the prompt, >
.
then hit Enter
/Return
. You should, of course, obtain the answer 2, as below.
[1] 2
You will also find that if you move your mouse to over the code block above, an option appears to copy the code to the clipboard, allowing you to then paste it into the R Console window in R Studio.
The base functions of R are greatly extended by the very many packages/libraries that have been developed for it. At the time of writing, there are 20103 of these on CRAN, which is the main repository for them. Many of these have been grouped into ‘tasks’ and topic areas, which can be viewed here.
Most of the packages that will be needed for this course will be installed as they are needed. However, some will be used so regularly that we should install them now. Cut and paste the following code chunk into the Console and hit Enter/Return.
Run the code above even if you have the packages installed already so that you also have available all the packages that these depend upon and link to.
If you type getwd()
into the R Console you will obtain your current working directory – the default location to look for files and to save content to. Mine is,
You may wish to change this to something else each time you start R. You can do this using the drop-down menus. There is also the function setwd(dir)
– type ?setwd
in the R Console to learn more.
Rather than having to set your working directory each time you restart R, you could also create a new project in R by using File –> New Project… from the dropdown menus and create it either in a new directory (probably most sensible) or an existing one.
There is nothing especially magical about a project in R. As stated here, “a project is simply a working directory designated with a .RProj file. When you open a project (using File/Open Project in RStudio or by double–clicking on the .Rproj file outside of R), the working directory will automatically be set to the directory that the .RProj file is located in.” However, it is that which makes it useful: when you open a project you know that you are going to be working in a specific folder on your computer which them becomes the default ‘container’ to save files to or to download them from.”
It would be a good idea to create a new project now which can then be the folder and working directory for this course and its contents.
You may notice that I prefer a blue to a white screen when working in R. To change it to this, from the drop-down menus use Tools –> Global Options… -> Appearance, and select Solarized Dark as the Editor theme. You may, of course, have your own preference.
---
title: "Getting Started"
---
```{r}
#| include: false
installed <- installed.packages()[,1]
pkgs <- c("XML", "tidyverse", "sf")
install <- pkgs[!(pkgs %in% installed)]
if(length(install)) install.packages(install, dependencies = TRUE, repos = "https://cloud.r-project.org")
```
## Install R
For this course we will be using R. R is a free software environment for statistical computing and graphics. To run the code blocks for this course on your own computer you will need to have [installed R](https://cran.r-project.org/){target="_blank"}. This is available for Linux, MacOS and Windows.
![](hazard.gif){width="75"}
<font size = 3>**IMPORTANT: Mac users, make sure you have installed the right version of R for your laptop's processor (M1/M2 or Intel)**</font>
At the time of publishing, I am using `r R.Version()$version`.
## Install R Studio
RStudio is an integrated development environment (IDE) that can make programming and other tasks easier in R. [An open source edition](https://posit.co/download/rstudio-desktop/){target="_blank"} is available to download and install.
![](hazard.gif){width="75"}
<font size = 3>You need to install R before you install R Studio.</font>
## Open R Studio
Once R and R Studio are installed, open R Studio on your computer and type the following in the Console to the left or bottom left of the screen, alongside the prompt, `>`.
```{r}
#| echo: true
#| eval: false
1 + 1
```
then hit `Enter`/`Return`. You should, of course, obtain the answer 2, as below.
```{r}
#| echo: false
#| eval: true
1 + 1
```
You will also find that if you move your mouse to over the code block above, an option appears to copy the code to the clipboard, allowing you to then paste it into the R Console window in R Studio.
![](copy.png)
## Install additional libraries/packages
The base functions of R are greatly extended by the very many packages/libraries that have been developed for it. At the time of writing, there are `r available.packages(repos = "https://cran.r-project.org", filters = c("CRAN", "duplicates")) |> nrow()` of these on [CRAN](https://cran.r-project.org/web/packages/available_packages_by_name.html){target="_blank"}, which is the main repository for them. Many of these have been grouped into 'tasks' and topic areas, which can be viewed [here](https://cran.r-project.org/web/views/){target="_blank"}.
Most of the packages that will be needed for this course will be installed as they are needed. However, some will be used so regularly that we should install them now. Cut and paste the following code chunk into the Console and hit Enter/Return.
```{r}
#| eval: false
install.packages("proxy", dependencies = TRUE)
install.packages("sf", dependencies = TRUE)
install.packages("tidyverse", dependencies = TRUE)
```
![](hazard.gif){width="75"}
<font size = 3>Run the code above even if you have the packages installed already so that you also have available all the packages that these depend upon and link to.</font>
## Changing the working directory
If you type `getwd()` into the R Console you will obtain your current working directory -- the default location to look for files and to save content to. Mine is,
```{r}
getwd()
```
You may wish to change this to something else each time you start R. You can do this using the drop-down menus. There is also the function `setwd(dir)` -- type `?setwd` in the R Console to learn more.
![](change_dir.png)
## Organising your files in a project
Rather than having to set your working directory each time you restart R, you could also create a new project in R by using File --\> New Project... from the dropdown menus and create it either in a new directory (probably most sensible) or an existing one.
There is nothing especially magical about a project in R. As stated [here](https://bookdown.org/ndphillips/YaRrr/projects-in-rstudio.html){target="_blank"}, "a project is simply a working directory designated with a .RProj file. When you open a project (using File/Open Project in RStudio or by double--clicking on the .Rproj file outside of R), the working directory will automatically be set to the directory that the .RProj file is located in." However, it is that which makes it useful: when you open a project you know that you are going to be working in a specific folder on your computer which them becomes the default 'container' to save files to or to download them from."
![](hazard.gif){width="75"}
<font size = 3>**It would be a good idea to create a new project now which can then be the folder and working directory for this course and its contents.**</font>
## Changing the appearance of R Studio
You may notice that I prefer a blue to a white screen when working in R. To change it to this, from the drop-down menus use Tools --\> Global Options... -\> Appearance, and select Solarized Dark as the Editor theme. You may, of course, have your own preference.