Hello World in R

Published on 01 November 2018 (Updated: 15 October 2020)

Hello World in R

In this article, we’re covering how to implement Hello World in R, a scientific programming language.

How to Implement the Solution

Fortunately, this task can be solved in a concise one-liner:

cat("Hello, World!")

You can see the string Hello, World! as the argument of the function cat. This function does all the work. If you are familiar with Bash, you may already know the cat tool which prints the content of one or several files.

Analogously, you can pass one or several strings to the cat function which prints the input to the standard output. The function allows an optional argument sep that represents the separator to use when you pass multiple strings. As a consequence, the following is an equivalent alternative to the solution above:

cat("Hello", "World!", sep=", ")

In this call, Hello and World! are glued together by placing the specified separator “, ” between the two strings, resulting in the desired output Hello, World!.

Now, we know what we need to produce the output. The next section explains how we can also see the output of our program.

How to Run the Solution

In order to run the solution we need an R compiler first. Furthermore, we need a copy of Hello World in R. From within the directory in which we saved the copy, we run the following command on the command line:

Rscript hello-world.R        # Linux/Unix
R.exe hello-world.R          # Windows

Alternatively, you can try an online compiler if you want to save the time required for installing the R environment locally.

Further Reading