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:
6. View Output in the Console:
7. Interactive Input:
8. Close RStudio:
Example
# 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
Post a Comment
Leave Comment