[R] puzzling behaviour of identical function

Duncan Murdoch murdoch.duncan at gmail.com
Sat May 30 21:56:30 CEST 2015


On 30/05/2015 10:35 AM, Munawar Cheema wrote:
> I am puzzled by the following seemingly contradictory calls to the function
> identical. Below is a minimal example, that reproduces them:
> 
>> sessionInfo()
> R version 3.2.0 (2015-04-16)
> Platform: x86_64-apple-darwin13.4.0 (64-bit)
> Running under: OS X 10.9.5 (Mavericks)
> 
> locale:
> [1] C
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  grid      methods
> [8] base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.2.0 tools_3.2.0    memoise_0.2.1  digest_0.6.8
>> readLines("tmp.R")
> [1] "if (identical(function(x)1,function(x)1)) cat(\"Identical!\\n\")
> else cat(\"Not Identical!\\n\")"
>> sys.source("tmp.R", envir=new.env(parent=baseenv()))
> Identical!
>> if (identical(function(x)1,function(x)1)) cat("Identical!\n") else cat("Not Identical!\n")
> Not Identical!
> 
> 
> 
> Can anyone explain why I get a false value from the command line but true if I
> use sys.source?
> 

sys.source has keep.source = getOption("keep.source.pkgs"), which
defaults to FALSE.  Normal evaluation uses getOption("keep.source"),
which defaults to TRUE.

You can see the difference if you save the functions, and use str(), e.g.

f <- function(x)1
g <- function(x)1

str(f)
str(g)

Duncan Murdoch



More information about the R-help mailing list