-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-intro_to_R.Rmd
executable file
·155 lines (97 loc) · 9.61 KB
/
01-intro_to_R.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
```{r setup, include = FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
# Introduction to R and posit Cloud - Week 2A
You should aim to complete this chapter in Week 2, Semester 1.
## What is R and posit Cloud?
So hopefully you have read through Chapter 1, where R and the posit Cloud interface were introduced in this module. But what really is R and what is the difference between R and posit Cloud?
Essentially R is a programming language that is commonly used in statistical computing, data handling, data visualisation and data analysis. Posit Cloud is a cloud based interface for a piece of software called RStudio (we wont be using the non cloud based RStudio in this sub-module, so we wont explore this software further here). Posit Cloud uses the R programming language but has a nice user friendly interface and is a great tool for learning how to conduct analysis in R.
We are using the posit cloud rather then RStudio because it means that no one has to worry about installing extra software on their own computers and everyone will be working with the same software versions. You will all need to create your own free accounts on posit Cloud, but first have a look at the short video below introducing you to the interface.
<div style="position:relative;height:0;padding-bottom:40%"><iframe width="560" height="315" src="https://www.youtube.com/embed/3l02OgYYb0I" title="Intro to posit Cloud" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
<p>
Now that you've watched the video, create your own Free posit Cloud account [here](https://login.rstudio.cloud/login?redirect=%2Foauth%2Fauthorize%3Fredirect_uri%3Dhttps%253A%252F%252Frstudio.cloud%252Flogin%26client_id%3Drstudio-cloud%26response_type%3Dcode%26show_auth%3D0%26show_login%3D1).
## A warning on managing your work flow
Your free posit Cloud account comes with some restrictions which you should be aware of. These are;
* You may have up to 50 projects in total
* You are limited to 25 project hours per month
* You have up to 1GB of RAM and 1 CPU per project
For the purposes of our work here these restrictions should not be a problem. But I strongly suggest you don't leave your coursework to the last minute. If you try and pack in a whole semester or years worth or material into a single month, you will exceed those 25 project hours. There are some options of last resort should this happen, you could install RStudio on your desktop for example, or set up an alternative posit cloud account with an alternative email address, but things will get complicated. Lets avoid that where we can! There are some ways you can increase the efficiency of your workspace which are documented below in section 2.4.
## Creating your first posit Cloud Project
Once you have created an posit Cloud account you should be presented with this window
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Screen for when you first login to posit Cloud", fig.cap ="Your workspace in posit Cloud"}
knitr::include_graphics("figures/posit1.jpg")
```
Under __Spaces__ go to __Your Workspace__ and under __Projects__ create a __New Project > New R studio Project__.
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Screen for creating a new project", fig.cap ="Creating a new project in posit Cloud"}
knitr::include_graphics("figures/posit2.jpg")
```
Lets name this project `testing_R`, notice that I have no spaces in my project name. Instead of a space I have used an underscore, there are a number of good habits you should try and adopt when project or file naming and not including spaces is one of them, we will go over this in more depth in lectures and later chapters. You can rename your project by clicking on __Untitled Project__ at the top of the window, and typing in your new project name.
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Screen for renaming your project", fig.cap ="Renaming your project"}
knitr::include_graphics("figures/posit3.jpg")
```
You will see that your new project has three panels with tabs showing the Console, Environment and Files for your project.
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Screen for your r studio workspace", fig.cap ="The three basic panels of an R Studio workspace"}
knitr::include_graphics("figures/posit4.jpg")
```
## Maximising the efficiency of your workspace
We know we have limited project hours on our free posit Cloud accounts to reduce your resource usage. In your project, go to settings (the cogwheel sign), Resources, and drag all of the sliders down to the minimum setting, apply the changes, this will mean the project has to restart. This will mean that running the project for 1 hour will consume 0.5 project hours. We wont be doing anything particularly computationally intensive so there is no need for RAM of CPUs to be higher.
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Making your project more efficient", fig.cap ="How to make your project more efficient"}
knitr::include_graphics("figures/posit5.jpg")
```
## Entering commands directly into the consol
Lets start by playing with some commands in the console. Type or copy and paste the simple calculation (or command) shown below, into the console. Your text should appear next to the `>` symbol. Press `Enter` on your keyboard, this will instruct R to run the command.
```{r}
5 + 9
```
The two final lines of your output should look like this:
```
> 5 + 9
[1] 14
```
So... your initial command `5 + 9` is shown after the `>`, symbol and the resulting output from R is shown after `[1]`. Don't worry too much about the syntax of `>` and `[1]` here. You can think of `>` as meaning that R is ready to receive a command and `[1]` as R telling you that the answer to the first part of your question is here (in this case `14`).
Try out some other commands... What happens when you input the following?
```{r}
362 * 12
```
```{r}
55 / 5
```
```{r}
(40 / 990) * 100
```
```{r}
4^2
```
See if you can work out what 30% of 735 is using the R console
## Sending commands down to the console from an R script
I have already mentioned the __reproducibility__ factor as an advantage of using posit Cloud. This is because you can record and run all of your commands from an R script within posit Cloud. This means that you have a written record of your analysis workflow, what you did to your data at which stage, and you can do all of this without altering the original data files! This is super important because it means that if you revisit your work in a few weeks/months/years you can see exactly what you did AND if someone else needs to rerun any of your analysis or use your workflow on some other data, they can!
So lets go about setting up your new script. You currently have three panels in posit Cloud. If you go to __file > New File > R Script__ a new panel will open.
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "creating a new r script", fig.cap ="How to create a new R script"}
knitr::include_graphics("figures/posit6.jpg")
```
```{r, eval=TRUE, echo=FALSE, out.width="90%", fig.alt= "Screen for your r studio workspace", fig.cap ="The four panels of an R Studio workspace"}
knitr::include_graphics("figures/posit7.jpg")
```
This new panel is essentially a text file where you can write your commands into a script and then send them down to the console when you want to run them. Lets have a play. Copy the below into your new R script.
```{r}
362 * 12
55 / 5
(40 / 990) * 100
4^2
```
These are all commands you have run before but now if you save your script you will have a text based document with your progress saved. It doesn't make a huge amount of sense to do this now because these are just a un-associated set of calculations and we are just playing with the interface, but you could if you wanted to.
Ok, so now we are ready to execute our commands. You can do this with each calculation individually, line by line, or you can run the whole script.
To run your script line by line, place your cursor on the line you wish to run and you can either;
* Click on the __Run__ button on the top right of the script panel
* Press __ctrl + Enter__ (or __Command + Enter__ on mac)
Or if you wish to run the entire script you can either;
* Manually highlight the script and click the __Run__ button or press __ctrl + Enter__ (or __Command + Enter__ on mac)
* Press __ctrl + A__ (or __Command + A__ on mac) and then click the run button or press __ctrl + Enter__ (or __Command + Enter__ on mac)
## A note on comments
When you are writing a script you can leave notes for yourself. This can be extremely useful if you need reminding on what a particular piece of script is doing. This is a good learning tool while you are still learning how to use R, but its also a good habit to get into for later. As you progress with R you will inevitably start constructing quite intricate scripts, leaving good notes on what each part of the script is doing is very useful for you and for anyone else who may later come to use your script. You can leave notes or comments in scripts if you precede them with `#`. If R sees `#` it will ignore all text on that line that comes after it. For example, copy and paste the following into your script and try running it line by line.
```{r}
# Four squared can be calculated with the following command
4^2
```
In this case I have reminded myself how to calculate 4 squared, everything after the `#` will be ignored by R when you try to run the line.
Well Done! You have started writing and running simple code. But R is capable of doing so much more than simple calculations. Next week in Chapter 3 we will explore how we can create objects, and use packages and functions.