Repository navigation
Expand file tree
/
Copy pathAQuikIntro2RandRStudioInQuarto.qmd
More file actions
296 lines (175 loc) · 10.4 KB
/
Copy pathAQuikIntro2RandRStudioInQuarto.qmd
File metadata and controls
296 lines (175 loc) · 10.4 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
title: "A quick introduction to R and Quarto from within RStudio"
author: "Tiago A. Marques"
date: "`r format(Sys.Date(), '%d %B %Y')`"
format: revealjs
editor: visual
embedded-resources: standalone
---
## Objective
This presentation provides an introduction to R and RStudio.
It covers topics to be used in courses that require such introduction, including TAM's courses Ecologia Numérica and Modelação Ecológica at FCUL.
Therefore you are most likely seeing it a class being delivered, unless you are working on your own.
## Independent use
This document could be also used as an introduction to the self-paced learning github repository hosted at <https://github.com/TiagoAMarques/AnIntro2RTutorial>
The tutorial has a landing webpage (and hence looks a bit friendlier) here:
<https://tiagoamarques.github.io/AnIntro2RTutorial/>
This document is also available online at RPubs at
<https://rpubs.com/talomarques/AnIntroToRandRStudio>
```{=html}
<!--
when you republish this presentation, it shows up in chrome as
not being at this adreess, but at something like
https://rpubs.com/talomarques/1342657
This is irrelevant and the above address will work regardless
-->
```
::: footer
This presentation was created using [Quarto](https://quarto.org/) and [revealjs](https://quarto.org/). Want to try yourself. Check its amazing capabilities [here](https://quarto.org/docs/presentations/revealjs/demo/#/title-slide)
:::
## Obtaining the material
You can get all the material by downloading and unziping the [github repository](https://tiagoamarques.github.io/AnIntro2RTutorial/) contents. See image below: press the button at the end of the red arrow, then that of the blue arrow, and finally unzip the material into a folder of your choice.
{.absolute top="300" left="30" width="1000" height="400"}
## Learning goals
- Obtain a working knowledge about the R software and environment, to be able to implement basic data analysis
- Integration of R and RStudio, an helpful R interface, via a short introduction to R and RStudio followed by a hands on assisted tutorial
## The R environment
- R is both a programming language and an environment for statistical computation
- R is free open source software
- Created in 1995 by Ross Ihaka and Robert Gentleman, Department of Statistics of the University of Auckland
- gets its name from another famous programming language for data analysis, S
- Project run by the "R Core Development Team"
- Software and many resources available at the [r-project.org/](http://www.r-project.org)
::: footer
I am using version 4.3.1 (2023-06-16) (those guys are fast...this will probably be outdated!)
:::
## R: a selected timeline {background="#43464B" background-image=""}
{.absolute top="100" left="30" width="1000" height="500"}
::: footer
In Giorgi et al. 2022 The R Language: An Engine for Bioinformatics and Data Science Life 12: 648 DOI: [10.3390/life12050648](https://www.mdpi.com/2075-1729/12/5/648)
:::
## The R base + packages
::::: columns
::: {.column width="60%"}
- R base installation comes with a number of libraries which provide access to a large number of commonly used features (e.g. linear models, survival analysis, basic statistical distributions, etc)
- One of R's key strengths comes from the fact that many more packages are available online, under constant increase
:::
::: {.column width="40%"}
{.absolute top="100" left="620" width="500" height="500"}
:::
:::::
::: footer
[image source at r4stats.com](http://r4stats.com/articles/popularity/) - [different sources](https://medium.com/@tomazkastrun/r-package-updates-and-release-dates-statistics-and-another-rise-of-r-language-5125faa80eb1) provide different numbers, but overall patters are the same
:::
## The R environment
- R was traditionally accessed solely via the command line, making the learning curve rather steep for beginners
{fig-align="center" style="border: 3px solid #dee2e6;" width="780"}
- Here we focus on Rstudio, a program to interface with R, making many of the most common tasks simpler. Let's open Rstudio shall we?
::: footer
[Download RStudio here](https://posit.co/blog/rstudio-2022-07-0-what-s-new/)
:::
## RStudio
::::: columns
::: {.column width="65%"}
- (as R) Free and open source
- An integrated development environment for data analysis
- Simplifies the user interaction with R
- In a single application you have access to a number of windows containing the code, the command line, the figures, the help, a windows explorer, etc.
:::
::: {.column width="35%"}
{.absolute top="200" left="700" width="400" height="300"}
:::
:::::
## 4 windows, 4 ways to interact with R
{.absolute top="100" left="50" width="1000" height="600"}
## The code and plots tabs
{.absolute top="100" left="50" width="1000" height="600"}
## The environment tab
{.absolute top="100" left="50" width="1000" height="600"}
## The packages tab
{.absolute top="100" left="50" width="1000" height="600"}
## History and "windows explorer" tabs
{.absolute top="100" left="50" width="1000" height="600"}
## The help tab
{.absolute top="100" left="50" width="1000" height="600"}
## Aditional online resources
R HAS AMAZING (ENDLESS) ONLINE RESOURCES

::: footer
[Check the landing page at my AnIntro2RTutorial github repos](https://github.com/TiagoAMarques/AnIntro2RTutorial)
:::
## Quarto and Dynamic Reports

## Creating a dynamic report I
Let us create a dynamic report from scratch in Quarto via RStudio

## Set Title and author
Let us create a dynamic report from scratch in Quarto via RStudio
## 
## Almost there
This is what you should be seeing. Check your title and name are there.

## Compile the .qmd
To do so press the "Knit" button. After you do you are asked to save the files, tell RStudio where it should do so it and what is the file name (I am using "My1stDynamicReport.Rmd")
## 
## If you run into issues
Regarding "latin" characters showing, like say "é", "õ" and â"
{.absolute top="200" left="50" width="1000" height="450"}
## Et voilá
Your first quarto dynamic report is ready
## 
## If you want it in the viewer pane
Unless you have customized RStudio to do otherwise, the html appears in a new window. To have it in the viewer pane, change the options under `Tools| Global Options`
## Possible outputs
- Your now have your first HTML automatically generated, obtained from compiling (=knitting) the .qmd file.
- Actually, the output could also be a Word file or a PDF.
- Not all possible features are available in Word
- Generating PDF's requires some additional software (LaTex) - you can try that on your own. Good luck ;)
## Experiment with the .qmd file
- Try to insert a new bit of code with `Code|insert chunck` (shortcut `Ctrl+Al+I`)
- Change a chunck's options (e.g. `eval`, `echo`, etc) from `TRUE` to `FALSE` and see what whappens to the output
- Try to use dynamic numbers like this one: `r mean(rnorm(5))` printing the mean value of 5 random Gaussian numbers. For that you need to use something like \`? mean(rnorm(5))\` (note: replace the "?" with the leter "r" so that it knows you want to run r code).
- For more, check e.g. the [quarto cheat sheet](https://rstudio.github.io/cheatsheets/quarto.pdf)
## Warning: command line vs .Rmd
{fig-align="center"}
- When you run some code within a .qmd, it is only run in the .qmd
- When you run some code in the command line, it is only run in the command line
- Therefore, reading the data in the command line does NOT mean it will be available for a dynamic report, and vice versa!
## Tips and Tricks
- It is much safer to compile the entire document than to run code chunks separately
- A .qmd is not a usual script, it has both code and text. The result is a dynamic document, with Text, Code, and Ooutputs of the code
- The code must go always, always, always inside a code chunck!
- Comments to the code can also be within code chunks (preceded by "\#" -- the hash symbol).
```{r,echo=TRUE}
# EXAMPLE: this would be a comment
# run the next lines of code the result is 11
A<-3;B<-8;A+B
```
## Useful Quarto web resources
- An quarto template with lots of tips and trick made just for YOU [here](https://github.com/TiagoAMarques/template_4_quarto_report)
- About R Markdown, the official site [here](https://rmarkdown.rstudio.com/)
- The online free [book](https://bookdown.org/yihui/rmarkdown)
- Chapter 27 on RMarkdown from the fRee online book: R FOR DATA SCIENCE [here](https://r4ds.had.co.nz/r-markdown.html)
-how to write a book with markdown, with several VERY USEFUL BOOK examples available for free [here](https://bookdown.org/)
BE PROACTIVE: EXPLORE RESOURCES
## And finally...
Open the hands on R tutorial ("TAMsIntro2RviaRStudioTutorial.pdf") and work through it
{fig-align="center"}
## If you are seeing this slide
It is because you either:
1. have completed the tutorial (well done you, keep it going!)
2. are being shown the slide in class by me/someone (and hence might do the tutorial next, go for it!)
3. are not following the rules if working alone (shame on you ;), but hey, rules are meant to be broken, YOLO.
Next couple of slides are about learning more about R on your own. Besides all the stuff at the landing page of my github repository here:
<https://github.com/TiagoAMarques/AnIntro2RTutorial>
You can try a couple of things
## Teach yourself
A great place to start [Ten simple rules for teaching yourself R](https://www.dropbox.com/s/6j5byl93z54hcpw/journal.pcbi.1010372.pdf?dl=0)
{.absolute top="150" left="100" width="1000" height="600"}
## Get a (virtual) R Tutor
You can find one within the [swirl R package](https://swirlstats.com/): try it out!
{fig-align="center"}
## And now, off you go!
The path to R mastery is winding, but the reward is worth it...
{fig-align="center"}
## The end {background="#43464B" background-image="extfiles/backgRound.png"}