Welcome back. In our last lesson, you became acquainted with the RStudio interface, your new digital laboratory for data analysis. You learned to identify the four main panes—Source, Console, Environment, and Output—and configured your settings to support a reproducible workflow.
Now that you know your way around the lab, it's time to start using the equipment. This lesson focuses on the fundamental skill of giving instructions to R. You will learn how to write and execute basic commands in both the interactive Console and the permanent Script Editor. This distinction is the bedrock of creating the reliable, documented, and shareable meta-analysis scripts that are your ultimate goal.
The Two Modes of Communication with R
Think of conducting research. Sometimes you need to do a quick, "back-of-the-envelope" calculation. Other times, you need to write a detailed, step-by-step protocol that someone else can follow precisely. RStudio accommodates both of these needs through the Console and the Script Editor.
1. The Console: Your Interactive Scratchpad
The Console is your direct, immediate line of communication with R. When you type a command at the > prompt and press Enter, R executes it on the spot. This makes it perfect for quick calculations, testing a function, or inspecting a value.
Let's see this in action. The following video demonstrates how R can be used as a simple calculator directly within the Console.
R programming for ABSOLUTE beginners
This clip from the "R Programming 101" channel shows the most basic interaction with R: performing arithmetic in the Console.
Watch the short segment from this demonstration. Notice how typing 5 + 6 and pressing Enter immediately produces the result 11.
This immediate feedback is excellent for exploration. However, commands typed in the Console are not saved as part of a reproducible workflow. For that, we need the Script Editor.
2. The Script Editor: Your Digital Lab Notebook
For any analysis you intend to save, document, or share—such as a systematic review—you will write your code in the Source pane, inside a file called an R script. This script becomes your complete, reproducible research protocol.
Let's dive into the mechanics of writing and running code from a script.
This guide by Doug Hemken provides a clear, practical walkthrough of writing and executing commands in both the script editor and the console.
First, create a new script in RStudio by going to File > New File > R Script. Then, read the section Working with Scripts. Follow the instructions by typing the two lines of code from the example (x <- rnorm(25) and t.test(x)) into your new script. Focus on the process described: How to run a single line of code using Ctrl+Enter (or Cmd+Enter on a Mac) or the Run button. How running one line automatically moves the cursor to the next, allowing you to step through your analysis. How to run a block of code by highlighting it first. For now, don't worry about what the functions rnorm() or t.test() do. The key takeaway is understanding how to send instructions from your script to the Console for execution.
The workflow you just practiced—writing in the Source pane and executing into the Console—is the standard for conducting analysis in R. Your script is the permanent record, and the Console shows you the results as you go.
3. Documenting Your Work with Comments
A crucial part of any research protocol is clear documentation. In an R script, you document your code using comments. A comment is any text that follows a hash symbol (#). R ignores comments when executing code, so they are purely for human readers—including your future self!
Good commenting explains the why behind your code, not just the what. For your goal of performing a meta-analysis, this means you can document which studies you are including, why you chose a particular statistical method, or what a specific section of your code is supposed to achieve.
Chapter 17 Basics of R and Rstudio | EPIB607
This chapter from the EPIB607 course guide explains the importance of commenting and how to do it in R.
Read the section Commenting Your Code. Pay attention to how the # symbol is used to add notes within a script.
Try It Yourself: Your First R Script
Let's combine everything you've learned.
-
If you haven't already, open a new R script (
File > New File > R Script). -
Save the script by clicking the floppy disk icon or going to
File > Save. Give it a memorable name, likemy_first_script.R. -
In the script, type the following lines. This is a simple calculation you might do to find a patient's age in a study.
# This is my first script. # It calculates the age of a participant born in 1974. 2024 - 1974 -
Place your cursor on the line
2024 - 1974and press Ctrl+Enter (or Cmd+Enter). You should see the command and its result,[1] 50, appear in the Console. -
Now, go directly to the Console pane. Type a different calculation, like
365 / 7, and press Enter. -
Notice the difference: the calculation from your script is saved and can be run again at any time, while the one you typed in the Console is part of the temporary session history.
This simple exercise demonstrates the fundamental workflow you will use for all your future analyses.
Conclusion
In this lesson, you have taken your first active steps in programming with R. You have learned the critical distinction between writing code in the Console for immediate interaction and in the Script Editor for permanent, reproducible analysis.
Here are the key takeaways:
- The Console: Use it for quick, exploratory commands. Instructions are executed immediately by pressing Enter.
- The Script Editor: Use it to write your full analysis "protocol." This is your digital lab notebook and the key to reproducible research.
- Execution: The Ctrl+Enter (or Cmd+Enter) shortcut is the primary way to send commands from your script to the Console.
- Comments (
#): Use comments liberally in your scripts to document your thought process and make your analysis understandable to others and your future self.
You now know how to give R instructions. In our next lesson, we will explore how to make R remember information by assigning values to variables, a foundational concept for handling the data from your systematic reviews.