Let’s see how to run a Hello World program in R programming language from RStudio IDE, command prompt using rscript. When you are learning any programming language we basically want to see execute a simple statement that we call as Hello World program. Executing this R hello world program also helps us to check if our R install is successfully done.
In case you do not have R Install R and RStudio IDE, please have them installed before you proceed with this article. Below I have provided quick URLs to download and install.
1. Run R Hello World Program in RStudio
In order to run the hello world program in R, open
RStudio IDE from your system which brings you the below RSudio application. On the IDE, go to the console tab and type
print('R Hello World Program') on the prompt and press enter to execute the statement.
2. Hello World Program with R Dataframe
Above Hello Word program in R doesn’t exactly show the benefit of the R hence, let’s create a data frame which is the main feature of R programming language.
You can initialize an R data frame by using
data.frame() function. Dataframe in R stores the data in the form of rows and columns similar to RDBMS tables. So it is a two-dimensional data structure such that one dimension refers to the row and another dimension refers to a column.
#Create dataframe my_dataframe=data.frame( "id"=c(11,22,33,44,55), "pages"=c(32,45,33,22,56), "name"=c("spark","python","R","java","jsp"), "chapters"=c(76,86,11,15,7), "price"=c(144,553,321,567,890) ) #Display the dataframe print(my_dataframe)
Yields the output in a table.
3. Run R Program From Command Line (File or Script)
Running R programs from an RStudio would be helpful during the development where you wanted to run the statements and validate the output. But in real-time we would write the r programs in the R script file with extension .r and run it from the command line.
3.1 Create an R Program in a Notepad
Open your favorite text editor and create a
helloworld.r file with the data frame and print statements (explained in section 2). Us the following link to learn more about different ways to create a DataFrame in R.
Now open the terminal or command prompt and run the r script file using
rscript command. If you stored the file in a custom path then use the absolute path of the script to execute.
3.2 Use File from RStudio IDE
Alternatively, you can also write the R script on RStudio IDE. On RStudio, go to File -> New File -> R Script, write an R script file with the above statements, and save it with a file name.
Select the statement you wanted to run and click on the
Run option to run the selected statement. In order to run all statements from the R script, select all and click on the Run option.
4. Run R Program from Online
Lately, there are online tools pretty much for every programing language where you can run the test programs online without installing the IDE and language package on your system. These online editors are useful when you are learning programming languages like R.
Below is a list of online editors to run the R programming language online.
In this article, you have learned how to execute R hello world simple program by executing on RStudio and running from the file.
- What is Vector in R
- What is Matrix in R
- Usage of dplyr Package
- Select Columns by Index Position in R
- How to Remove Column in R?
- Add Column to DataFrame in R
- How to Add Empty Column to DataFrame in R?
- dplyr Rename() – To Change Column Name