0
0
R Programmingprogramming~10 mins

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

Choose your learning style9 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.