R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Certainly! Creating and executing an R script in RStudio is a fundamental skill for anyone looking to leverage the power of R for data analysis. This tutorial will walk you through the steps to create, edit, and run an R script in RStudio.
Open RStudio. If you haven't already installed RStudio, you can download and install it from the official website: https://rstudio.com/
File
.New File
.R Script
.This will open a new script tab in the upper-left pane where you can start typing your R commands.
Let's type a simple R command for illustration:
print("Hello, RStudio!")
Before executing the script, it's a good practice to save it:
File
-> Save
.my_script.R
.Save
.There are a few methods to execute the commands written in your R script:
Run a Single Line: Place the cursor on the line you wish to run and press Ctrl + Enter
(or Cmd + Enter
on Mac). This will run the line and display the output in the console.
Run a Chunk: Highlight multiple lines that you want to execute and press Ctrl + Enter
.
Run the Entire Script: Click on the Source
button at the top-right of the script pane. Alternatively, you can use the shortcut Ctrl + Shift + S
(or Cmd + Shift + S
on Mac).
After executing, look at the console (usually located in the lower-left pane) to see the output of your script. If you followed our example, you should see:
[1] "Hello, RStudio!"
If there's an error in your script, RStudio will display an error message in the console. Use this message to help debug your script. RStudio also provides features like breakpoints to assist in debugging.
Once you're done, you can close the R script by clicking the small x
in the script tab. Don't forget to save your changes if you made any.
RStudio offers a user-friendly interface to create, edit, and execute R scripts. The IDE provides many features that make the R programming experience smoother, from auto-completion to integrated documentation. As you become more familiar with RStudio, you'll discover many more features that will aid your data analysis journey.
Writing R code in R Studio:
# Hello World in R print("Hello, World!")