The R Console lets you type and run commands one by one to see results quickly. Script files let you save many commands in a file to run them all at once later.
You want to try a quick calculation or check a function result immediately.
You are exploring data step-by-step and want to see outputs as you go.
You want to save your work so you can run the same commands again later.
You need to share your code with someone else or keep it organized.
You want to run a set of commands automatically without typing each time.
Syntax
R Programming
# In R Console, just type commands and press Enter# In script files, write commands line by line# Then run the whole script using source('filename.R') or Run button
The R Console is interactive and shows results immediately.
Script files usually have the extension .R and can be edited in any text editor or RStudio.
Examples
Type these commands one by one in the console to see results immediately.
R Programming
# Example in R Console2 + 3print('Hello')
This script saves commands to add two numbers and print the result.
R Programming
# Example script file content (my_script.R)
x <- 5
y <- 10print(x + y)
This command runs all commands saved in the script file my_script.R.
R Programming
source('my_script.R')
Sample Program
This script creates two numbers, adds them, and prints the result with a message.
R Programming
# Save this as example_script.R# This script calculates and prints the sum of two numbers
num1 <- 7
num2 <- 8sum <- num1 + num2
print(paste('Sum is', sum))
OutputSuccess
Important Notes
Use the R Console for quick tests and learning.
Use script files to keep your work safe and repeatable.
In RStudio, you can run scripts easily with the Run button or Ctrl+Enter.
Summary
The R Console is for typing and running commands one at a time.
Script files let you save many commands and run them all together.
Using both helps you learn and organize your R work better.
Practice
(1/5)
1. What is the main purpose of the R Console?
easy
A. To type and run commands one at a time
B. To save many commands in a file
C. To create graphical plots only
D. To install R packages automatically
Solution
Step 1: Understand the R Console function
The R Console allows users to type commands and see results immediately, one command at a time.
Step 2: Compare with script files
Script files are used to save many commands to run later, not the console's main role.
Final Answer:
To type and run commands one at a time -> Option A
Quick Check:
R Console = run commands one by one [OK]
Hint: Console runs commands instantly, scripts save commands [OK]
Common Mistakes:
Confusing console with script file
Thinking console saves commands permanently
Assuming console only creates plots
2. Which of the following is the correct way to run a script file named analysis.R in R?
easy
A. execute("analysis.R")
B. run("analysis.R")
C. source("analysis.R")
D. load("analysis.R")
Solution
Step 1: Recall R function to run scripts
In R, the function source() is used to run commands saved in a script file.
Step 2: Check other options
Functions like run(), execute(), and load() do not run script files; load() loads saved data, not scripts.
Final Answer:
source("analysis.R") -> Option C
Quick Check:
Run script = source() [OK]
Hint: Use source() to run saved R scripts [OK]
Common Mistakes:
Using load() to run scripts
Trying run() or execute() which don't exist
Forgetting to use quotes around filename
3. What will be the output when you run the following commands in the R Console?
x <- 5
y <- 3
z <- x + y
print(z)
medium
A. NULL
B. 53
C. Error: object 'z' not found
D. 8
Solution
Step 1: Calculate the sum of x and y
x is assigned 5, y is assigned 3, so z = 5 + 3 = 8.
Step 2: Print the value of z
The print(z) command outputs the value stored in z, which is 8.
Final Answer:
8 -> Option D
Quick Check:
5 + 3 = 8 [OK]
Hint: Add variables then print result [OK]
Common Mistakes:
Concatenating numbers as strings (53)
Forgetting to assign z before print
Expecting NULL output
4. You wrote this script file calc.R:
result <- a + b
print(result)
When you run source("calc.R") in the console, you get an error. What is the problem?
medium
A. Variables a and b are not defined before use
B. The print() function is misspelled
C. source() cannot run script files
D. The assignment operator <- is incorrect
Solution
Step 1: Check variable definitions
The script uses variables a and b without assigning values first, causing an error.
Step 2: Verify other parts
print() is correct, source() runs scripts, and <- is valid assignment in R.
Final Answer:
Variables a and b are not defined before use -> Option A
Quick Check:
Undefined variables cause errors [OK]
Hint: Define variables before using them in scripts [OK]
Common Mistakes:
Assuming print() is wrong
Thinking source() can't run scripts
Confusing assignment operator syntax
5. You want to organize your R work by saving commands in a script file and running them all at once. Which approach is best?
hard
A. Use load() to run commands saved in a script
B. Write commands in a script file and use source() to run them
C. Save commands in a text file and open it in a text editor only
D. Type all commands directly in the console one by one
Solution
Step 1: Understand script file usage
Script files save many commands so you can run them together later.
Step 2: Use source() to run scripts
The source() function runs all commands in the script file at once.
Step 3: Evaluate other options
Typing in console is manual and slow; text files alone don't run commands; load() loads data, not scripts.
Final Answer:
Write commands in a script file and use source() to run them -> Option B
Quick Check:
Scripts + source() = organized runs [OK]
Hint: Save commands in script, run with source() [OK]
Common Mistakes:
Running scripts by typing manually
Using load() instead of source()
Not saving commands in R script format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.