Bird
Raised Fist0
R Programmingprogramming~10 mins

R Console and script files in R Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - R Console and script files
Start R Console
↓
Type commands
↓
Commands execute immediately
↓
View output in console
↓
Write script file (.R)
↓
Save script
↓
Run script in console
↓
See script output in console
↓
Modify script and rerun
↓
End session or continue
This flow shows how you start the R console, type commands directly or run saved script files, and see output immediately.
Execution Sample
R Programming
x <- 5
print(x)
# Save this in script.R
# Run script with source('script.R')
Assign 5 to x, print x in console, save commands in a script file, then run the script to see output.
Execution Table
StepActionInput/CodeConsole OutputNotes
1Start R ConsoleR console is ready for commands
2Type commandx <- 5Assign 5 to variable x
3Type commandprint(x)[1] 5Print value of x
4Write script filescript.R with x <- 5; print(x)Save commands in a file
5Run scriptsource('script.R')[1] 5Run script, output shown in console
6Modify scriptChange x <- 10Update script file
7Run script againsource('script.R')[1] 10See updated output
8End sessionClose R console or continue
💡 User ends session or stops running commands
Variable Tracker
VariableStartAfter Step 2After Step 6Final
xundefined51010
Key Moments - 3 Insights
Why does typing commands in the console show output immediately?
Because the console executes each command as soon as you press Enter, as shown in steps 2 and 3 of the execution_table.
What is the difference between typing commands in the console and running a script file?
Typing commands runs them one by one immediately, while running a script file executes all commands saved in the file at once, as seen in steps 3 and 5.
How do changes in the script file affect the output when rerun?
Modifying the script changes variable values or commands, so when you run it again, the output updates accordingly, shown in steps 6 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 2?
A5
Bundefined
C10
D0
💡 Hint
Check the variable_tracker after Step 2 shows x = 5
At which step does the console show output '[1] 5'?
AStep 5
BStep 7
CStep 3
DStep 2
💡 Hint
Look at execution_table output column for Step 3
If you change x <- 10 in the script and rerun, what will be the output at step 7?
A[1] 5
B[1] 10
CNo output
DError
💡 Hint
See variable_tracker and execution_table steps 6 and 7 for updated value
Concept Snapshot
R Console lets you type commands and see results immediately.
Script files (.R) save commands to run later.
Use source('file.R') to run scripts in console.
Modify scripts and rerun to update output.
Console is interactive; scripts automate commands.
Full Transcript
This visual trace shows how the R console works alongside script files. You start the console and type commands like assigning a value to x and printing it. The console runs commands immediately and shows output. You can save commands in a script file with extension .R. Running the script with source('script.R') executes all commands inside and shows output in the console. Changing the script and rerunning updates the output. Variables like x change values as the script changes. This helps beginners see the difference between interactive console use and running saved scripts.

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

  1. Step 1: Understand the R Console function

    The R Console allows users to type commands and see results immediately, one command at a time.
  2. Step 2: Compare with script files

    Script files are used to save many commands to run later, not the console's main role.
  3. Final Answer:

    To type and run commands one at a time -> Option A
  4. 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

  1. Step 1: Recall R function to run scripts

    In R, the function source() is used to run commands saved in a script file.
  2. Step 2: Check other options

    Functions like run(), execute(), and load() do not run script files; load() loads saved data, not scripts.
  3. Final Answer:

    source("analysis.R") -> Option C
  4. 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

  1. Step 1: Calculate the sum of x and y

    x is assigned 5, y is assigned 3, so z = 5 + 3 = 8.
  2. Step 2: Print the value of z

    The print(z) command outputs the value stored in z, which is 8.
  3. Final Answer:

    8 -> Option D
  4. 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

  1. Step 1: Check variable definitions

    The script uses variables a and b without assigning values first, causing an error.
  2. Step 2: Verify other parts

    print() is correct, source() runs scripts, and <- is valid assignment in R.
  3. Final Answer:

    Variables a and b are not defined before use -> Option A
  4. 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

  1. Step 1: Understand script file usage

    Script files save many commands so you can run them together later.
  2. Step 2: Use source() to run scripts

    The source() function runs all commands in the script file at once.
  3. Step 3: Evaluate other options

    Typing in console is manual and slow; text files alone don't run commands; load() loads data, not scripts.
  4. Final Answer:

    Write commands in a script file and use source() to run them -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard