R
To install R, visit a nearby
CRAN mirror (i.e.,
a website with the same content as the main CRAN page), download R via
Download R for Windows > base >
Download R-X.Y.Z for Windows and install it by opening the
downloaded .exe file.
R is not required to read R scripts: R scripts are plain-text files that can be read by applications such as NotePad.
Making R stricter
R can be configured by changing environment variables and various
options, see help("environment variables"),
help("options"), help("install.packages"), and
help(".libPaths"). Although options for startup can also be
changed (see help("Startup") and the chapter R Startup from the
book What They Forgot to Teach You About R),
that should be done cautiously because those options likely make code
behave differently on PCs where those options are not set, for example
when changing which packages are automatically loaded at startup.
Various options can be changed to make R a bit stricter:
- Warn in case of partial matching when extracting using
$(e.g.,list(mean = 3)$me), when matching function arguments, and when getting object attributes:options(warnPartialMatchDollar = TRUE, warnPartialMatchArgs = TRUE, warnPartialMatchAttr = TRUE), with defaultFALSEfor each of these. These options were introduced in R 2.6.0. Details:help("pmatch"),help("Extract"), andhelp("attr"). - Error instead of warn when calling
a:bwhen numericaorbis longer than one, such as3:c(5, 7):Sys.setenv("_R_CHECK_LENGTH_COLON_" = "TRUE"). This option was introduced in R 4.3.0. Details:help("colon", package = "base"). - Error instead of silently using only the first element in logical
operations such as
c(TRUE, TRUE) && TRUE):Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = "TRUE"). Details:help("Logic", package = "base"). This option was introduced in R 3.6.0 and is no longer used since R 4.3.0 because there calling&&or||with length larger than one always gives an error. - Error instead of warn if a condition has length larger than one,
such as
if(3 < c(5, 7)):Sys.setenv("_R_CHECK_LENGTH_1_CONDITION_" = "TRUE"). Details:help("Control", package = "base"). This option was introduced in R 3.4.0 and is no longer used since R 4.2.0 because there conditions with length larger than one always give an error. - Error instead of warn in case of conflicts (i.e., if objects with
the same name exist in two or more places on the search path):
options(conflicts.policy = "strict"). This option was introduced in R 3.6.0. Details:help("conflicts", package = "base")and the sectionConflictsinhelp("conflictRules", package = "base")for further options to tune conflict resolution, andconflicted::conflicts_prefer(<pkg>::<func>)from packageconflictedto declare preferences (<pkg>and<func>should be replaced by the actual names of the package and function to get working code). - Print warnings immediately as they occur
(
options(warn = 1)) or make them an error (options(warn = 2)). The latter should only be used for debugging because it may trigger bugs and resource leaks. To suppress warnings altogether, useoptions(warn = -1)or wrap function calls insuppressWarnings()to suppress particular warnings. The default isoptions(warn = 0)to warn after the top-level function returns. Details:help("stop")andhelp("warning"). - Enter the environment browser upon error:
options(error = browser), with defaultoptions(error = NULL)to not enter the environment browser. PressQorEscapeto quit the browser mode and presscto continue code. Details:help("browser").
In addition, package strict makes R
stricter by warning about various unsafe practices, such as the
behaviour of sample() and diag() if called
with an argument of length one, and type-unsafe
sapply().
Information about R
Several variables and functions provide information about the current R session:
-
capabilities()andextSoftVersion()provide details about external software that can be used with R. - Environment variables affect an R session. Its help page
(
help("environment variables")) lists some of these environment variables. -
getRversion()provides the version of the running R. -
.Machine(seehelp(".Machine")) andSys.info()provide information about the machine and platform R is running on. Operating systems might identify themselves and their versions in surprising ways, and Windows might report older versions than the versions that are actually installed (see the sectionosVersioninhelp("sessionInfo", package = "utils")and theNoteinhelp("win.version", package = "utils"). -
.Platform(seehelp(".Platform")) andR.Version()provide information about the platform R was built on. -
Sys.getlocale()andl10n_info()provide details about the locale (i.e., settings that depend on the user’s language or region). -
utils::sessionInfo()extracts parts of the information mentioned above about the operating system and R, and lists attached and loaded packages. Its printing method can be used to print additional information about the used locale (i.e., settings that depend on the user’s language or region) and random number generation:print(sessionInfo(), locale = TRUE, RNG = TRUE).sessioninfo::session_info()provides more details about the origin of loaded or installed packages, and has the option to show only information about selected packages and their dependencies.
Rtools
Rtools
is not an R package but software used to build R from source and to
build R packages from source. The latter is needed when installing
packages from GitHub and when
installing older versions of packages from CRAN or Bioconductor
(see the section Installing packages in the vignette R
packages for instructions on installing R packages:
vignette("r_pkgs", package = "checkrpkgs")).
To install Rtools,
download the version appropriate for the installed version of R (see the
output of getRversion()) from CRAN via
Download R for Windows > Rtools >
RTools X.Y and set it up using the instructions given
there. See also the HowTo
by Tomas Kalibera. Alternatively, use
pkgbuild::check_build_tools(debug = TRUE) to check and
install Rtools.
If you have installed Rtools, you might get the question
Do you want to install from sources the packages which need compilation?
when updating packages. You can choose Yes to install the
latest package versions by building them from source, or choose
No to get slightly less up-to-date package versions but a
faster installation. See the section Troubleshooting in the
vignette R packages for details:
vignette("r_pkgs", package = "checkrpkgs").
RStudio
RStudio is an integrated development environment for R developed by Posit that can be downloaded here.
RStudio can also be used to read and modify plain-text files.
Configuring RStudio
After installing RStudio, start RStudio, go to Tools
> Global Options > General to
deselect the option
Restore .RData into workspace at startup and set the option
Save workspace to .RData on exit to Never to
make work portable (i.e., make sure that R does not use information or
data that is not present on another PC). A more disrupting change to
enhance portability is to deselect the options
Show diagnostics for R and
Prompt to install missing R packages discovered in R source files
at Tools > Options > Code
> Diagnostics. Deselecting these options ensures
namespaces are not automatically loaded when RStudio loads a
project.
In the same tab General, you can also specify the
version of R that should be used and the default working directory (see
help("getwd")) to be used when not in a
project.
Keyboard shortcuts can be modified at Tools >
Modify Keyboard Shortcuts, e.g., to change the shortcut
Run current line or selection from Ctrl+Enter
to Ctrl+R so it can be used with one hand. Sometimes
RStudio does not use the modified keyboard shortcuts. Going to
Tools > Modify Keyboard Shortcuts usually
fixes that without the need to actually reset the shortcuts.
The appearance of code can be changed at Tools >
Global options > Appearance. Nice editor
themes are the light Xcode and the dark
Tomorrow Night Bright, Idle Fingers, and
Pastel On Dark. Nice editor fonts are
Consolas, Cacadia Mono Light, and
Lucida Console.
Choosing a good editor font deserves some attention: using a font with clearly distinct characters prevents confusing similar characters when reading or debugging code. The following strings group together characters that in some fonts are similar in appearance. Letters are indicated with their names in the same case as used in the string:
-
B8 S5 y4 Z2: BEE, eight; ESS, five; wye, four; ZED, two -
gq ijy rnm uvvw UVVW: gee, cue; eye, jay, wye; ar, en, em; u, repeated vee, double-u; U, repeated VEE, double-U -
., :; "'' __: dot, comma; colon, semicolon; double quotes, repeated single quotes; repeated underscores -
cldcIdc|dc1: cee, el, dee, cee, EYE, dee, cee, vertical bar, dee, cee, one -
71lI|i/: seven, one, el, EYE, vertical bar, eye, slash -
oQO0D: oh, CUE, OH, zero, DEE
Combining these strings gives
B8 S5 y4 Z2 gq ijy rnm uvvw UVVW ., :; "'' __ cldcIdc|dc1 71lI|i/ oQO0D
which can be used to compare fonts, for example using Adobe Fonts, Font Squirrel, Google Fonts, or MyFonts.
Documentation and help
-
Bug reporting and
the relevant section of the
R FAQ - CRAN: the homepage and an
overview of its mirrors (for details
on mirrors, see the section
Mirror websitesin the vignette R packages:vignette("r_pkgs", package = "checkrpkgs")). - R homepage with FAQs,
NEWS, and manuals (especially theR Installation and Administration manual) with derived versions better suited for searching. - R help: from inside R through
utils::help.start()or online - R mailing lists with a web interface with information and archives and a mirror for searching the mailing list archives
- RStudio user guide and cheatsheet by Posit
- Search engines specific for R: METACRAN, r-project, Rseek, R-universe
-
StackOverflow
posts with the
rtag
Resources with more details than this vignette:
- The book
What They Forgot to Teach You About Rby J. Bryan, J. Hester, S. Pileggi, and E. D. Aja - The book
An introduction to Rby A. Douglas, D. Roos, F. Mancini, A. Couto and D. Lusseau - Chapter
Installation & Environment Setupfrom the bookMastering R Through Errors and Warningsby K. Guo - Chapter
Getting Started and Getting Helpfrom theR Cookbookby J. Long and P. Teetor