- 1. Objective
- 2. Introduction to R Programming
- 2. History of R language
- 3. Why should you adopt R Programming Language?
- 4. R Programming Features
- 5. Comparison Of R With Other Technologies
- 6. Working with R Scripts
- 7. R Scripting
- 8. Sourcing a Script in R
- 9. R Applications
- 10. Conclusion
In this R programming tutorial, we are going to learn what is R statistics, introduction to R, various R software editors like RGui and R Studio and their components. We will also learn R Features, Applications of R programming, how to develop R scripts with the help of examples.
2. Introduction to R Programming
R language is an open source program maintained by the R core-development team – team of volunteer developers from across the globe. R language used for performing statistical operations and is available from the R-Project website www.r-project.org. R is a command line driven program. The user enters commands at the prompt (> by default) and each command is executed one at a time.
Many routines have been written for R analytics by people all over the world and made freely available from the R project Website as packages. However, the basic installation (for Linux, Windows, or Mac) contains a powerful set of tools for most purposes.
R is a consolidated environment for performing statistical operations and generating R data analysis reports in graphical or text formats. R commands entered in the console are evaluated and executed. R cannot handle certain auto-formatting characters such as en-dashes or smart quotes; therefore, you need to be careful while copying and pasting commands into R from other applications.
2. History of R language
John Chambers and colleagues developed R at Bell Laboratories. R is an implementation of the S
programming Language and combines with lexical scoping semantics inspired by Scheme. R was named
partly after the first names of two R authors. The project conceives in 1992, with an initial version
released in 1995 and a stable beta version in 2000.
3. Why should you adopt R Programming Language?
- R programming language is best for statistical, data analysis and machine learning. By using this language we can create objects, functions, and packages. We can use it anywhere. It’s platform- independent, so it can be applied to all operating system. It’s free, so anyone can install it in any organization without purchasing a license.
- R is open source. Thus, Google is utilizing R programming as it is a suitable language. By using R
we can create any form of statistics and data manipulation. Furthermore, it can be used in
almost every field like finance, marketing, sports etc.
- R, SAS, and SPSS are three statistical languages. Of these three statistical languages, R is the only an open source. SAS is the most important private software business in the world. SPSS is now overseen by IBM. R Programming is extensible and hence, R groups are noted for its energetic contributions. Lots of Rs typical features can be written in R itself and hence, R has gotten faster over time and serves as a glue language.
4. R Programming Features
- R supports procedural programming with functions and object-oriented programming with
generic functions. Procedural programming includes procedure, records, modules, and
procedure calls. While object-oriented programming language includes class, objects, and
- Packages are part of R programming. Hence, they are useful in collecting sets of R functions into a single unit.
- Rs programming features include database input, exporting data, viewing data, variable labels, missing data, etc.
- R is an interpreted language. So we can access it through command line interpreter.
- R supports matrix arithmetic.
- It has effective data handling and storage facilities.
- It supports a large pool of operators for performing operations on arrays and matrices.
- It has facilities to print the reports for the analysis performed in the form of graphs either on-screen or on hardcopy.
You can obtain the installation files for the R program on the official R Website (www.r-project.org). The website has general documentation related to R along with the libraries of routines. The R program can be simply downloaded and installed from the R Website.
5. Comparison Of R With Other Technologies
- Data handling Capabilities – Good data handling capabilities and options for parallel computation.
- Availability / Cost – R is an open source and we can use it anywhere.
- Advancement in Tool – if you are working on latest technologies, R gets latest features.
- Ease of Learning – R has a learning curve. R is a low-level programming language. As a result, simple procedures can take long codes.
- Job Scenario – It is a better option for start-ups and companies looking for cost efficiency.
- Graphical capabilities – R is having the most advanced graphical capabilities. Hence, it provides you advanced graphical capabilities.
- Customer Service support and community – R is the biggest online growing community.
6. Working with R Scripts
R is utilized as a statistical programming environment for solving problems. R tools can also operate as a general matrix calculation toolbox.
R provides the freedom of selecting and editing tools to interact with the native console. While scripting in R, you don’t need to type commands but rather call functions to achieve results. The RConsole allows command editing.
The developed R scripts can be executed in the selected editor. You could download the data and save them in a local file, or just cut and paste the data from the browser to an editor such as Notepad, and then save them. The prominent editors available for R programming language are:
- RGui (R graphical user interface)
- Rstudio – Studio R offers a richer editing environment than RGui and makes some common tasks easier and more fun.
We will see about them in details below.
6.1. R Graphical User Interface (RGui)
Once you download R, RGui is provided as the standard graphical user interface (GUI). Most important component of RGui is the R console window. The console window in R is a place where instructions, scripts, and general R operations are performed. The console window also has R tools to manage the R environment. The R console screen appears every time the RGui is opened. It lists some basic information such as the R version installed and the licensing conditions.
In the RGui window, you can open a new script, go to the ‘File’ menu and select ‘New Script’. The RGui can be accessed using the menu shortcuts created during the installation process.
The R prompt, a ‘>’ symbol indicates the place where the user can enter commands. To quit an active R session, you need to type the following code in the console after the command prompt (>):
R asks a question to ensure that the user wishes to quit the active session.
Note the parentheses after the q; this is because in R you don’t type commands but rather call functions to achieve results, even quit.
RStudio is an integrated development environment (IDE) for R language. RStudio is a code editor and development environment, with some nice features that make code development in R easy and fun.
a) Features of RStudio
- Code highlighting that gives different colors to keywords and variables, making it easier to read
- Automatic bracket matching
- Code completion, so as to reduce the effort of typing the commands in full
- Easy access to R Help, with additional features for exploring functions and parameters of functions
- Easy exploration of variables and values. RStudio is available free of charge for Linux, Windows, and Mac devices. It can be directly accessed by clicking the RStudio icon in the menu system on the desktop.
Because RStudio is available free of charge for Linux, Windows, and Mac devices, it is a good option to use with R. To open RStudio, click the RStudio icon in the menu system or on the desktop.
b) Components of RStudio
- Source – Top left corner of the screen contains a text editor that lets the user work with source script files. Multiple lines of code can also be entered here. Users can save R script file to disk and perform other tasks on the script.
- Console – Bottom left corner is the R console window. The console in RStudio is identical to the console in RGui. All the interactive work of R programming is performed in this window.
- Workspace and History – The top right corner is the R workspace and history window. This provides an overview of the workspace, where the variables created in the session along with their values can be inspected. This is also the area where the user can see a history of the commands issued in R.
Files, Plots, Package, and Help The bottom right corner gives access to the following tools:
- Files – This is where the user can browse folders and files on a computer.
- Plots – This is where R displays the user’s plots.
- Packages – This is where the user can view a list of all the installed packages.
- Help – This is where you can browse the built-in Help system of R.
7. R Scripting
Let us start scripting in R.
Let’s create a script to print “Hello world!” in R. To create scripts in R, you need to perform the following steps:
Here in R, you will have to enclose some commands in print() to get the same output as on the command line. So you need to type below command: This takes “Hello World” as input in R.
>print (“Hello world!”)
We get the output as:
 “Hello world!”
8. Sourcing a Script in R
R provides the feature to develop scripts in external editors and then import in R for execution. This is known as sourcing a script. The individual line of code or a block or entire script can be sourced in R in RGUI or RStudio as below:
Multiple lines of code can be entered in the source editor without having each line evaluated by R. Once the scripting is completed, the entire script can be sent to R. In other words, you can source the script
The script can be sourced in either Rgui or Rstudio in the following ways:
To send an individual line of code from the editor to the console:
- In RGUI, Click the line of code and then press Ctrl+R to execute the instruction.
- In RStudio, press Ctrl+Enter or click the Run button to execute the instruction.
To send a block of highlighted code to the console:
- Select the block that needs to be executed and then presses Ctrl+R in RGui or Ctrl+Enter in RStudio.
To send the entire script to the console:
- In RGui, click anywhere in the script window, and then choose EditðRun all.
- In RStudio, click anywhere in the source editor and press Ctrl+Shift+Enter or click the Source
These keyboard shortcuts are defined only in RStudio. In the different source editor, these options may be different.
Now the entire script can be sent to the R console. To do this, click the Source button at the top right corner of the editor window or choose edit→Source. The script starts reach the point where it asks for input and then waits for the user to enter the name in the console window.
9. R Applications
- Many data analysts and research programmers use R because R is the most prevalent language.
Hence, R is used as a fundamental tool for finance.
- Many quantitative analysts use R as their programming tool. Hence, R helps in data importing
and cleaning, depending on what manner of strategy you are using on.
- R is best for data Science because it gives a broad variety of statistics. In addition, R provides the environment for statistical computing and design. Rather R considers as an alternate execution of S.
We have studied about the introduction to R programming in detail. Hence it is clear from above
information that R is more popular and better option as R supports a different kind of programming
languages. R is an Open source and has far more capabilities and availabilities to other languages.