3  Setup

3.1 Installation

R is a lightweight programming language that can be installed using installers available at CRAN (Comprehensive R Archive Network). https://cran.r-project.org/. Apart from this, many linux distributions have R which can be installed using the package managers within linux.  

3.2 Rstudio

To code in R, there are plenty of code editors and IDEs i.e. Integrated Development Environments that support this language. Some of the popular IDEs include Visual studio code and Jupyter, and code editors such as notepad++ or vi etc. But the most popular IDE for coding in R is undoubtedly Rstudio which has been specifically designed to work with R.

Rstudio offers all the advantages that a conventional IDE including but not limited to  syntax highlighting, code auto-completion, line numbers etc. In addition to that Rstudio has some unique features that makes it even more convenient to work with R. The user interface for Rstudio is structured into different panels (see screenshot below). One of the panels in R studio is, of course, to write the code which is the editor panel. Then there is a console panel (located below the editor panel, by default) where you can see the output of the commands in a separate sub-window within the Rstudio. In addition to these there are panels for having a quick view at the different environment variables that we have. You can switch between the editor panel and console panel using the shortcut keys ctrl+1 and ctrl+2, respectively

 

In Rstudio there are options to open different file types. To begin with, you need a file where you can type the code and execute it. This can be ideally performed with an R script file or a .r file. Once you have written some code in the R script, it is executed using the Run button in the GUI or using the keyboard shortcut Ctrl+Shift+Enter. You may also execute the selected line(s) using Ctrl+Enter; which is indeed a unique and very useful feature, particularly for new programmers. 

An R script can be executed outside of the Rstudio as well — on the terminal type R <script name>. Or you can also execute the script files within Rstudio – without explicitly opening the file – by running the command

source <script filename>.

One of the great things about Rstudio is that you can save your session which means whatever environment variables you have in the current session would be available to you whenever you restart a saved session. This makes it very convenient to start again from where you have left.

It might take some time to get used to all the features and functionality of Rstudio. Gradually, as you continue to use it you will become fluent in different aspects of this powerful IDE.

3.3 Markdown

In addition to the script file (.r), another important file type for Rstudio is R markdown file. This file allows you to not just code but also to annotate code with some plain text that is not executed. It is like having a notebook with plain text and code blocks that can be executed. What’s interesting is that these code blocks can be of other programming languages (python, julia, SQL, etc.) as well. The figure below shows that screen shot of the notebook showing text along with R code block (grey background).

R notebook

The markdown syntax allows text formatting e.g., a hash symbol at the beginning of a sentence would format it as a heading level 1 and two hashes would turn it into a heading level 2. Likewise, a text enclose within underscore is formatted as italics. To learn more about markdown syntax please follow the link given in the table at the end.

3.4 R in the cloud

You can also run Rstudio in the cloud which is now called as POSIT cloud. This cloud service gives an online Rstudio environment in which we can code just like the desktop version of Rstudio.