0
0
R Programmingprogramming~5 mins

Running R code in R Programming

Choose your learning style9 modes available
Introduction

Running R code lets you tell the computer to do tasks like math, data analysis, or making graphs.

You want to calculate the average of some numbers.
You need to create a simple graph to see your data.
You want to try out a small piece of R code to check how it works.
You want to run a script that processes data automatically.
You want to test a function you wrote in R.
Syntax
R Programming
# To run R code, just type the commands and press Enter
# Example:
print("Hello, R!")

# Or run a script file from the console:
source("script.R")

You can run R code line by line in the R console or RStudio.

Use source() to run a whole script file at once.

Examples
This prints a message to the screen.
R Programming
print("Hello, world!")
This assigns 5 to x, then adds 3 and shows 8.
R Programming
x <- 5
x + 3
This runs all the code inside the file named my_script.R.
R Programming
source("my_script.R")
Sample Program

This program prints a welcome message, then calculates double of 10 and prints the result.

R Programming
# Simple R program to print a greeting and do math
print("Welcome to R!")
number <- 10
result <- number * 2
print(paste("Double of", number, "is", result))
OutputSuccess
Important Notes

In R, commands run immediately when you press Enter in the console.

Use comments starting with # to explain your code.

Running scripts helps you repeat tasks without typing again.

Summary

Running R code means typing commands or running script files.

You can run code line by line or run whole scripts with source().

Running code lets you do calculations, print messages, and analyze data.