Hello World in R
Your first R program - the classic Hello World example with Docker setup for statistical computing
R is a powerful language for statistical computing and graphics. Let’s start with the traditional “Hello, World!” program to verify your R environment is working correctly.
The Program
R makes printing output simple with its print() or cat() functions. For our Hello World example, we’ll use the most straightforward approach.
Create a file named hello.R:
| |
That’s it! R’s syntax is designed to be intuitive for data analysis and statistical work. The print() function outputs text to the console, automatically handling formatting.
Understanding the Code
Let’s break down what’s happening:
print(): A built-in R function that displays output to the console"Hello, World!": A character string (text) enclosed in double quotes- R automatically adds
[1]before the output, indicating this is the first element of the result
Alternative: Using cat()
R also provides the cat() function for concatenating and printing:
| |
The cat() function:
- Doesn’t add the
[1]prefix - Doesn’t automatically add a newline (that’s why we include
\n) - Is useful when you want clean output without R’s default formatting
Direct Expression
In R, you can also print by simply typing the value:
| |
When you run this interactively in the R console, it will display the string. However, in scripts, it’s better to use explicit print or cat functions.
Running with Docker
Docker provides a consistent R environment without needing to install R locally. We’ll use the official R base image.
Pull the Docker Image
First, pull the official R Docker image:
| |
This downloads the official R 4.4.2 runtime environment based on Debian, giving you a complete R installation.
Run Your Program
Execute your Hello World program:
| |
Let’s break down this Docker command:
docker run: Execute a container--rm: Automatically remove the container when it exits (keeps your system clean)-v $(pwd):/app: Mount your current directory to/appin the container-w /app: Set the working directory inside the container to/appr-base:4.4.2: The Docker image to use (R 4.4.2)Rscript hello.R: Command to run -Rscriptexecutes R scripts in batch mode
Expected Output
[1] "Hello, World!"
Notice the [1] prefix—this is R’s way of indicating vector element positions. Even a single string is treated as a vector of length 1 in R. This becomes important when working with data analysis, where you’re often dealing with vectors, matrices, and data frames containing many elements.
What’s Next?
Now that you have R running, you’re ready to explore its powerful statistical and data analysis capabilities:
- Variables and data types: Learn about R’s vector-based data structures
- Data frames: Work with tabular data, R’s primary data structure
- Functions: Discover R’s extensive library of statistical functions
- Plotting: Create visualizations with base graphics or ggplot2
- Packages: Extend R with CRAN’s 20,000+ packages
Why R?
R isn’t just another programming language—it’s a complete environment for statistical computing:
- Statistical Analysis: Built-in support for virtually every statistical method
- Data Visualization: Create publication-quality plots with ease
- Reproducible Research: R Markdown integrates code, output, and narrative
- Extensive Packages: CRAN hosts thousands of specialized packages
- Active Community: Large, helpful community of statisticians and data scientists
Whether you’re analyzing clinical trial data, building predictive models, or creating interactive dashboards, R provides the tools you need. Welcome to the world of statistical computing!