{{Short description|Programming language for statistics}} {{redirect-distinguish|R lang|Erlang (disambiguation){{!}}Erlang|Rust (programming language)}} {{Use dmy dates|date=June 2018}} {{Infobox programming language | name = R | logo = R logo.svg | logo_size = 150px | screenshot = R terminal.jpg | screenshot_size = 250px | screenshot caption = Terminal window for R | released = {{Start date and age|1993|08}} | designer = Ross Ihaka and Robert Gentleman | developer = R Core Team | typing = Dynamic | influenced = {{startflatlist}} * Julia<ref name="Introduction">{{cite web |url=https://docs.julialang.org/en/stable/manual/introduction/#man-introduction-1 |title=Introduction |work=The Julia Manual |access-date=2018-08-05 |archive-url=https://web.archive.org/web/20180620172516/https://docs.julialang.org/en/stable/manual/introduction/#man-introduction-1 |archive-date=20 June 2018 |url-status=dead |df=dmy-all}}</ref> * pandas<ref name="pandas">{{cite web |url=https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_r.html |title=Comparison with R|access-date=2024-07-15 |work=pandas Getting started}}</ref> {{endflatlist}} | platform = arm64 and x86-64 | license = GPL-2.0-or-later<ref name="gnugpl" /> | website = {{URL|https://www.r-project.org/|r-project.org}} | wikibooks = R Programming | paradigms = Multi-paradigm: procedural, object-oriented, functional, reflective, imperative, array<ref name="Morandat"/> | latest release version = {{wikidata|property|edit|reference|P348|P548=Q2804309}} | latest release date = {{start date and age|df=yes|{{wikidata|qualifier|P348|P548=Q2804309|P577}}}} | latest preview version = {{wikidata|property|edit|reference|P348|P548=Q51930650}} | latest preview date = {{start date and age|df=yes|{{wikidata|qualifier|P348|P548=Q51930650|P577}}}} | influenced_by = {{startflatlist}} * Lisp * S<ref name="R FAQ"/> * Scheme<ref name="Morandat" /> {{endflatlist}} | file_ext = {{Unbulleted list|.R<ref>{{Cite web |title=R scripts |url=http://mercury.webster.edu/aleshunas/R_learning_infrastructure/R%20scripts.html|access-date=2021-07-17 |website=mercury.webster.edu}}</ref>|.r|.rdata|.rhistory|.rds|.rda<ref>{{Cite web |date=2017-06-09 |title=R Data Format Family (.rdata, .rda) |url=https://www.loc.gov/preservation/digital/formats/fdd/fdd000470.shtml|access-date=2021-07-17 |website=Loc.gov}}</ref>}} }}

'''R''' is a programming language for statistical computing and data visualization. It has been widely adopted in the fields of data mining, bioinformatics, data analysis, and data science.<ref>{{Cite journal |last1=Giorgi |first1=Federico M. |last2=Ceraolo |first2=Carmine |last3=Mercatelli |first3=Daniele |date=2022-04-27 |title=The R Language: An Engine for Bioinformatics and Data Science |journal=Life |language=en |volume=12 |issue=5 |page=648 |doi=10.3390/life12050648 |pmid=35629316 |pmc=9148156 |bibcode=2022Life...12..648G |doi-access=free}}</ref>

The core R language is extended by a large number of software packages, which contain reusable code, documentation, and sample data. Some of the most popular R packages are in the tidyverse collection, which enhances functionality for visualizing, transforming, and modelling data, as well as improves the ease of programming (according to the authors and users).<ref>{{Cite web |title=Home - RDocumentation |url=https://www.rdocumentation.org/ |access-date=2025-06-13 |website=www.rdocumentation.org}}</ref>

R is free and open-source software distributed under the GNU General Public License.<ref name="gnugpl">{{Cite web |title=R - Free Software Directory |url=https://directory.fsf.org/wiki/R#tab=Details |access-date=2024-01-26 |website=directory.fsf.org}}</ref><ref>{{Cite web |title=R: What is R? |url=https://www.r-project.org/about.html |access-date=2025-05-10 |website=www.r-project.org}}</ref> The language is implemented primarily in C, Fortran, and R itself. Precompiled executables are available for the major operating systems (including Linux, MacOS, and Microsoft Windows).

Its core is an interpreted language with a native command line interface. In addition, multiple third-party applications are available as graphical user interfaces; such applications include RStudio (an integrated development environment), Jupyter (a notebook interface), as well as Termux and Google Colab for mobile devices.<ref>{{bulleted list|{{Cite web |last=Conor I. |first=Anderson |date=2018-07-08 |title=Installing R on Android via Termux |url=https://conr.ca/post/installing-r-on-android-via-termux/ |access-date=2025-12-12 |website=Conor I. Anderson |language=en-ca}}|{{Cite journal |last=Quintana Denis |first=José Armando |date=2025 |title=Análisis de datos en R utilizando Google Colab |url=https://ebuah.uah.es/dspace/handle/10017/67257 |journal=Escuela Politécnica Superior |publisher=Universidad de Alcalá}}|{{Cite journal |last=Hewitt |first=Christopher |date=2024-04-01 |title=Exploring Canadian Geospatial Data with GeoPandas in Colab Notebooks |url=https://openjournals.uwaterloo.ca/index.php/acmla/article/view/5671 |journal=Bulletin - Association of Canadian Map Libraries and Archives (ACMLA) |language=en |issue=173 |pages=14–17 |doi=10.15353/acmla.n173.5671 |issn=2561-2263|doi-access=free}}|{{Cite journal |last1=Poolman |first1=Toryn M. |last2=Townsend-Nicholson |first2=Andrea |last3=Cain |first3=Amanda |date=2022 |title=Teaching genomics to life science undergraduates using cloud computing platforms with open datasets |journal=Biochemistry and Molecular Biology Education |language=en |volume=50 |issue=5 |pages=446–449 |doi=10.1002/bmb.21646 |issn=1539-3429 |pmc=9804627 |pmid=35972192}}|{{Cite journal |last=Nurdin |first=Naufal Muharam |title=Development of blood hemoglobin level early detection device based on a noninvasive optical platform |url=https://www.academia.edu/97712835 |journal=Heliyon |date=2022 |volume=8 |issue=11 |article-number=e11260 |doi=10.1016/J.HELIYON.2022.E11260 |doi-access=free |pmid=36345523 |bibcode=2022Heliy...811260I |pmc=9636482}}|{{Cite journal |last3=Alamsyah |first3=Dafiq Fajri |last2=Fauzi Helmi |last1=Miftahul Arifin |title=Analisis Pola Asosiasi Penjualan Produk Ritel dengan Platform Google Colab |url=https://journal.ibrahimy.ac.id/index.php/JUSTIFY/article/view/5569 |access-date=2025-12-12 |journal=Justify: Jurnal Sistem Informasi Ibrahimy |date=2024 |volume=3 |pages=74–85 |doi=10.35316/justify.v3i1.5569|doi-access=free}}}}</ref>

== History == {{Multiple image |total_width = 250 | align = left | header = Co-originators of the R language | image1 = Ross Ihaka (5189180796).jpg | caption1 = Ross Ihaka | image2 = Robert Gentleman on R Consortium.jpg | caption2 = Robert Gentleman }} R was started by professors Ross Ihaka and Robert Gentleman as a programming language to teach introductory statistics at the University of Auckland.<ref name="otago_pg12">{{Cite web |last=Ihaka |first=Ross |title=The R Project: A Brief History and Thoughts About the Future |url=https://www.stat.auckland.ac.nz/~ihaka/downloads/Otago.pdf |page=12 |url-status=live |quote=We set a goal of developing enough of a language to teach introductory statistics courses at Auckland. |archive-url=https://web.archive.org/web/20221228043824/https://www.stat.auckland.ac.nz/~ihaka/downloads/Otago.pdf |archive-date=2022-12-28 |access-date=2022-12-27}}</ref> The language was inspired by the S programming language, with most S programs able to run unaltered in R.<ref name="R FAQ">{{Cite web |last1=Hornik |first1=Kurt |last2=The R Core Team |date=2022-04-12 |title=R FAQ |url=https://cran.r-project.org/doc/FAQ/R-FAQ.html#What-are-the-differences-between-R-and-S_003f |url-status=live |archive-url=https://web.archive.org/web/20221228045640/https://cran.r-project.org/doc/FAQ/R-FAQ.html |archive-date=2022-12-28 |access-date=2022-12-27 |website=The Comprehensive R Archive Network |at=3.3 What are the differences between R and S?}}</ref> The language was also inspired by Scheme's lexical scoping, allowing for local variables.<ref name="Morandat">{{cite journal |last1=Morandat |first1=Frances |last2=Hill |first2=Brandon |last3=Osvald |first3=Leo |last4=Vitek |first4=Jan |date=11 June 2012 |title=Evaluating the design of the R language: objects and functions for data analysis |url=https://doi.org/10.1007/978-3-642-31057-7_6 |journal=European Conference on Object-Oriented Programming |volume=2012 |pages=104–131 |doi=10.1007/978-3-642-31057-7_6|access-date=2016-05-17 |via=SpringerLink|url-access=subscription}}</ref>

The name of the language, R, comes from being both an S language successor and the shared first letter of the authors, Ross and Robert.<ref>{{Cite web |last1=Hornik |first1=Kurt |last2=The R Core Team |date=2022-04-12 |title=R FAQ |url=https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-is-R-named-R_003f |url-status=live |archive-url=https://web.archive.org/web/20221228045640/https://cran.r-project.org/doc/FAQ/R-FAQ.html |archive-date=2022-12-28 |access-date=2022-12-28 |website=The Comprehensive R Archive Network |at=2.13 What is the R Foundation?}}</ref> In August 1993, Ihaka and Gentleman posted a binary file of R on StatLib — a data archive website.<ref>{{Cite web |title=Index of /datasets |url=https://lib.stat.cmu.edu/datasets/ |access-date=2024-09-05 |website=lib.stat.cmu.edu}}</ref> At the same time, they announced the posting on the ''s-news'' mailing list.<ref name="Interface98">{{Cite web |last=Ihaka |first=Ross |title=R: Past and Future History |url=https://www.stat.auckland.ac.nz/~ihaka/downloads/Interface98.pdf |url-status=live |page=4 |archive-url=https://web.archive.org/web/20221228071311/https://www.stat.auckland.ac.nz/~ihaka/downloads/Interface98.pdf |archive-date=2022-12-28 |access-date=2022-12-28}}</ref> On 5 December 1997, R became a GNU project when version 0.60 was released.<ref>{{Cite web |last=Ihaka |first=Ross |date=1997-12-05 |title=New R Version for Unix |url=https://stat.ethz.ch/pipermail/r-announce/1997/000014.html |url-status=live |archive-url=https://web.archive.org/web/20230212133422/https://stat.ethz.ch/pipermail/r-announce/1997/000014.html |archive-date=2023-02-12 |access-date=2023-02-12 |website=stat.ethz.ch}}</ref> On 29 February 2000, the 1.0 version was released.<ref name="otago_pg18">{{Cite web |last=Ihaka |first=Ross |title=The R Project: A Brief History and Thoughts About the Future |url=https://www.stat.auckland.ac.nz/~ihaka/downloads/Otago.pdf |page=18 |url-status=live |archive-url=https://web.archive.org/web/20221228043824/https://www.stat.auckland.ac.nz/~ihaka/downloads/Otago.pdf |archive-date=2022-12-28 |access-date=2022-12-27}}</ref>

== {{Anchor|CRAN}}Packages == {{Main|R package}}

[[File:Ggplot2 PlantGrowth violin plot.svg|alt=refer to caption|thumb|A violin plot created with the R package ggplot2 for data visualization ]] R packages are collections of functions, documentation, and data that expand R.<ref name="rds_pagexvii">{{cite book |last1=Wickham |first1=Hadley |last2=Cetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |title=R for Data Science, Second Edition |publisher=O'Reilly |year=2023 |page=xvii |isbn=978-1-492-09740-2}}</ref> For example, packages can add reporting features (using packages such as R Markdown, Quarto,<ref>{{Cite web |title=Quarto |url=https://quarto.org/ |access-date=2024-09-05 |website=Quarto |language=en}}</ref> knitr, and Sweave) and support for various statistical techniques (such as linear, generalized linear and nonlinear modeling, classical statistical tests, spatial analysis, time-series analysis, and clustering). Ease of package installation and use have contributed to the language's adoption in data science.<ref name="Chambers2020">{{Cite journal |last=Chambers |first=John M. |date=2020 |title=S, R, and Data Science |url=https://journal.r-project.org/archive/2020/RJ-2020-028/index.html |journal=The R Journal |language=en |volume=12 |issue=1 |pages=462–476 |doi=10.32614/RJ-2020-028 |issn=2073-4859 |doi-access=free |quote=The R language and related software play a major role in computing for data science. ... R packages provide tools for a wide range of purposes and users.}}</ref>

Immediately available when starting R after installation, base packages provide the fundamental and necessary syntax and commands for programming, computing, graphics production, basic arithmetic, and statistical functionality.<ref>{{cite book |title=The Book of R: A First Course in Programming and Statistics |first1=Tilman M. |last1=Davies |isbn=9781593276515 |year=2016 |publisher=No Starch Press |location=San Francisco, California |chapter=Installing R and Contributed Packages |pages=739}}</ref>

An example is the tidyverse collection of R packages, which bundles several subsidiary packages to provide a common API. The collection specializes in tasks related to accessing and processing "tidy data",<ref>Wickham, Hadley (2014). "Tidy Data" ([https://vita.had.co.nz/papers/tidy-data.pdf PDF]). ''Journal of Statistical Software''. '''59''' (10). doi:10.18637/jss.v059.i10.</ref> which are data contained in a two-dimensional table with a single row for each observation and a single column for each variable.<ref name="rds">{{cite book |last1=Wickham |first1=Hadley |title=R for Data Science, Second Edition |last2=Cetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |publisher=O'Reilly |year=2023 |isbn=978-1-492-09740-2}}</ref>

Installing a package occurs only once. For example, to install the tidyverse collection:<ref name="rds" /> <syntaxhighlight lang="rout"> > install.packages("tidyverse") </syntaxhighlight>

To load the functions, data, and documentation of a package, one calls the <code>library()</code> function. To load the tidyverse collection, one can execute the following code:{{efn|This code displays to standard error a listing of all the packages that the tidyverse collection depends upon. The code may also display warnings showing namespace conflicts, which may typically be ignored.}} <syntaxhighlight lang="rout"> > # The package name can be enclosed in quotes > library("tidyverse")

> # But the package name can also be used without quotes > library(tidyverse) </syntaxhighlight>

The Comprehensive R Archive Network (CRAN) was founded in 1997 by Kurt Hornik and Friedrich Leisch to host R's source code, executable files, documentation, and user-created packages.<ref name=":10" /> CRAN's name and scope mimic the Comprehensive TeX Archive Network (CTAN) and the Comprehensive Perl Archive Network (CPAN).<ref name=":10">{{Cite journal |last=Hornik |first=Kurt |date=2012 |title=The Comprehensive R Archive Network |url=https://onlinelibrary.wiley.com/doi/10.1002/wics.1212 |journal=WIREs Computational Statistics |language=en |volume=4 |issue=4 |pages=394–398 |doi=10.1002/wics.1212 |issn=1939-5108 |s2cid=62231320|url-access=subscription}}</ref> CRAN originally had only three mirror sites and twelve contributed packages.<ref>{{cite Q|Q101068595}}<!-- Announce: CRAN -->.</ref> {{As of|2025|06|30}}, it has 90 mirrors<ref name=":3">{{Cite web |title=The Status of CRAN Mirrors |url=https://cran.r-project.org/mirmon_report.html |access-date=2024-10-16 |website=cran.r-project.org}}</ref> and 22,390 contributed packages.<ref name=":9">{{Cite web |title=CRAN - Contributed Packages |url=https://cran.r-project.org/web/packages/index.html |access-date=2024-10-16 |website=cran.r-project.org}}</ref> Packages are also available in repositories such as R-Forge, Omegahat, and GitHub.<ref>{{Cite web |title=R-Forge: Welcome |url=https://r-forge.r-project.org/ |access-date=2024-09-05 |website=r-forge.r-project.org}}</ref><ref>{{Cite web |title=The Omega Project for Statistical Computing |url=https://www.omegahat.net/ |access-date=2024-09-05 |website=www.omegahat.net}}</ref><ref>{{Cite web |title=Build software better, together |url=https://github.com/trending/r |access-date=2024-09-05 |website=GitHub |language=en}}</ref>

To provide guidance on the CRAN web site, its [https://cran.r-project.org/web/views/ Task Views] area lists packages that are relevant for specific topics; sample topics include causal inference, finance, genetics, high-performance computing, machine learning, medical imaging, meta-analysis, social sciences, and spatial statistics.

The Bioconductor project provides packages for genomic data analysis, complementary DNA, microarray, and high-throughput sequencing methods.

== Community == thumb|The R Consortium is one of the three main groups that support R. There are three main groups that help support R software development:

* The R Core Team was founded in 1997 to maintain the R source code. * The R Foundation for Statistical Computing was founded in April 2003 to provide financial support. * The R Consortium is a Linux Foundation project to develop R infrastructure.

''The R Journal'' is an open access, academic journal that features short to medium-length articles on the use and development of R. The journal includes articles on packages, programming tips, CRAN news, and foundation news. thumb|125x125px|UseR! conference is one place the R community can gather at. The R community hosts many conferences and in-person meetups.{{efn|Information about conferences and meetings is available in a community-maintained list on GitHub, {{URL|jumpingrivers.github.io/meetingsR/}}}} These groups include:

* UseR!: an annual international R user conference ([https://www.r-project.org/conferences/ website]) * Directions in Statistical Computing (DSC) ([https://www.r-project.org/conferences/ website]) * R-Ladies: an organization to promote gender diversity in the R community ([https://rladies.org website]) * SatRdays: R-focused conferences held on Saturdays ([https://satrdays.github.io/ website]) * Data Science & AI Conferences ([https://rstats.ai/ website]) * posit::conf (formerly known as rstudio::conf) ([https://www.posit.co/conference/ website]) * uRos: an annual conference on the Use of R in Official Statistics ([https://urosconf.org/ website]) On social media sites such as Twitter, the hashtag <code>#rstats</code> can be used to follow new developments in the R community.<ref>{{Cite book |last1=Wickham |first1=Hadley |url=https://r4ds.had.co.nz/introduction.html |title=1 Introduction {{!}} R for Data Science |last2=Grolemund |first2=Garrett |date=January 2017 |publisher=O'Reilly Media |isbn=978-1491910399 |edition=1st |language=en}}</ref>

== Examples == === Hello, World! === The following is a "Hello, World!" program: <syntaxhighlight lang="rout">> print("Hello, World!") [1] "Hello, World!"</syntaxhighlight>Here is an alternative version, which uses the <code>cat()</code> function:<syntaxhighlight lang="rout"> > cat("Hello, World!") Hello, World! </syntaxhighlight>

=== Basic syntax === The following examples illustrate the basic syntax of the language and use of the command-line interface.{{efn|An expanded list of standard language features can be found in the manual "An Introduction to R", {{URL|cran.r-project.org/doc/manuals/R-intro.pdf}}}}

In R, the generally preferred assignment operator is an arrow made from two characters <code><-</code>, although <code>=</code> can be used in some cases.<ref>{{cite web |author=R Development Core Team |title=Assignments with the = Operator |url=https://developer.r-project.org/equalAssign.html|access-date=2018-09-11}}</ref>

<syntaxhighlight lang="rout"> > x <- 1:6 # Create a numeric vector in the current environment > y <- x^2 # Similarly, create a vector based on the values in x. > y # Print the vector’s contents. [1] 1 4 9 16 25 36

> z <- x + y # Create a new vector that is the sum of x and y > z # Return the contents of z to the current environment. [1] 2 6 12 20 30 42

> z_matrix <- matrix(z, nrow = 3) # Create a new matrix that transforms the # vector z into a 3x2 matrix object > z_matrix [,1] [,2] [1,] 2 20 [2,] 6 30 [3,] 12 42

> 2 * t(z_matrix) - 2 # Transpose the matrix; multiply every element by 2; # subtract 2 from each element in the matrix; and # then return the results to the terminal. [,1] [,2] [,3] [1,] 2 10 22 [2,] 38 58 82

# Create a new dataframe object that contains the data from a transposed # z_matrix, with row names 'A' and 'B' > new_df <- data.frame(t(z_matrix), row.names = c("A", "B")) > names(new_df) <- c("X", "Y", "Z") # Set the column names of the new_df dataframe as X, Y, and Z. > new_df # Print the current results. X Y Z A 2 6 12 B 20 30 42

> new_df$Z # Output the Z column [1] 12 42

> new_df$Z == new_df['Z'] && new_df[3] == new_df$Z # The dataframe column Z can be accessed using the syntax $Z, ['Z'], or [3], and the values are the same. [1] TRUE

> attributes(new_df) # Print information about attributes of the new_df dataframe $names [1] "X" "Y" "Z"

$row.names [1] "A" "B"

$class [1] "data.frame"

> attributes(new_df)$row.names <- c("one", "two") # Access and then change the row.names attribute; this can also be done using the rownames() function > new_df X Y Z one 2 6 12 two 20 30 42

</syntaxhighlight>

=== Structure of a function === R can create functions that add new functionality and enable code reuse.<ref>{{cite web |url=http://www.statmethods.net/management/userfunctions.html |title=Quick-R: User-Defined Functions |first=Robert |last=Kabacoff |year=2012|access-date=2018-09-28 |website=statmethods.net}}</ref> Objects created within the body of the function (which are enclosed by curly brackets) remain accessible only from within the function, and any data type may be returned. In R, almost all functions and all user-defined functions are closures.<ref>{{cite web |url=http://adv-r.had.co.nz/Functional-programming.html#closures |title=Advanced R - Functional programming - Closures |website=adv-r.had.co.nz |first=Hadley |last=Wickham}}</ref>

The following is an example of creating a function to perform an arithmetic calculation: <syntaxhighlight lang="r"># The function's input parameters are x and y. # The function, named f, returns a linear combination of x and y. f <- function(x, y) { z <- 3 * x + 4 * y

# An explicit return() statement is optional--it could be replaced with simply `z` in this case. return(z) }

# As an alternative, the last statement executed in a function is returned implicitly. f <- function(x, y) 3 * x + 4 * y</syntaxhighlight>

The following is some output from using the function defined above: <syntaxhighlight lang="rout"> > f(1, 2) # 3 * 1 + 4 * 2 = 3 + 8 [1] 11

> f(c(1, 2, 3), c(5, 3, 4)) # Element-wise calculation [1] 23 18 25

> f(1:3, 4) # Equivalent to f(c(1, 2, 3), c(4, 4, 4)) [1] 19 22 25 </syntaxhighlight>

It is possible to define functions to be used as infix operators by using the special syntax <code>`%name%`</code>, where "name" is the function variable name: <syntaxhighlight lang="rout"> > `%sumx2y2%` <- function(e1, e2) {e1 ^ 2 + e2 ^ 2} > 1:3 %sumx2y2% -(1:3) [1] 2 8 18 </syntaxhighlight>

Since R version 4.1.0, functions can be written in a short notation (inspired by the lambda calculus), which is useful for passing anonymous functions to higher-order functions:<ref>{{cite web |url=https://cran.r-project.org/doc/manuals/r-release/NEWS.html#:~:text=R%20now%20provides%20a%20shorthand%20notation%20for%20creating%20functions |title=NEWS |website=r-project.org}}</ref> <syntaxhighlight lang="rout"> > sapply(1:5, \(i) i^2) # here \(i) is the same as function(i) [1] 1 4 9 16 25 </syntaxhighlight>

=== Native pipe operator === In R version 4.1.0, a native pipe operator, <code>|></code>, was introduced.<ref>{{Cite web |title=R: R News |url=https://cran.r-project.org/doc/manuals/r-devel/NEWS.html |access-date=2024-03-14 |website=cran.r-project.org}}</ref> This operator allows users to chain functions together, rather than using nested function calls.

<syntaxhighlight lang="rout"> > nrow(subset(mtcars, cyl == 4)) # Nested without the pipe character [1] 11

> mtcars |> subset(cyl == 4) |> nrow() # Using the pipe character [1] 11 </syntaxhighlight>

An alternative to nested functions is the use of intermediate objects, rather than the pipe operator:

<syntaxhighlight lang="rout"> > mtcars_subset_rows <- subset(mtcars, cyl == 4) > num_mtcars_subset <- nrow(mtcars_subset_rows) > print(num_mtcars_subset) [1] 11 </syntaxhighlight>While the pipe operator can produce code that is easier to read, influential R programmers like Hadley Wickham suggest to chain together at most 10-15 lines of code using this operator and saving them into objects having meaningful names to avoid code obfuscation.<ref>{{Cite book |last1=Wickham |first1=Hadley |url=https://r4ds.hadley.nz/ |title=R for data science: import, tidy, transform, visualize, and model data |last2=Çetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |date=2023 |publisher=O'Reilly |isbn=978-1-4920-9740-2 |edition=2nd |location=Beijing; Sebastopol, CA |chapter=4 Workflow: code style |oclc=on1390607935 |chapter-url=https://r4ds.hadley.nz/workflow-style.html}}</ref>

=== Object-oriented programming === The R language has native support for object-oriented programming. There are two native frameworks, the so-called S3 and S4 systems. The former, being more informal, supports single dispatch on the first argument, and objects are assigned to a class simply by setting a "class" attribute in each object. The latter is a system like the Common Lisp Object System (CLOS), with formal classes (also derived from S) and generic methods, which supports multiple dispatch and multiple inheritance<ref>{{cite web |url=https://stat.ethz.ch/R-manual/R-devel/library/base/html/UseMethod.html |title=Class Methods|access-date=2024-04-25}}</ref>

In the example below, <code>summary()</code> is a generic function that dispatches to different methods depending on whether its argument is a numeric vector or a ''factor'': <syntaxhighlight lang="rout"> > data <- c("a", "b", "c", "a", NA) > summary(data) Length Class Mode 5 character character > summary(as.factor(data)) a b c NA's 2 1 1 1 </syntaxhighlight>

=== Modeling and plotting === right|thumb|200px|Diagnostic plots for the model from the example code in the "Modeling and plotting" section (q.v. the <code>plot.lm()</code> function). Mathematical notation is allowed in labels, as shown in the lower left plot. The R language has built-in support for data modeling and graphics. The following example shows how R can generate and plot a linear model with residuals. <syntaxhighlight lang="r"> # Create x and y values x <- 1:6 y <- x^2

# Linear regression model: y = A + B * x model <- lm(y ~ x)

# Display an in-depth summary of the model summary(model)

# Create a 2-by-2 layout for figures par(mfrow = c(2, 2))

# Output diagnostic plots of the model plot(model) </syntaxhighlight>

The output from the <code>summary()</code> function in the preceding code block is as follows: <syntaxhighlight lang="rout"> Residuals: 1 2 3 4 5 6 7 8 9 10 3.3333 -0.6667 -2.6667 -2.6667 -0.6667 3.3333

Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -9.3333 2.8441 -3.282 0.030453 * x 7.0000 0.7303 9.585 0.000662 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.055 on 4 degrees of freedom Multiple R-squared: 0.9583, Adjusted R-squared: 0.9478 F-statistic: 91.88 on 1 and 4 DF, p-value: 0.000662 </syntaxhighlight>

=== Mandelbrot set === thumb|200px|A Mandelbrot set as visualized in R. (Note: The colours in this image differ from the output of the sample code in the "Mandelbrot set" section.) This example of a Mandelbrot set highlights the use of complex numbers. It models the first 20 iterations of the equation <code>z = z<sup>2</sup> + c</code>, where <code>c</code> represents different complex constants.

To run this sample code, it is necessary to first install the package that provides the <code>write.gif()</code> function: <syntaxhighlight lang="r"> install.packages("caTools") </syntaxhighlight>

The sample code is as follows: <syntaxhighlight lang="r">library(caTools)

jet.colors <- colorRampPalette( c("green", "pink", "#007FFF", "cyan", "#7FFF7F", "white", "#FF7F00", "red", "#7F0000"))

dx <- 1500 # define width dy <- 1400 # define height

C <- complex( real = rep(seq(-2.2, 1.0, length.out = dx), each = dy), imag = rep(seq(-1.2, 1.2, length.out = dy), times = dx) )

# reshape as matrix of complex numbers C <- matrix(C, dy, dx)

# initialize output 3D array X <- array(0, c(dy, dx, 20))

Z <- 0

# loop with 20 iterations for (k in 1:20) {

# the central difference equation Z <- Z^2 + C

# capture the results X[, , k] <- exp(-abs(Z)) }

write.gif( X, "Mandelbrot.gif", col = jet.colors, delay = 100)</syntaxhighlight>

== Version names == alt=A CD with autographs on it|thumb|A CD of R Version 1.0.0, autographed by the core team of R, photographed in Quebec City in 2019|180x180px All R version releases from 2.14.0 onward have codenames that make reference to ''Peanuts'' comics and films.<ref>{{Cite book |last=Monkman |first=Martin |url=https://bookdown.org/martin_monkman/DataScienceResources_book/r-release-names.html |title=Chapter 5 R Release Names {{!}} Data Science with R: A Resource Compendium}}</ref><ref>{{Cite web |last=McGowan |first=Lucy D’Agostino |date=2017-09-28 |title=R release names |url=https://livefreeordichotomize.com/posts/2017-09-28-r-release-names/index.html |access-date=2024-04-07 |website=livefreeordichotomize.com |language=en}}</ref><ref>{{Citation |title=r-hub/rversions |date=2024-02-29 |url=https://github.com/r-hub/rversions |access-date=2024-04-07 |publisher=The R-hub project of the R Consortium}}</ref>

In 2018, core R developer Peter Dalgaard presented a history of R releases since 1997.<ref name=":2">{{Cite web |last=Dalgaard |first=Peter |date=2018-07-15 |title=What's in a name? 20 years of R release management |url=https://www.youtube.com/watch?v=z1vTSdRolgI |access-date=2024-04-09 |website=YouTube |format=video}}</ref> Some notable early releases before the named releases include the following:

* Version 1.0.0, released on 29 February 2000, a leap day * Version 2.0.0, released on 4 October 2004, "which at least had a nice ring to it"<ref name=":2" />

The idea of naming R version releases was inspired by the naming system for Debian and Ubuntu versions. Dalgaard noted an additional reason for the use of Peanuts references in R codenames—the humorous observation that "everyone in statistics is a P-nut."<ref name=":2" />

== Interfaces == {{Gallery |title=Examples of user interfaces for R |width=160 | height=150 |noborder=yes |align=center |footer= |File:RKWard_screenshot.png |Screenshot of the RKWard front-end running on the KDE 4 environment |alt1= |File:RinEmacswithESS.png |R running in the emacs editor with the ESS package |alt2= |File:RStudio 2023.12.1 Build 402 (256-135).png |RStudio integrated development environment (IDE) |mode=packed}}

R is installed with a command line console by default, but there are multiple ways to interface with the language:

* Integrated development environment (IDE): ** R.app<ref>{{Cite web |title=R for macOS |url=https://cran.r-project.org/bin/macosx/ |access-date=2024-09-05 |website=cran.r-project.org}}</ref> (OSX/macOS only) ** Rattle GUI ** R Commander ** RKWard ** RStudio ** Positron<ref>{{Cite web |title=IDE from Posit PBC, the creators of RStudio {{!}} Positron - Home |url=https://positron.posit.co/ |access-date=2025-09-24 |website=Positron|language=en}}</ref> ** Tinn-R<ref>{{Cite web |title=IDE/Editor para Linguagem R {{!}} Tinn-R - Home |url=https://tinn-r.org/en/ |access-date=2024-09-05 |website=Tinn-R |language=pt-br}}</ref> * General-purpose IDEs: ** Eclipse via the [https://marketplace.eclipse.org/content/statet-r/ StatET plugin] ** Visual Studio via R Tools for Visual Studio. * Source-code editors: ** Emacs ** Vim via the [https://www.vim.org/scripts/script.php?script_id=2628 Nvim-R plugin] ** Kate ** LyX via Sweave ** WinEdt ([https://cran.r-project.org/package=RWinEdt website]) ** Jupyter ([https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/ website]) * Other scripting languages: ** Python ([https://rpy2.github.io website]) ** Perl ([https://metacpan.org/module/Statistics::R website]) ** Ruby ([https://github.com/alexgutteridge/rsruby source code]) ** F# ([https://bluemountaincapital.github.io/FSharpRProvider/ website]) ** Julia ([https://github.com/JuliaInterop/RCall.jl source code]). * General-purpose programming languages: ** Java via the [https://cran.r-project.org/web/packages/Rserve/index.html Rserve socket server] ** .NET C# ([https://rdotnet.github.io/rdotnet/ website])

Statistical frameworks that use R in the background include Jamovi and JASP.{{citation needed|date=March 2025}}

== Implementations == The main R implementation is written primarily in C, Fortran, and R itself. Other implementations include the following: * [http://www.pqr-project.org pretty quick R] (pqR), by Radford M. Neal, which attempts to improve memory management. * Renjin for the Java Virtual Machine. * [https://www.cs.kent.ac.uk/projects/cxxr CXXR] and Riposte<ref>{{cite book |last1=Talbot |first1=Justin |last2=DeVito |first2=Zachary |last3=Hanrahan |first3=Pat |title=Proceedings of the 21st international conference on Parallel architectures and compilation techniques |chapter=Riposte: A trace-driven compiler and parallel VM for vector code in R |date=1 January 2012 |pages=43–52 |doi=10.1145/2370816.2370825 |publisher=ACM |isbn=9781450311823 |s2cid=1989369}}</ref> written in C++. * Oracle's [https://github.com/oracle/fastr FastR] built on [https://www.graalvm.org/ GraalVM]. * TIBCO Enterprise Runtime for R (TERR) to integrate with Spotfire.<ref>Jackson, Joab (16 May 2013). [https://www.pcworld.com/article/2038944/tibco-offers-free-r-to-the-enterprise.html TIBCO offers free R to the enterprise]. ''PC World''. Retrieved 20 July 2015.</ref> (The company also sold S-Plus, a discontinued implementation of the S language.)

Microsoft R Open (MRO) was an R implementation. As of 30 June 2021, Microsoft began to phase out MRO in favor of the CRAN distribution.<ref>{{cite web |title=Looking to the future for R in Azure SQL and SQL Server |date=30 June 2021 |access-date=7 November 2021 |url=https://cloudblogs.microsoft.com/sqlserver/2021/06/30/looking-to-the-future-for-r-in-azure-sql-and-sql-server/}}</ref>

== Commercial support == {{anchor|Commercialised versions of R|Commercialized versions of R}} <!--used in https://cran.r-project.org/doc/FAQ/R-FAQ.html#What-is-R_002dplus_003f-->

Although R is an open-source project, some companies provide commercial support: * Oracle provides commercial support for its Big Data Appliance, which integrates R into its other products. * IBM provides commercial support for execution of R within Hadoop.

== See also == * Comparison of statistical packages * List of numerical-analysis software * List of R programming books * List of R software and tools * List of statistical software * Rmetrics

==Notes== {{Notelist}}

== References == {{reflist}}

== Further reading == * {{Cite book |last1=Wickham |first1=Hadley |url=https://r4ds.hadley.nz/ |title=R for data science: import, tidy, transform, visualize, and model data |last2=Çetinkaya-Rundel |first2=Mine |last3=Grolemund |first3=Garrett |date=2023 |publisher=O'Reilly |isbn=978-1-4920-9740-2 |edition=2nd |location=Beijing Boston Farnham Sebastopol Tokyo}} * {{Cite book |last=Gagolewski |first=Marek |year=2024 |isbn=978-0-6455719-2-9 |title=Deep R Programming |url=https://zenodo.org/record/7490464 |language=en |doi=10.5281/ZENODO.7490464}}

== External links == * [https://www.r-project.org/other-docs.html R documentation] * [https://developer.r-project.org/TechDocs/ R Technical Papers] * [https://www.bigbookofr.com/ Big Book of R], curated list of R-related programming books * [https://www.r-project.org/doc/bib/R-books.html Books Related to R - R Project], partially annotated curated list of books relating to R or S.

{{R (programming language)}} {{GNU}} {{Numerical analysis software}} {{Statistical software}} {{Programming languages}}

{{Portal bar|Computer programming}}

{{Authority control}}

Category:R (programming language) Category:Array programming languages Category:Cross-platform free software Category:Data mining and machine learning software Category:Data-centric programming languages Category:Dynamically typed programming languages Category:Free plotting software Category:Free statistical software Category:Functional languages Category:GNU Project software Category:Literate programming Category:Numerical analysis software for Linux Category:Numerical analysis software for macOS Category:Numerical analysis software for Windows Category:Programming languages Category:Programming languages created in 1993 Category:Science software Category:Statistical programming languages Category:Articles with example R code