Steps to Run R Program in RStudio

In this guide, we will show you how to run the R programs step-by-step in RStudio IDE.

Steps to Run R Program in RStudio

1. Open RStudio: 

  • Launch the RStudio application on your computer. 

2. Start a New Script: 

  • Click on the File menu in the top-left corner. 
  • Hover over New File in the dropdown menu. 
  • Click R Script from the submenu. This will open a new script tab in the script editor pane. 

3. Write or Paste Your R Program: 

  • In the script editor pane, type out your R code or paste it from another source. 

4. Save the Script (optional, but recommended): 

  • Click on the File menu in the top-left corner. 
  • Select Save or Save As. 
  • Choose a location on your computer to save the file. 
  • Give the file a name, ensuring it has the .R extension, and click Save

5. Run the Program: 

To Run the Entire Script: Click on the Source button located at the top-right of the script editor pane (or press Ctrl + Shift + S on the keyboard). 

To Run a Specific Portion of the Script: Highlight the lines of code you want to run, and then click the Run button (located to the right of the Source button) or press Ctrl + Enter on the keyboard. 

6. View Output in the Console: 

The console pane (typically located below the script editor) will display both the commands executed and their outputs. Look here to see the results of your R program. 

7. Interactive Input: 

If your R program requires user input (e.g., using readLines()), after running the program, the console will wait for you to provide the necessary input. Type in the input and press Enter. 

8. Close RStudio: 

Once you're done, save any unsaved work. Click on the File menu and select Quit Session to close RStudio or directly close the application window.

Example

Let's walk through the steps to run a simple R program in RStudio using a basic R script that calculates the mean of a numeric vector.
# This program calculates the mean of a numeric vector

# Create a numeric vector
numbers <- c(10, 20, 30, 40, 50)

# Calculate the mean
mean_value <- mean(numbers)

# Print the mean value
cat("The mean of the numbers is:", mean_value, "\n")

Steps to Run the Above Program in RStudio

1. Open RStudio: 

Start the RStudio application. 

2. Start a New Script: 

Go to File > New File > R Script. 

3. Write the Sample R Program: 

In the script editor, paste or type out the R code provided above. 

4. Save the Script: 

Go to File > Save As. Navigate to your desired directory. Name the file something descriptive, like calculate_mean.R, and click Save. 

5. Run the Program: 

Click the Source button at the top-right of the script editor pane. 

6. View Output in the Console: 

Look in the console pane. You should see the output: 

The mean of the numbers is: 30 

7. Inspect Variables in the Environment Pane: 

On the top-right pane, under the Environment tab, you'll notice an object numbers (a numeric vector) and an object mean_value (a numeric value of 30). This allows you to quickly inspect the variables you've created in your R session. 

8. Close RStudio: 

Save any unsaved work. Go to File and choose Quit Session or simply close the application window.

Comments