-
Notifications
You must be signed in to change notification settings - Fork 0
/
managing.qmd
85 lines (43 loc) · 4.22 KB
/
managing.qmd
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
# Managing your files and data {#sec-managing}
## Questions
- Why is good file and data management important in R?
- What is the concept of a project in R, and how does it help in organizing your work?
- What is the purpose of creating R project folders and scripts?
- Describe the significance of a working directory in R, and how it impacts your workflow?
- How do you create, save, and run R scripts for efficient code management?
## Learning Objectives
- Understand the importance of properly organized files and datasets for efficient R projects.
- Understand the concept of R projects and their role in organizing your work.
- Write and run R code in scripts for better code organization and reproducibility.
- Master the concept of working directories and navigate your project files effectively.
## Lesson Content
### Introduction
Good file and data management allows the user to debug code, reduce the number of coding errors, and improve data quality. Additionally, by utilizing best practices, the learner can easily share their work, organize projects, and develop a reproducible workflow.
### Projects
An R project enables the learner to keep all their work in one folder. In the R project folder, all scripts, data files, figures, tables, history, and output can be stored in sub-folders within the R project. The root folder of the project is also known as the working directory (described in the section below).
To create a new project folder:
File --\> New Project
### Scripts
To ensure that the learner has a record of all the code that they have written in the console, it is important that the learner also write the code in a script and saves it. Failure to do this will mean that the learner will have to write the code in the console every time they need to execute it. Code written in script files allows one to run it multiple times and edit when necessary.
To create a new script file:
File --\> New File --\> R Script
Save the file in your project folder
Ensure that the file names are readable for both the R software and collaborators.
Examples include:
- "my_model_1.R"
### Working Directory
The working directory is a file path or location on your computer that R uses for reading and writing files. The function `getwd()` prints the current working directory, and `setwd()` allows the user to set the working directory. This is where R looks for files that you ask it to load, and where it puts any files that you ask it to save.
The paths to files or directories can be defined using relative or absolute paths. The relative path is where the file is found in the location where the user is, and absolute includes the entire path from the root directory.
### Finding help when using packages, functions, and inbuilt datasets
- There is a built-in help system that can be accessed via the "Help" menu, or one can use the `?` symbol before the function, package, or dataset to get a brief description and instructions for use.
- Online R [documentation](https://cran.r-project.org/manuals.html) is also available to allow the user to learn the basics of R.
## Exercises
i. Create a new RStudio project called "novice_programmer_guide".
ii. Within the project folder, create a new R Script called "introduction.R" and save it.
iii. Set your working directory in the console to the root of this new project, and practice loading data from a file within it.
iv. Explain the significance of using relative and absolute file paths in R, and experiment with using relative paths to access different data files within your project directory.
v. Explain the role of the functions, `file.path()`, `list.files()`, and `dir.create()`, in R.
vi. Describe the purpose of the `.Rproj` file in an R project.
vii. How can you clear the R workspace? Explore functions like `rm()` and `rm(list=ls())` to remove unwanted objects.
## Summary
Hopefully, this lesson has demonstrated to the learner the importance of good file and data management. We have reviewed the concepts of projects, scripts, and working directories and shown how this can help with file organization, saving/reusing code, and ultimately impact project reproducibility. In the next lesson, we will see how this is important when importing data and saving analysis outputs.