Beginners often confuse four related concepts: scripts, console, environment, and working directory. A script is a saved text file containing commands. The console is where commands run and output appears. The environment is the collection of objects currently stored in memory.
The working directory is the folder R uses as the starting point for file paths. Understanding these concepts prevents many early frustrations.
Consider the following small script:
participant_id <- c(“P001”, “P002”, “P003”)
age_years <- c(34, 29, 41)
enrollment_data <- tibble::tibble(participant_id, age_years)
enrollment_data
The first line creates an object called participant_id. The second line creates an object called age_years. The third line combines these objects into a small tabular dataset called enrollment_data. The final line prints the dataset to the console. If this code is saved in a script, the commands remain available after the R session ends. If the code is typed only into the console, it may not be easy to reconstruct later.
The assignment operator <- is used to assign a value to an object. In the first line, the values “P001“, “P002“, and “P003” are assigned to the object participant_id. The object now exists in the environment. R object names should be clear, consistent, and meaningful.
In clinical data management, names such as raw_enrollment, clean_labs, query_list, and screening_export are much more useful than names such as x, data2, or newfile.
The working directory can be checked with:
getwd()
This command returns the current working directory. In a well-organized RStudio project, it should return the project root. Learners may be tempted to use setwd() to change the working directory manually. Although setwd() can be useful for quick experiments, relying on it in shared scripts can create problems because every person’s computer has a different folder structure. R projects and relative paths are a better foundation for reproducible work.
| Concept | Meaning | Example | Why it matters |
|---|---|---|---|
| Script | Saved file containing R commands | `03_quality_checks.R` | Preserves the workflow |
| Console | Place where commands execute | Output from `summary(data)` | Shows results and error messages |
| Environment | Objects currently in memory | `enrollment_data`, `lab_data` | Shows what R can currently use |
| Working directory | Starting folder for file paths | Project root folder | Determines where R looks for files |
Errors in R are not signs of failure. They are messages from the system that something needs attention. A common early error is:
Error: ‘data_raw/redcap_export.csv’ does not exist in current working directory.
This usually means that either the file path is wrong, the file name is different from the script, or the working directory is not the expected project folder. The data manager should respond systematically: check the project, check the folder, check the file name, and check whether the script uses the correct relative path. This habit mirrors clinical data management more broadly.
When a data issue appears, the correct response is not guesswork, but structured investigation.