[R] puzzling behaviour of identical function

William Dunlap wdunlap at tibco.com
Sat May 30 21:49:51 CEST 2015


Their 'srcref' attributes differ.  srcref describes the text that
the parser was given when creating the function.

> identical(f1 <- function(x)1, f2 <- function(x)1)
[1] FALSE
> str(f1)
function (x)
 - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 17 1 28 17 28 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment:
0x00000000166c02a0>
> str(f2)
function (x)
 - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 37 1 48 37 48 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment:
0x00000000166c02a0>

You can remore the the srcref attribute with attr(f1, "srcref")<-NULL and
you can
avoid having it attached to functions when they are created by setting
options(keep.source=FALSE).



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, May 30, 2015 at 7:35 AM, Munawar Cheema <munawar.a.cheema at gmail.com>
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?
>
> --
> Kind Regards
> Munawar Cheema
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list