Add bit on naming files + light edits
This commit is contained in:
parent
003a3b949e
commit
cc736d2fd7
|
@ -19,12 +19,49 @@ Keep experimenting in the console, but once you have written code that works and
|
|||
RStudio will automatically save the contents of the editor when you quit RStudio, and will automatically load it when you re-open.
|
||||
Nevertheless, it's a good idea to save your scripts regularly and to back them up.
|
||||
|
||||
TODO: Add file naming advice
|
||||
## Naming files
|
||||
|
||||
Saving your code in a script requires creating a new file that you will need to name.
|
||||
It might be tempting to name this file `code.R` or `myscript.R`, but you should think a bit harder before choosing a name for your file.
|
||||
Three important principles for file naming are as follows:
|
||||
|
||||
1. File names should be machine readable: Avoid spaces, punctuation, symbols, and accented character. Do not rely on case sensitivity to distinguish files. Make deliberate use of delimiters.
|
||||
2. File names should be human readable: Use file names that describe what is in the file.
|
||||
3. File names should play well with default ordering: Start file names with numbers that allow them to be sorted in the order they get used.
|
||||
|
||||
Suppose you have the following files in a project folder.
|
||||
|
||||
run-first.R
|
||||
alternative model.R
|
||||
code for exploratory analysis.R
|
||||
finalreport.qmd
|
||||
FinalReport.qmd
|
||||
fig 1.png
|
||||
Figure_02.png
|
||||
model_first_try.R
|
||||
temp.txt
|
||||
|
||||
There are a variety of problems here: the files are misordered, file names contain spaces, there are two files with basically the same name but different capitalization (`finalreport` vs. `FinalReport`), and some file names don't reflect their contents (`run-first` and `temp`).
|
||||
|
||||
Below is an alternative way of naming and organizing the same set of files.
|
||||
|
||||
01-load-data.R
|
||||
02-exploratory-analysis.R
|
||||
03-model-approach-1.R
|
||||
04-model-approach-2.R
|
||||
fig-01.png
|
||||
fig-02.png
|
||||
notes-on-report-draft.txt
|
||||
report-2022-03-20.qmd
|
||||
report-2022-04-02.qmd
|
||||
|
||||
Numbering and descriptive names that are similarly formatted allow for a more useful organization of the R scripts.
|
||||
Additionally, the figures are labelled similarly, the reports are distinguished by dates included in the file names, and `temp` is renamed to `notes-on-report-draft` to better describe its contents.
|
||||
|
||||
## Running code
|
||||
|
||||
The script editor is also a great place to build up complex ggplot2 plots or long sequences of dplyr manipulations.
|
||||
The key to using the script editor effectively is to memorise one of the most important keyboard shortcuts: Cmd/Ctrl + Enter.
|
||||
The key to using the script editor effectively is to memorize one of the most important keyboard shortcuts: Cmd/Ctrl + Enter.
|
||||
This executes the current R expression in the console.
|
||||
For example, take the code below.
|
||||
If your cursor is at █, pressing Cmd/Ctrl + Enter will run the complete command that generates `not_cancelled`.
|
||||
|
@ -94,7 +131,7 @@ With your R scripts (and your data files), you can recreate the environment.
|
|||
It's much harder to recreate your R scripts from your environment!
|
||||
You'll either have to retype a lot of code from memory (making mistakes all the way) or you'll have to carefully mine your R history.
|
||||
|
||||
To foster this behaviour, I highly recommend that you instruct RStudio not to preserve your workspace between sessions:
|
||||
To foster this behavior, I highly recommend that you instruct RStudio not to preserve your workspace between sessions:
|
||||
|
||||
```{r, echo = FALSE, out.width = "75%"}
|
||||
knitr::include_graphics("screenshots/rstudio-workspace.png")
|
||||
|
@ -130,7 +167,7 @@ getwd()
|
|||
|
||||
As a beginning R user, it's OK to let your home directory, documents directory, or any other weird directory on your computer be R's working directory.
|
||||
But you're six chapters into this book, and you're no longer a rank beginner.
|
||||
Very soon now you should evolve to organising your analytical projects into directories and, when working on a project, setting R's working directory to the associated directory.
|
||||
Very soon now you should evolve to organizing your analytical projects into directories and, when working on a project, setting R's working directory to the associated directory.
|
||||
|
||||
**I do not recommend it**, but you can also set the working directory from within R:
|
||||
|
||||
|
@ -160,7 +197,7 @@ There are three chief ways in which they differ:
|
|||
|
||||
## RStudio projects
|
||||
|
||||
R experts keep all the files associated with a project together --- input data, R scripts, analytical results, figures.
|
||||
R experts keep all the files associated with a given project together --- input data, R scripts, analytical results, figures.
|
||||
This is such a wise and common practice that RStudio has built-in support for this via **projects**.
|
||||
|
||||
Let's make a project for you to use while you're working through the rest of this book.
|
||||
|
@ -216,7 +253,7 @@ In summary, RStudio projects give you a solid workflow that will serve you well
|
|||
|
||||
- Create an RStudio project for each data analysis project.
|
||||
|
||||
- Keep data files there; we'll talk about loading them into R in \[data import\].
|
||||
- Keep data files there; we'll talk about loading them into R in \@ref(data-import).
|
||||
|
||||
- Keep scripts there; edit them, run them in bits or as a whole.
|
||||
|
||||
|
@ -224,7 +261,7 @@ In summary, RStudio projects give you a solid workflow that will serve you well
|
|||
|
||||
- Only ever use relative paths, not absolute paths.
|
||||
|
||||
Everything you need is in one place, and cleanly separated from all the other projects that you are working on.
|
||||
Everything you need is in one place and cleanly separated from all the other projects that you are working on.
|
||||
|
||||
## Exercises
|
||||
|
||||
|
|
Loading…
Reference in New Issue