R is the most widely used programming language in data analysis and data mining. When you first get started with R it can get a little but intimidating if you are a newbie, and sometimes even for statistics pros as the syntax can be a little bit new.
There are several ways you can access R. You can install it to your Mac, PC or Linux machine and run it from the terminal. There are also various clients you can install to assist you with the user experience.
Datazar, on the other hand, offers a cloud based client for R. Meaning you can use R right in your browser and analyze data, create charts, use packages and share your results.
Creating the Project
Once logged in to Datazar, click on the “New Project” button on top right corner and fill out the popup with your project name. When finished hit “Create Project” and you’re done! You have created your project.
Selecting an R Interface
Now you have a choice between two R interfaces: the R console and the R notebook. Both are equally useful and it really all comes down to the preferences.
Let’s go with the R console for now. Click on the “R Console” button. This will take you to your freshly created R Console. It resembles a terminal a little bit and has a text input field at the bottom.
And there you have it. This is your portal to R and it’s crazy-awesome functionality. Let’s test it out with a quick hello world message command.
> message("Hello World")
R comes with datasets already included in the core program. So let’s use the famous iris dataset and play around with it. We’ll run two commands as below:
The first command will return the entire dataset and the second command will return the first part of the dataset.
The R Console has command history support so you can use your keyboard arrows to navigate to your previous commands.
Now let’s look at graphics. The R console will return the graphics in-line with the text instead of a separate window as in the terminal. Using the console gives you a more natural feel with a little bit extra something.
Importing External Datasets
Having a very sophisticated interface is useless if you can’t use data you have collected or gathered. Let’s look at how you can use CSVs in your console.
Above the console is a button named “File.” It will show you the list of files you have in your project. Click on the checkbox that’s next to the file you want to import to your R console and click “Load Selected Files.” This ensures only the files you want in your session are loaded and keeps your workspace clean.
Let’s save the dataset to a variable called dataset:
Since this dataset is kind of long, let’s take a part of it and save it to another variable called sample:
And finally plot the sample dataset:
Importing External Functions
The method for importing external functions to your R workspace is exactly the same as the method for importing datasets. Once you’ve imported the function you want from your project, use the following function to use it:
This R file can be an R script that contains all your custom-reusable functions. Or even functions you copied from somewhere else.
Use External Libraries
If you want to juice up your R workspace with extra packages, all you have to do it run this function:
R has an infinite amount of R packages that are contributed by the community on a regular basis. Packages like ggplot2 make your R experience come to life.
Although we used the R console throughout this entire guide, here’s what it would have looked like if it was made with the R notebook interface.
R notebooks are very useful when you want to go back and edit code, especially if you’re working in a team and you want a more presentable format. R consoles on the other hand are great for quick, dirty explorations. Dirty because all your error and commands will be shown. Again, it’s all a personal preference.
Feel free to copy my file and play around with it:
Useful resources and documentation when using R:
Datazar Blog: R Language
R Tutor: Introductions