R tutorial: Hello world

This tutorial helps you get started with R. It explains what you need to write your first R program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and running your code. If you have never written a R program before this is the place to start. We have created this article, so you can get started with R, and move on to our sms examples. If you are already familiar with R, you can jump directly to one of the following SMS projects.

R sms examples:

R send sms with the HTTP rest api (code sample)
R send multiple sms with the HTTP rest api (code sample)
R schedule sms with the HTTP rest api (code sample)
R receive sms with the HTTP rest api (code sample)
R delete sms with the HTTP rest api (code sample)
Download the latest R sms api library from Github

What is R

R is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that programming languages have a more rigorous structure, to help the computer understand it better.

What is a R Hello world program

The R hello world program is the simplest program you can write. It simply prints out the sentence: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in R

To write your first program in R:

  1. Check prerequisites
  2. Setup R
  3. Open Notepad
  4. Create a file with .R extension
  5. Write Hello World program in R
  6. Run your R Hello World program
  7. Check the output
  8. Fix errors

Prerequisites

Here is what you need to get started. To write your first computer program in R, you need a Windows computer, the R language, and the example code presented below.

  • Windows 10 computer
  • R language
  • Ozeki Hello World example project

Download the R language

You might want to download the R language using this link: https://cran.rapporter.net/

Video 1 - How to download the R language (Video tutorial)

Download R

To start coding in the R language, you must first download and install it. Go to https://cran.rapporter.net/ first. Click on the link that reads "Download R for Windows" (Figure 1). This will start downloading the installer to your computer.

download r language
Figure 1 - Download R language

Install the R language

After the download procedure finished, we can continue with the installation.

Video 2 - How to install the R language (Video tutorial)

Write your first 'hello world' program in R

To write code with R language, we will use the notepad.
First of all we have to create a new file with R extension. Then open it in the Windows Notepad.

print("Hello world!")
	

Code 1 - Your first 'hello world' program in R language

Video 3 - How to write your 'hello world' program in R (Video tutorial)

Run your first 'hello world' program in the command prompt

To run our program, we will use the command prompt. To do that we have to cd into the folder which contains the code we wrote in the R language.
Then we can run our code using the following command:

/*If you named your file differently, you have to replace
the helloWorld part with the name you gave to your project */
Rscript helloWorld.R
	

Code 2 - Command to run your program in the command prompt

Video 4 - How to run your hello world program in the command prompt (Video tutorial)

Output of your code

You can run your code inside the Command Prompt. Simply enter Rscript helloWorld.R (Figure 2). The program then runs and the "Hello World!" message gets displayed inside the Command Prompt window.

Figure 2 - The output of the code

What happens if you make an error in your code

If you make an error in your code, the compiler will give you feedback about the error.
In the following video I will show you what happens if there's an error in your code.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in R and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in R?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Video 5 - How to handle a syntax error in R (Video tutorial)

Error report in R

When you make a mistake in the code it will trigger an error. Here we can see that the "Hello World" message does not get displayed. An error message appears instead, signalling to the developer that something went wrong (Figure 3). Error messages are useful, because they contain information you could use to fix the errors.

Figure 3 - Error report in R language

Summary

This document helps you to get started with the R programming language by showing you how to set up the environment and write your first Hello World program. We did our very best to make this tutorial as lucid as possible for you to see that coding in R for creating solutions is simple and beneficial. The knowledge this article offers will help you to study other R program codes and gives you the ability to learn to create programs for your system's needs.

Find more tutorials to read on the Ozeki website, you can learn more about coding in R. Move on to the document titled how to send SMS from R.

Download the Ozeki SMS Gateway now and boost your messaging system!

More information