[Rd] Locking of base environment in R 4.1.0 breaks simple assignment of .First() (etc) from Rprofile.site

Jake Elmstedt j@ke@e|m@tedt @end|ng |rom gm@||@com
Tue May 25 01:35:31 CEST 2021


Commits 80162 and 80163 lock the base environment and namespace during
startup, leading to an error when attempting to directly assign anything
from within Rprofile.site. While this is intentional and good, the help
file has not been updated to reflect this change.

Startup.Rd ( Description, paragraph 3) reads,

> ...This code is sourced into the base package. Users need to be careful
not to unintentionally overwrite objects in base, and it is normally
advisable to use local if code needs to be executed: see the examples.

Since the base environment and namespace are locked as of 4.1.0, I
recommend this be edited to something to the effect of:

> Prior to R 4.1.0, this code is sourced into the base package. Users need
to be careful not to unintentionally overwrite objects in base, and it is
normally advisable to use local if code needs to be executed: see the
examples. As of R 4.1.0 the base environment and namespace are locked and
any attempted direct assignment will fail with an error, and none of the
subsequent commands will be invoked. Users migrating to R 4.1.0 and above
can edit assignments made in Rprofile.site from the form `x <- value` to
`assign("x", value, envir = globalenv())`. Common uses of this may include
the binding of functions `.First` and `.Last`.

Description paragraph 8,

> "A function .First (and .Last) can be defined in appropriate ‘.Rprofile’
or ‘Rprofile.site’ files..."

which might be misleading to users not well-versed in the intricacies of
the R startup process. Should this be edited to indicate variable
assignment from Rprofile.site must be done using assign() to bind them to
globalenv()? Something to the effect of,

> A function .First (and .Last) can be defined in appropriate ‘.Rprofile’
or ‘Rprofile.site’ files (note: as of R 4.1.0, assignments from
Rprofile.site must target the global environment, e.g. use
`assign(".First", function(){}, envir = globalenv())` rather than `.First
<- function(){}`) or have been saved in ‘.RData’.

I also recommend adding to Examples, under the Example of Rprofile.site
section, something to the effect of,

# Setting .First and .Last from within Rprofile.site requires
# assignment into the global environment for R versions
# 4.1.0 and later and is a best practice for earlier versions.
assign(".First", function() cat("\n   Welcome to R!\n\n"), envir =
globalenv())
assign(".Last", function() cat("\n   Goodbye!\n\n"), envir = globalenv())

	[[alternative HTML version deleted]]



More information about the R-devel mailing list