Getting setup
Filed under: R data-science
Last updated on: July 12, 2021
- Length:
- 4 minute read, 698 words
TL;DR
Step 1: Install R
Our very first step is to download and install R from CRAN - a repository for R. Download and install the version of R that corresponds to your operating system.
You can think of R as an interpreter between you and your computer. Once R is installed, you can write instructions using the R programming language, which are then translated by R into a language that your computer will understand.
In the real world, using an interpreter is only possible if you speak a language that they understand. The same goes here. Our job as programmers is to write R programs that communicate clearly and without error so that these instructions can be translated effectively and relayed to the computer. In the next step we will install RStudio which is a tool that makes this task easy.
Step 2: Install RStudio
In theory, you could use any text editor (like Notepad or MS Word) to write R programs. But this would be like using a kitchen knife to perform a surgery. It might get the job done, but it won’t be easy or pretty.
When you install R on your system, it also installs a code editor that can be used to write R programs. While this is a more appropriate tool than a text editor it is a bit basic. What we need is a Ferrari - a feature rich code editor that is built to write and manage R programs. This is what RStudio offers.
RStudio is free and packed with features that make writing code in R a breeze. It also comes with a lot of other convenient features that make it really easy to manage R projects and publish your work.
You can download RStudio from the following link. Once you have R Studio installed, open it up to write your first few lines of code.
Step 3: Run commands in the console
Let’s print(“hello world”) as our first command.
print("Hello World!")
The right angle bracket with blinking cursor shows the place where we can enter commands. You should follow along with the next sequence of steps so that you get familiar with typing commands in R.
Let’s try out a few basic arithmetic operations. Enter the arithmetic as shown below one line at a time.
2 + 2
320 * 987
10 > 12
The pane in RStudio that we are currently using is called the console. You can think of the R console as a calculator on steroids. Similar to the calculator it allows us to enter commands and it prints the results of those below the command.
Dealing with errors
Computers, unlike humans are extremely finicky and precise about language. While humans can infer meaning from jumbld up wrds or incomplete senteces; computers will protest and complain even if a period is not where it is supposed to be. Think about them as an interpreter who will refuse to translate what you said if you did not use the correct grammar.
This means that at some point we will invariably type in something that yields an error. When that happens R will return an error message like the one shown below that indicates why there was an issue with executing a particular line of code.
Errors can be frustrating if you are new to coding. They are often cryptic and you might not be able to resolve them immediately.
But that is OK. Errors, are a part and parcel of programming. And resolving them is a key programming skill. So when you get an error, take a deep breath and remember that this is an opportunity to learn something new.
Often the best strategy to find a solution is to search for the error online to see if others have resolved it with someone else’s help. Initially even these solutions will seem like incoherent technobabble.
And right now, you might simply copy paste these solutions and move on, but, with practice and patience you will learn to troubleshoot effectively and learn from these errors. And hey, you might even come to enjoy the puzzle solving experience that an odd error brings. 😄