For the complete documentation index, see llms.txt. This page is also available as Markdown.

19.5 R

R is a programming language and free software environment for statistical computing and graphics visualization, released in 1993 by Ross Ihaka and Robert Gentleman of the Department of Statistics at the University of Auckland, and currently maintained by the R Core Team.

Its name is both a combination of the first letters of its two authors' names and a tribute to the S language from Bell Labs: R comes from S and is an open-source (GPL) derivative of the S programming language; most code written in S can run in R without modification.

R is widely used in statistics, data science, bioinformatics, econometrics, and social sciences. Its ecosystem includes tens of thousands of extension packages, which are published through the Comprehensive R Archive Network (CRAN).

FreeBSD provides R through both Ports and pkg. In Ports, the R language package is located under the math/R category.

Installing R

Install using pkg:

# pkg install R

Or install using Ports:

# cd /usr/ports/math/R/
# make install clean

After installation, verify the version with the following command:

$ R --version
R version 4.6.0 (2026-04-24) -- "Because it was There"
Copyright (C) 2026 The R Foundation for Statistical Computing
Platform: amd64-portbld-freebsd16.0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

Enter an interactive session:

In the interactive session, use q() to exit.

CRAN Mirrors and Package Management

CRAN is the official distribution network for R extension packages, with dozens of mirror sites worldwide. When using install.packages() for the first time, R prompts the user to select a mirror. You can explicitly specify a mirror in the R session as follows (using the Tsinghua University TUNA mirror as an example):

Tip

Since building R packages is not easy (similar to Python), FreeBSD Port developers maintain a large number of R packages, most of which are named R-cran-*. Taking ggplot2 as an example, it is graphics/R-cran-ggplot2 in Ports.

You can also create an ~/.Rprofile configuration file in the user's home directory to make the mirror settings take effect automatically every time R starts:

Common package management commands include:

Basic Usage Examples

The following examples demonstrate R's basic arithmetic operations, vector operations, and simple plotting capabilities. Start R and execute them in sequence. Please install the Port graphics/R-cran-ggplot2 first.

Basic arithmetic operations:

Vector operations:

Simple linear regression

Plotting a scatter plot with regression line

This will output the following figure:

Scatter plot with regression line

RStudio IDE

RStudio is one of the most popular integrated development environments (IDE) for R. It provides syntax highlighting, code completion, a plot viewer, a data browser, and version control features.

Install the RStudio desktop client using pkg (the server version is RStudio-server):

Or install using Ports:

After installation, launch RStudio from the application menu in the desktop environment, or run in the terminal:

Data Import and Export

R supports reading and writing multiple data formats. Common examples are as follows:

Statistical Modeling and Graphics Visualization

The following examples demonstrate common usage of linear models and ggplot2 plotting:

This will output the following figure:

Scatter plot with fitted curve
Category
Package/Port
Purpose

Data processing

dplyr (databases/R-cran-dtplyr), tidyr (devel/R-cran-tidyr), data.table (devel/R-cran-data.table)

Data cleaning, filtering, reshaping, and summarization

Graphics visualization

ggplot2 (graphics/R-cran-ggplot2)

Static and interactive plotting

Time series

tseries (finance/R-cran-tseries)

Time series modeling and forecasting

Machine learning

caret (devel/R-cran-caret)

Classification and regression model training

Parallel computing

future (devel/R-cran-future), doParallel (devel/R-cran-doParallel)

Multi-core and distributed computing

Interoperability with Other Languages

R can interoperate with multiple programming languages. Common scenarios are as follows:

Calling C/C++ Code

The Rcpp package (Port devel/R-cran-Rcpp) allows embedding C++ code directly in R:

Calling Python Code from R

The reticulate package allows calling Python from within an R session:

Calling R from Python

The rpy2 package (math/py-rpy2) for Python allows calling R from Python:

Press Ctrl + D to exit the above interface.

Command-Line Batch Processing Mode

R supports running scripts in batch mode. Create a script analysis.R with the following content:

Execute in the terminal:

This mode is suitable for integrating R into shell scripts, cron jobs, or batch processing workflows.

Troubleshooting

Extension Package Compilation Failure

Some extension packages require C/C++/Fortran compilers. Ensure that devel/gmake, lang/gcc (including gfortran), and related dependencies are installed. You can install the common compilation toolchain at once using the following command:

Chinese Character Garbled Display

First, properly set the LANG, LC_ALL, and other locale environment variables as described in other chapters. In R, you can additionally set:

Graphics Device Fails to Start

In a server environment without a graphical interface, you need to output plots to file devices (png, pdf, svg, etc.):

View the image:

Sample image

References

  • R CORE TEAM. R: The R Project for Statistical Computing[EB/OL]. [2026-06-09]. https://www.r-project.org/. R official website

  • COMPREHENSIVE R ARCHIVE NETWORK. CRAN mirrors[EB/OL]. [2026-06-09]. https://cran.r-project.org/mirrors.html. CRAN mirror list

  • R CORE TEAM. An Introduction to R[EB/OL]. [2026-06-09]. https://cran.r-project.org/manuals.html. An Introduction to R.

  • Wickham H, Çetinkaya-Rundel M, Grolemund G. R for Data Science: Import, Tidy, Transform, Visualize, and Model Data[M]. 2nd ed. Sebastopol: O'Reilly Media, 2023. ISBN: 978-1-492-09740-2.

Last updated