generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_project-oriented-workflow.Rmd
98 lines (62 loc) · 3.05 KB
/
02_project-oriented-workflow.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
# Project-oriented workflow
**Learning objectives:**
- Explain why `setwd()` makes it difficult to share code.
- Describe the structure of a project and how that structure makes code more portable.
- Use an IDE to work with projects.
## Setting a working directory
There are several way to make your life easier when working on more than one file in RStudio.
It might be helpful to **set a working directory** especially if you use `.R` scripts more than `.Rmd` or `.qmd` files, as these types of files have embedded a *working directory* which is where the files are saved in, while the `.R` scripts have not.
If you'd like to source other files in your `.R` scripts, or save the output in the same folder where your script is saved, it might be a requirement to set a working directory to avoid error warnings.
Let's have a look at some examples:
1. I saved a .R script on a folder inside this project and named it: `2_example.R`. I am now in a `.Rmd` file so it shouldn't be an issue to source the file's content from here.
```{r echo=FALSE}
knitr::include_graphics("images/2_image1.png")
```
The file is not found as I need to specify the folder where I positioned it. And it worked!
```{r}
source("scripts/2_example1.R")
```
What happens if I do the same from an `.R` script?
```{r echo=FALSE}
knitr::include_graphics("images/2_image3.png")
```
Setting a working directory works for the user who set it, but it might be a cause of confusion when working on a team, and other in the team load the file in their machine setting the same working directory. Obviously the path can be easly different, just thinking about the user name of the machine in use and then other files' names can differ as well.
A workaround this is to use **{rstudioapi}** package. As in this example:
```{r eval=FALSE}
library(rstudioapi)
```
Here the **wd** is set directly to the `ex1` object in the *scripts* folder:
```{r eval=FALSE}
setwd(getSrcDirectory(ex1))
```
More info about the wd can be retrieved with:
rstudioapi::getActiveDocumentContext()
```{r eval=FALSE}
rstudioapi::getActiveDocumentContext()
```
In general, if you are in `.R` script and work on a team sharing work, it might be helpful to use:
```{r eval=FALSE}
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
```
```{r eval=FALSE}
getwd()
```
Another option is to use **{here}** package:
```{r eval=FALSE}
here::set_here()
```
## Organize work into projects & use an IDE
Create a project on RStudio and store all the files needed for it.
RStudio provides an integrated development environment (IDE) which is a set of tools built to help you be more productive with R.
[source](https://posit.co/download/rstudio-desktop/)
## Meeting Videos {.unnumbered}
### Cohort 1 {.unnumbered}
`r knitr::include_url("https://www.youtube.com/embed/LxOsSswRBTs")`
<details>
<summary>Meeting chat log</summary>
```
00:44:28 Federica Gazzelloni: https://rstudio.github.io/rstudioapi/
00:50:32 mohamed.shoeb: I have to leave now. See you in the next session!
00:54:42 Federica Gazzelloni: https://www.alfredapp.com/
```
</details>