Category Archives: R language

R is a language and environment for statistical computation of data. You will find explanation and content for this chapter in this category.

Understanding the Visual Studio IDE for R language

Introduction and Background

Previously we had talked much about installing and setting up the environment for R language. In this post, I will walk you through different stuff that Visual Studio provides you with. Typically, every R IDE supports the following tools and features for data processing.

  1. A scripting toolkit; where you can write R language code.
  2. An R interactive shell for executing the commands.
  3. Plot builder for plotting the graphics.
  4. Markdown editors and much more.

I will walk you through these windows in Visual Studio IDE itself and would give examples of using Visual Studio as your default IDE for R programming. In my personal (and humble) opinion, I think Visual Studio is the best IDE out there, provided its simplicity, ease of use and now that the price has also dropped down to zero for personal use! What more can someone ask for? 🙂

Exploring Visual Studio for R

I have talked about the things that we are going to explore in this post. Those are listed above, and in this section I am going to expand them and I am going to demonstrate what benefit they have for R developers in the industry of data science.

visual-studio-2013-logo
Figure 1: Visual Studio logo.

Besides R, Visual Studio is also going to have the tools for Java programming. At least, I am very much excited about Visual Studio’s future, in which it is going to be used as a universal IDE. But in this post we should just focus on the R tools provided. If you have not yet set up the R tools for Visual Studio, I recommend that you read the post of mine for that, Installing and setting up R environment on Windows. This would help you to set up the environment for data analysis using R language.

1. R code editor

First thing to know about in any IDE for any programming language, is the code editor that it has. Code editors are simply the text editors, but there are a few other additional features that it has apart from the ordinary text editing and caching services. Visual Studio code editor provides the following powerful features:

  1. R syntax highlighting.
    • Every IDE should support syntax highlighting and highlight the different data types, different keywords and the scope resolutions like matching brackets.
  2. Autocomplete suggestions.
    • Visual Studio calls them IntelliSense, which provides the easy way of writing the programs. It provides the clean way of providing suggestions for function calls and the parameter lists.
  3. Squiggly lines for errors
    • It is better to show off the errors in the language syntax or commands, before programmers build their project and finally stumble upon the errors. Visual Studio shows the errors if there are any, in the commands using the interpreters!

Screenshot (3147)
Figure 1: Visual Studio code editor.

This is the most basic usage of the code editor at now.  Visual Studio renders the codes in their own format, R syntax has been implemented in Visual Studio to support for string literals, keywords, comments and matching brackets.

You should consider using the R code editor instead of using a plain-text file to write the R scripts.

2. R interactive shell

Every interpreted programming language has an interactive shell. Python has it, so does R because R is an interpreted language. Interactive shells allow you to execute minimal programs, commands, and test the environment itself. They are also used to compile (interpret!) and execute the statements in the source file that you may have somewhere.

Visual Studio supports the Interactive Shell. If you do not find it, it is right there under R Tools → Windows → R Interactive. The WIndow allows you to enter the commands to:

  1. Create new variables in the environment.
  2. Explore the environment.
  3. Print the variables.
  4. Perform calculations on the data.

Screenshot (3232)
Figure 2: R interactive shell.

You can see that the R project starts, and then it allows the users to enter the commands that are to be tested or executed in the R environment. If you pay a bit more attention, you will see that i am currently running Microsoft’s R Open,

Screenshot (3232) - Copy
Figure 3: Microsoft R Open being used in the shell.

This allows you to add the commands which require execution. You can perform many functions in this shell as already mentioned above.

Screenshot (3235)
Figure 4: Executing commands in R Interactive Shell.

You can also run other versions of the R. But, Microsoft R Open is the Microsoft’s implementation and distribution of R language. To change the currently active engine to be used for R, you need to have other versions of R installed on your machine. If they are available, then you can update the current active R engine from the settings themselves. Just select R Tools → Options. Inside those options, select the path for the R engine’s installation folder.

Screenshot (3233)
Figure 5: Options in R Tools.

Select this field and update the directory which it targets to target another directory for a different R engine. Once done, you can restart the Visual Studio to take the actions.

3. Plotting graphs

Another great feature of R programming is that it provides you with native support for graphical presentations. If you want to build charts, pie charts and bar charts or graphs. You can get the functions that can do the trick for you! In no time, you will be up and running your own projects, presentations and anything.

Benefit is that R generates quality and enterprise ready graphics, not just careless graphics! You can show the R plot window by either executing the command for plotting the graph or by manually opening it. To select the R plot, you can go to R Tools → Windows → R Plot. You can then direct R to build and generate the graphics on the plotter, a few of the commands used are:

  1. Pie
  2. Plot
  3. Barplot

You can use these commands to build the graphics, based on the data sets or vectors that you have. You can consult any of the statistical book or guide to learn more about charts and how they are used to render the data on the graphical level.

Just to demonstrate, I am going to use the following data, assuming they are marks:

> a <- c(55, 73, 44, 85, 65, 76)

Execute this, and R would create a variable in the environment.

Using plot()

First function to use is the plot() function. It plots the dotted graph of the points. Execute the following function,

> plot(a)

Following is the result of this command in the shell:

Screenshot (3236)
Figure 6: Plotting the dotted graph.

You can change how text is rendered and how the indices are selected, too!

Using barplot()

Next I would like to talk about the barplot() function that generates the graphics in the bar graph form. You pass the elements, and then it would generate the output in the bars form. The benefit is that you can compare how long a bar is, as compared to another one.

To get a bar graph, execute the following command:

> barplot(a)

The following output would be captured by you:

Screenshot (3237)
Figure 7: Bar graph for the data set.

This shows the data in a bar graph form. This is more readable and is easily understood. You can see how our graph is built and how the indices are selected for our data sets. You can then perform other actions, which I will talk about later before ending this section.

Using pie()

Pie is another kind of graphical notation in which we can render the data, to learn about it, read any basic statistical guide or course book. The pie uses the shape of a pie to render the graph. But, it uses the ratio of each data item to slice the pie into different sections to demonstrate their part in the pie.

To build a pie chart, use the following command:

> pie(a)

This would result in the following:

Screenshot (3238)
Figure 8: Pie chart graph.

Although this uses 1, 2, 3…6 to label. You can change them later in the function call. But I just want to tell you how Visual Studio can help you to have multiple windows working simultaneously for your project.

Features of R Plot

I mentioned that I will talk about the features of the R Plot. One of the best feature is that they are native. You do not need to import any extra package or library. But you can import other libraries, if you like any other implementation of the graphs.

Secondly, R Plot allows you to import the graphics in the form of:

  1. PNG images
  2. PDF documents

Which makes it even more easier to share the graphics that you render using the data set of your own environment.

Generating documents — R Markdown

Finally, I would like to talk about the R Markdown a bit. I think, this is one of the best things that R has right now. If you have ever been to Stack Overflow, GitHub or other similar websites, you would know that they already support a markdown that can be used to generate and render the HTML in a much simpler way. For example,

Hello _everyone_! I am using the following code

```
var obj = new Object();
```

// Would result in
<p>Hello <i>everyone</i>! I am using the following code</p>

<pre>
var obj = new Object();
</pre>

Which makes it much easier to render the documents in a much simpler way. Instead of writing a complex, standard or platform based language. You write a third-party standard, which is then converted to another standard. R Markdown does the same thing. You use it to generate the documents. The benefit is that you can use a single R markdown document to build multiple documents of multiple types and kinds:

  1. PDF documents
  2. HTML documents
  3. Word documents
  4. Other documents, supported!

Thus, it seriously elevates the document generation for the research that you have just conducted. Plus, it allows embedding the document code which can be executed when needed, such as generating the charts, calculating the means and so on and so forth.

In Visual Studio, you can create a new R Markdown document just the way you would create any other file in Visual Studio. Right click → Add → New Item → R Markdown.

This document can contain the plain-markdown content, which would be then rendered and compiled based on the medium that you want to render the data in.

References

To learn more about R Markdown, please refer to the following links:

  1. R Markdown
  2. Knitr with R Markdown

Points of Interest

In this post I have talked about and walked you around many aspects of using R programming in Visual Studio IDE. Visual Studio is, no doubt, the best programming IDE available and providing the services for R programming is a great step by Microsoft.

RTVS
Figure 9: Visual Studio IDE having 4 windows of R tools open.

I assume, using Visual Studio for R programming is pretty much explained in this post, and in the later posts I will walk you through using R language for real projects and real time data processing and analysis.

In the later posts, I will talk about using R programming in real world projects. Hold tight, it’s going to be an excellent data science journey. 🙂

Advertisements

Installing and setting up R environment on Windows

Introduction and Background

One day in the previous week, I heard my professor say something about “R language” and I said to myself, “What is this R language?”. Of course I had no idea about what that language was but I was a bit curious to know about it and to learn about it. Luckily, I was provided with a few concepts about R language on C# Corner’s R language category and later I Googled stuff around to learn more about it. In this post, I am going to walk you through different things that I have learnt during my learning time.

In this post, I will walk you through many steps to set up the environment for R programming. I will be covering the basic R environment set up and later I will talk about using Visual Studio as the default IDE for R programming, after all, “Who doesn’t like Visual Studio?!”.

What is R, anyways?

A good introduction to the R language and R environment is given by Mahesh on C# Corner, I would recommend that you read that one, What Is R Language.

R is a programming language, as well as an environment for computation. What sort of computation? Statistical computation is the major field for R. However, many fields are using R language for their purposes, for example, Data Scientists are using R language for many analysis that they have to perform on data sets. Data mining etc. come later in the picture.

I will point out a few of the major fields that R can be used in, later, but first a bit of the intro to R language would be useful.

R-Programming-Language-Logo-785x595
Figure 1: R logo.

R language was developed in the early 1990s for statistical computation purposes. It started all with small modules, then it gathered and gained much reputation from online communities and it became a very popular environment to use in many fields of computer-related sciences. It was developed using C and FORTRAN as the basic language for interpretation.

R is distributed under GNU license and allows modifications to be made and shared online. R was developed in C and FORTRAN. That is why, you can modify the source code and recompile the binaries to make it suitable for your own needs. You can do that, too!

Why learn R?

As already mentioned that R is a programming language and environment for statistical programming and computation. That is why, R is a very prominent language to be used in data science projects. Specifically, the R language is used instead of C, Java or even C++ in these cases because the packages in R are already ready to be used and in no time, you get to use the best of the data science in this environment.

Untitled-1
Figure 2: A chart for benefits of learning R language. 

Even if you are not an open source developer, you would still find R language very much helpful. The usage of R ranges from field to field. As already depicted in the diagram above, this project can be used in many ways, in many fields and for many purposes. A few of the basics where, this can be used can be categorized as following:

  1. Working with data science projects.
  2. Building reports for data analysis.
  3. Creating presentations with graphical representations of results.
  4. Adding your own extensions to the projects.
  5. Educational use.

I will write some articles about using R in many ways, for many projects and tasks in computer science, but I would like to leave that all to a later time. Until now, just know that if you are going to analyze large amount of data sets, process data mining and similar projects. Then your best bid would be R language instead of C++ or Java.

Another beauty of R language is that it can be coupled with many other famous programming languages and environments and frameworks. For example, R functions can be called from C, C++, Java, Python and from the .NET environment, easily. You would be using the R language to:

  1. Process the data from CSV or TXT files.
  2. Generate summary reports in the form of graphics or tables.
  3. Use a special type of Markdown format to build the results.
  4. Use multiple formats to extract the data results.

There is much more to learn R for. Many giants such as Google and Facebook are already using R for their computation purposes, because it is open source and can be updated as per needs. It allows educational institutes to teach their students in a much simpler and easy way.

Setting up R environment

First step to perform would be to install the R environment packages on your machine. You can get the binaries for installation packages free of cost, from R website itself. Go to the CRAN network and select your own operating system and download the binaries to be used for installation purposes on your own machine.

I am using Windows operating system, so I used Windows instead. You can download the binaries for your own system. Start the setup and continue to follow the steps that are provided on the UI itself.

Screenshot (2752)
Figure 3: R installation setup for Windows.

Later you would be asked to select the location where you want to install the packages. Chose a location, but recommendation is to leave it as it is.

Screenshot (2753)
Figure 4: Location selection process.

Then the environment would be installed. You can run the environment programs by selecting either of the following icons.

Screenshot (2754)

Select the one that suits your architecture, mine was 64 bit so I executed the one for my architecture.

Screenshot (2755)
Figure 5: Running the R console.

Just to test that everything is running and set up fine, execute the following test on it.

> 3 + 3
[1] 6

Screenshot (2756)
FIgure 6: Running a test in R console.

This shows the R is set up and we can continue to use it in our projects for our own purposes. Now, you don’t want to work on your projects using this console only. For that, you would require a full-featured Integrated development environment; IDE. In the next steps, I will walk you through many steps to install the IDE for R.

Setting up R using Visual Studio

Visual Studio is, no doubt, the best IDE available for everything. It already supports C, C++, C#, VB.NET, Python and many more languages and frameworks such as web development languages of HTML, CSS and JavaScript.

R language support has also just arrived with the “R Tools for Visual Studio” extension that you can install to get started with using R language in Visual Studio. But for this to run, you need to have Visual Studio 2015 with Update 1, any edition would work from Community to Enterprise.

To set up, you can go to the URL provided above. Download the tools, you can also install the Microsoft R Open manually. Microsoft R Open is what we are going to use from now on. Go to this website for Revolution Analytics, and start setting up MRAN (Microsoft’s distribution of CRAN)

  1. Download Microsoft R Open and install it.
  2. Download MKL Math Library, which is going to enhance the performance of MRAN.

Screenshot (2811)
Figure 7: Microsoft R Open installer.

After that, install the MKL and you’re done!

Screenshot (2812)
Figure 8: MKL installer.

After that, just run the Visual Studio IDE to get started with your first R program. That, I am going to cover in an upcoming post.

Points of Interest

In this post, I have talked about a few of the stages that you can undergo to set up your R environment. Before that, I talked about why and how to use R. Apart from that, I also shared a few of the things required to set up R environment on Windows and to use Visual Studio as the default IDE for programming with R language.

In the later posts, I will write about using R language for your computation that require a lot of data processing. See you in the next posts. 🙂