GitHub
LinkedIn
Twitter
YouTube
RSS

New features in R 4.2.0

Author: Colin Gillespie

Published: April 1, 2022

New features in R 4.2.0

R-4.2.0 is due to be released!

Another year has passed, which means an updated version of R is due to be released - R version 4.2.0. As usual there have been many changes and improvements over the last year. This blog post only picks out a tiny handful, but there are dozens of smaller changes and bug fixes that can be found in the changelog.


Do you use RStudio Pro? If so, checkout out our managed RStudio services


Pipe updates

The native pipe |> was introduced in R 4.1.0. This release has introduced the _ that can be used as a placeholder in a named argument. For example,

# This works in R 4.2
mtcars |>
  lm(mpg ~ disp, data = _)

Side note: some of the older R users amongst us, still remember when _ was used as an assignment operator!

Improved help page

The HTML help system has a few new features:

  • LaTeX-like mathematics can be typeset using either KaTeX or MathJax, usage and example code is highlighted using Prism.
  • Dynamic help for examples and code demos using {knitr}.

The HTML help system now uses HTML5 (via PR#18149). What this means to everyday R users is that the help package looks a lot nicer

The lm help page with R 4.2.0

The examples can now be easily run using the Run Examples button

The Run example page using 4.2.0

if and while statements

Calling while() or if() statements with a condition of length greater than one gives an error rather than a warning.

if (1:2 == 1) do_something()
# Error in if (1:2 == 1) do_something() : the condition has length > 1

This is clearly a good thing, as when the length of the condition is greater than one, this is almost certainly an error. If you are using older versions of R, you can set _R_CHECK_LENGTH_1_CONDITION_ to true in your .Renviron to get the same effect.

Changes to Windows

There are quite few changes to Windows, in particular

  • Support for 32-bit builds has been dropped.
  • UTF-8 locales are used where available.
  • The default personal library on Windows, folder R\win-library\x.y where x.y stands for R release x.y.z, is now a subdirectory of local application data directory (usually a hidden directory C:\Users\username\AppData\Local).

Data frames gain a new method

The as.vector() function gains an S3 data frame method which returns a simple list.

#> as.vector(mtcars[1:5,])
#$mpg
#[1] 21.0 21.0 22.8 21.4 18.7
#
#$cyl
#[1] 6 6 4 6 8
#
#$disp
#[1] 160 160 108 258 360

This is a breaking change; in previous versions of R, as.vector() just returned the data frame.

Changes to logical functions

This was part of R 4.2.0, but has been bumped to R-devel as there are around 200 packages that have to be updated.

Similar to the if() and while() statements, the logical functions, && and ||, now give a warning when one of the comparisons has a length greater than one:

1:2 && 1
# [1] TRUE
# Warning message:
# In 1:2 && 1 : 'length(x) = 2 > 1' in coercion to 'logical(1)'

In future versions, this will become an error.

Trying the latest version out for yourself

The rocker project takes away the pain of installing the latest version. If you have docker installed, then simply use the devel version:

docker run --rm -ti rocker/r-ver:devel

If you want to use RStudio as well, then run

docker run -e PASSWORD=<PASSWORD> -p 8787:8787 rocker/rstudio:devel

See also

Do you have some nostalgia for previous versions of R? If so, check out our previous blog posts:


Jumping Rivers Logo