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.
0
0
R Console and script files in R Programming
Introduction
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 Console 2 + 3 print('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 <- 10 print(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 <- 8 sum <- 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.