R (programming language): Difference between revisions

Content deleted Content added
Mandelbrot set: Clarified the connection between the image on the right and the sample code on the left.
m Reverting possible vandalism by 154.115.204.4 to version by 207.91.254.122. Report False Positive? Thanks, ClueBot NG. (4413697) (Bot)
 
(41 intermediate revisions by 16 users not shown)
Line 1:
{{Technical|introduction|date=May 2025}}
{{Short description|Programming language for statistics}}
{{About|the programming language|the eighteenth letter of the English alphabet|R|other uses|R (disambiguation)}}
{{Use dmy dates|date=June 2018}}
{{Infobox programming language
Line 31 ⟶ 29:
| 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>|.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>}}
}}
{{redirect-distinguish|R lang|Erlang}}
 
'''R''' is a [[programming language]] for [[statistical computing]] and [[Data and information visualization|data visualization]]. It has been widely adopted in the fields of [[data mining]], [[bioinformatics]] and, [[data analysis]]/, and [[data science]].<ref>{{Cite journal
|last1=Giorgi
|first1=Federico M.
Line 52 ⟶ 51:
|doi-access=free }}</ref>
 
The core R language is augmentedextended by a large number of [[R package|extension software packages]], which contain [[Reusability|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 software is [[Openfree and source|open-source]] and [[free software]]. R is a [[GNU Project]] and licenseddistributed 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> ItThe language is writtenimplemented primarily in [[C (programming language)|C]], [[Fortran]], and [[Self-hosting (compilers)|R itself]]. [[Preprocessor|Precompiled]] [[executable]]s are providedavailable for variousthe major [[operating system]]s (including [[Linux]], [[MacOS]], and [[Microsoft Windows]]).
 
AsIts core is an [[interpreted language]], R haswith a native [[command line interface]]. MoreoverIn addition, multiple [[Third-party software component|third-party]] applications are available as [[graphical user interface]]s; aresuch available,applications such asinclude [[RStudio]] (an [[integrated development environment]]) and [[Jupyter]] (a [[notebook interface]]).
 
== History ==
{{Multiple image |total_width = 250 | align = left
{{Gallery
|title header = Co-originators of the R language
|File: image1 = Ross Ihaka (5189180796).jpg
|width=160 | height=170 |noborder=yes
| caption1 = [[Ross Ihaka]]
|align=center
|File: image2 = Robert Gentleman on R Consortium.jpg
|footer=
| caption2 = [[Robert Gentleman (statistician)|Robert Gentleman]]
|File:Ross Ihaka (5189180796).jpg
|Ross Ihaka
|alt1=
|File:Robert Gentleman on R Consortium.jpg
|Robert Gentleman
|alt2=
}}
 
R was started by professors [[Ross Ihaka]] and [[Robert Gentleman (statistician)|Robert Gentleman]] as a programming language to teach introductory statistics at the [[University of Auckland]].<ref name="otago_pg12">{{Cite web
|last=Ihaka
Line 167 ⟶ 160:
</syntaxhighlight>
 
To load the functions, data, and documentation of a package, one executescalls the <code>library()</code> function. To load the tidyverse collection, one can execute the following code:{{efn|This code displays to [[Standard streams|standard error]] a listing of all the packages that ''the tidyverse'' collection depends upon. ItThe code may also display warnings showing namespace conflicts, which may typically be ignored.}}
<syntaxhighlight lang="rout">
> # The package name can be enclosed in quotes
Line 176 ⟶ 169:
</syntaxhighlight>
 
The [[R package#Comprehensive R Archive Network (CRAN)|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 [[Mirrormirror site|mirror sites]]s and twelve contributed packages.<ref>{{cite Q|Q101068595}}<!-- Announce: CRAN -->.</ref> {{As of|20242025|1006|1630}}, it has 9990 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 2122,513390 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 [[Repository (version control)|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 science]]s, and [[spatial statistics]].
Line 183 ⟶ 176:
 
== Community ==
[[File:RConsortiumLogo.webp|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:
 
Line 190 ⟶ 184:
 
''[[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.
[[File:UseR Logo.png|thumb|125x125px|UseR! conference is one place the R community can gather at]]
 
The R community hosts many conferences and in-person meetups.{{efn|SeeInformation hereabout forconferences and meetings is available in a community -maintained GitHub 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.orggithub.io/ website])
* RData ConferenceScience & AI Conferences ([https://rstats.ai/ website])
* posit::conf (formerly known as rstudio::conf) ([https://www.posit.co/conference/ 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 |last=Wickham |first=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]] |year=2017 |isbn=978-1491910399 |edition=1st |language=en}}</ref>
 
== Examples ==
Line 211 ⟶ 205:
 
=== Basic syntax ===
The following examples illustrate the basic [[programming language syntax|syntax of the language]] and use of the command-line interface.{{efn|An expanded list of standard language features can be found in the R manual, "An Introduction to R", {{URL|cran.r-project.org/doc/manuals/R-intro.pdf}}}}
 
In R, the generally preferred [[Assignment (computer science)|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>
Line 267 ⟶ 261:
 
</syntaxhighlight>
 
=== Structure of a function ===
R is able to create [[Function (computer programming)|functions]] that add new functionality for 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> [[Object (computer science)|Objects]] created within the body of the function (which are enclosed by curly brackets) remain [[Local variable|accessible only]] from within the function, and any [[data type]] may be returned. In R, almost all functions and all [[user-defined function]]s are [[closure (computer programming)|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>
Line 319 ⟶ 314:
</syntaxhighlight>
 
AnotherAn alternative to nested functions is the use of intermediate objects, rather than the pipe operator:
 
<syntaxhighlight lang="rout">
Line 326 ⟶ 321:
> print(num_mtcars_subset)
[1] 11
</syntaxhighlight>While the pipe operator can produce code that is easier to read, itinfluential isR programmers like [[Hadley Wickham]] advisablesuggest to chain together at most 10-15 lines of code using this operator, asand wellsaving as to chunk codethem into [[Taskobjects (projecthaving management)|sub-tasks]]meaningful thatnames areto savedavoid into[[Code objectsobfuscating|code having meaningful namesobfuscation]].<ref>{{Cite book |last=Wickham |first=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>
 
The following is an example having fewer than 10 lines, which some readers may find difficult to grasp in the absence of intermediate named steps:<syntaxhighlight lang="r" line="1">(\(x, n = 42, key = c(letters, LETTERS, " ", ":", ")"))
strsplit(x, "")[[1]] |>
(Vectorize(\(chr) which(chr == key) - 1))() |>
(`+`)(n) |>
(`%%`)(length(key)) |>
(\(i) key[i + 1])() |>
paste(collapse = "")
)("duvFkvFksnvEyLkHAErnqnoyr")</syntaxhighlight>The following is a version of the preceding code that is easier to read:<syntaxhighlight lang="r">
default_key <- c(letters, LETTERS, " ", ":", ")")
 
f <- function(x, n = 42, key = default_key) {
split_input <- strsplit(x, "")[[1]]
results <- (Vectorize(\(chr) which(chr == key) - 1))(split_input) |>
(`+`)(n) |>
(`%%`)(length(key)) |>
(\(i) key[i + 1])()
combined_results <- paste(results, collapse = "")
return(combined_results)
}
 
f("duvFkvFksnvEyLkHAErnqnoyr")
</syntaxhighlight>
 
=== Object-oriented programming ===
The R language has native support for [[object-oriented programming]]. There are two native [[Application framework|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 [[CLOS|Common Lisp Object System (CLOS)]], with formal classes (also derived from [[S (programming language)#S4|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>
Line 455 ⟶ 426:
 
== Version names ==
[[File:CD of R 1 0 0.png|alt=A CD with autographs on it|thumb|A CD of R Version 1.0.0, autographed by the core team of R, photographed R in Quebec City in 2019|180x180px]]
All R version releases from 2.14.0 onward have [[Code name|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 (2000-02-29), a [[Leap days|leap day]]
* Version 2.0.0, released on 4 October 2004 (2004-10-04), "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 version history|Debian]] and [[Ubuntu version history|Ubuntu]] version naming systemversions. Dalgaard also noted thatan anotheradditional reason for the use of Peanuts references forin R codenamescodenames—the ishumorous because,observation that "everyone in statistics is a [[P-value|P-nut]]"."<ref name=":2" />
 
{| class="wikitable"
Line 472 ⟶ 443:
!Peanuts reference
!Reference
|-
|4.5.1
|2025-06-13
|Great Square Root
|<ref>{{Cite web |title=Read Peanuts by Charles Schulz on GoComics |url=https://www.gocomics.com/peanuts/1978/02/24 |access-date=2025-06-13 |website=www.gocomics.com |language=en}}</ref>
|<ref>{{Cite web |title=[Rd] R 4.5.1 is released |url=https://hypatia.math.ethz.ch/pipermail/r-announce/2025/000713.html |access-date=2025-06-13 |website=hypatia.math.ethz.ch}}</ref>
|-
|4.5.0
Line 752 ⟶ 729:
|2015-03-09
|Smooth Sidewalk
|<ref>{{Cite book |last=Schulz |first=Charles M. |title=Happiness is a warm puppy |date=2019 |publisher=Penguin Workshop |___location=New York : Penguin Workshop |isbn=978-1-5247-8995-4}}</ref>{{Page needed|date=April 2024}}
|<ref>{{Cite web |title=R 3.1.3 is released |url=https://stat.ethz.ch/pipermail/r-announce/2015/000582.html |access-date=2024-04-07 |website=stat.ethz.ch}}</ref>
|-
Line 848 ⟶ 825:
== Interfaces ==
{{Gallery
|title=ExampleExamples alternativeof user interfaces tofor 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]] [[Integratedintegrated development environment]] (IDE)
|mode=packed}}
 
R comesis installed with a [[Command-line interface|command line console]] by default, but itthere isare notmultiple the only wayways to interface with R.the language:
 
* By [[Integrated development environment|integratedIntegrated 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]]
Line 871 ⟶ 848:
** [[RStudio]]
** 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>
* By general General-purpose IDEs:
** [[Eclipse (software)|Eclipse]] via the [https://marketplace.eclipse.org/content/statet-r/ StatET plugin]
** [[Microsoft Visual Studio|Visual Studio]] via [[R Tools for Visual Studio]].
* By [[Source-code editor|source-code editors]]s:
** [[Emacs Speaks Statistics|Emacs]]
** [[Vim (text editor)|Vim]] via the [https://www.vim.org/scripts/script.php?script_id=2628 Nvim-R plugin]
Line 881 ⟶ 858:
** [[WinEdt]] ([https://cran.r-project.org/package=RWinEdt website])
** [[Jupyter]] ([https://docs.anaconda.com/anaconda/navigator/tutorials/r-lang/ website])
* By otherOther [[scripting language]]s:
** [[Python (programming language)|Python]] ([https://rpy2.github.io website])
** [[Perl]] ([https://metacpan.org/module/Statistics::R website])
Line 887 ⟶ 864:
** [[F Sharp (programming language)|F#]] ([https://bluemountaincapital.github.io/FSharpRProvider/ website])
** [[Julia (programming language)|Julia]] ([https://github.com/JuliaInterop/RCall.jl source code]).
* By general General-purpose programming languages:
** [[Java (programming language)|Java]] via the [https://cran.r-project.org/web/packages/Rserve/index.html Rserve socket server]
** [[C Sharp (programming language)|.NET C#]] ([https://rdotnet.github.io/rdotnet/ website])
 
Statistical frameworks whichthat use R in the background include [[Jamovi]] and [[JASP]].{{factcitation needed|date=March 2025}}
 
== Implementations ==
The main R implementation is written primarily in [[C (programming language)|C]], [[Fortran]], and [[Self-hosting (compilers)|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]] is an implementation of ''R'' for the [[Java virtual machine|Java Virtual Machine]].
* [https://www.cs.kent.ac.uk/projects/cxxr CXXR] and Riposte<ref>{{cite book
|last1=Talbot
Line 911 ⟶ 888:
|publisher=ACM
|isbn=9781450311823
|s2cid=1989369}}</ref> are implementations of ''R'' written in [[C++]].
* [[Oracle Corporation|Oracle's]] [https://github.com/oracle/fastr FastR] is an implementation of ''R'', built on [https://www.graalvm.org/ GraalVM].
* [[TIBCO Software|TIBCO]], creatorEnterprise ofRuntime [[S-PLUS]],for wroteR (TERR — an ''R'' implementation) to integrate with [[Spotfire]].<ref>Jackson, Joab (16 May 2013). [httphttps://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 created [[S-PLUS|S-Plus]], an implementation of the S language.)
 
Microsoft R Open (MRO) was an ''R'' implementation. As of 30 June 2021, Microsoft startedbegan 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
Line 925 ⟶ 902:
 
Although R is an [[open-source]] project, some companies provide commercial support:
* [[Oracle Corporation|Oracle]] provides commercial support for theits ''[[Oracle Big Data Appliance|Big Data Appliance]]'', which integrates R into its other products.
* [[IBM]] provides commercial support for in-[[Hadoop]] execution of R within [[Hadoop]].
 
== See also ==