2

I was trying to test equality of different R Objects and found that sometimes, when comparing objects in the wrong order, following error is occurring:

Error: C stack usage 7975620 is too close to the limit

Am I right that this is a Sign of too deep recursion?

It should be reproducible by following comparisons:

all.equal(mean, sd) # no error
all.equal(sd,mean) # Error: C stack usage 7975620 is too close to the limit
all.equal(NULL, mean) # no error
all.equal(mean,NULL) # Error: C stack usage 7975620 is too close to the limit 
all.equal(mean, sum); all.equal(sd, sum) # no Error
all.equal(sum,NULL) # no error
all.equal(sd, var) # no error
all.equal(var, mean) # Error: C stack usage 7975620 is too close to the limit
all.equal(var, NULL)  # Error: C stack usage 7975620 is too close to the limit

I am aware that the methods/functions I compared are implemented quite differently in R and there seems to be a pattern in comparison failure depending on how a given method is implemented, however I am wondering whether the behavior of the function is intended as such (I couldnt find a note on the order of objects to be compared within the documentation). I am also curious about it and would very appreciate if someone could explain this behavior to me.

I could also reproduce these issues within R --vanilla when from the terminal.

Session Info:

R version 4.1.3 (2022-03-10) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.4 LTS

Matrix products: default BLAS:
/usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=de_DE.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=de_DE.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils
datasets methods base

loaded via a namespace (and not attached): [1] compiler_4.1.3

Edit: Tried the code samples on Rstudio Server and could NOT reproduce above behavior. Outputs of all.equal Function also differ

Session Info:

R version 4.0.3 (2020-10-10) Platform: x86_64-suse-linux-gnu (64-bit) Running under: openSUSE Leap 15.2

Matrix products: default BLAS: /usr/lib64/R/lib/libRblas.so LAPACK: /usr/lib64/R/lib/libRlapack.so

locale: [1] LC_CTYPE=de_DE.UTF-8 LC_NUMERIC=C
LC_TIME=de_DE.UTF-8 LC_COLLATE=de_DE.UTF-8
LC_MONETARY=de_DE.UTF-8 [6] LC_MESSAGES=de_DE.UTF-8
LC_PAPER=de_DE.UTF-8 LC_NAME=C LC_ADDRESS=C
LC_TELEPHONE=C [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] stats graphics grDevices utils
datasets methods base

loaded via a namespace (and not attached): [1] compiler_4.0.3 tools_4.0.3

3
  • Share your sessionInfo(). I am getting "target, current do not match when deparsed" when I run all.equal(mean, sd) or all.equal(sd,mean) I am using R version 4.0.2
    – zx8754
    Mar 29, 2022 at 7:15
  • Just updated my Question with the session info. Your output would be the one I expected!. However when running all.equal(mean, sd) I get a very extensive list of mismatches (more than 1000 outputs)
    – Agnosie
    Mar 29, 2022 at 7:45
  • On Windows 11 I am also getting a very extensive list of mismatches, 2837. But when running all.equal(sd,mean) both Rstudio and Rscript hang. Mar 29, 2022 at 7:52

1 Answer 1

5

I followed the error for all.equal(sd,mean), it actually stems from the call all.equal.environment(environment(sd), environment(mean), ignore.environment = FALSE).

From the documentation of all.equal(), we see that the environment method has the extra argument evaluate which is a

logical indicating if “promises should be forced”

This defaults to true, and seems to cause the stack usage problem.

To fix it, just call all.equal(..., evaluate = FALSE):

all.equal(mean, sd, evaluate = FALSE)
#> [1] "target, current do not match when deparsed"                                                                                                   
#> [2] "names of environments differ: Lengths (1370, 1134) differ (string compare on first 1134) names of environments differ: 1134 string mismatches"

Created on 2022-03-29 by the reprex package (v2.0.1)

Results:

all.equal(mean, sd, evaluate = FALSE) # no Error
all.equal(sd,mean, evaluate = FALSE) # no Error
all.equal(NULL, mean, evaluate = FALSE) # no Error
all.equal(mean,NULL, evaluate = FALSE) # no Error
all.equal(mean, sum, evaluate = FALSE) # no Error
all.equal(sum,NULL, evaluate = FALSE) # no Error
all.equal(sd, var, evaluate = FALSE) # no Error
all.equal(var, mean, evaluate = FALSE) # no Error
all.equal(var, NULL, evaluate = FALSE)  # no Error
5
  • Thank You! This solves my Problem. Also, the argument check.environment = FALSE prevents the c stack error. Could you also explain why the order of the objects matter and do you know whether this is intended?
    – Agnosie
    Mar 29, 2022 at 7:57
  • Yes, check.environment = FALSE makes sure that all.equal.environment is not run at all, and thus you never get the problem. It's an easy solution if you don't need to compare the environments at all.
    – jpiversen
    Mar 29, 2022 at 8:23
  • A bit simplified: When you run all.equal() on two functions with check.environment = TRUE, all.equal() will check if environments are the same. This means that it will see if the base package contains the same functions as the stats package. When evaluate is also TRUE, R loops through the n first functions in both packages and compare them with all.equal (recursion, as you suspected). n is here the number of function in the package from where your first function is from - thus the order of the arguments matter.
    – jpiversen
    Mar 29, 2022 at 8:28
  • Since base has more functions than stats, this leads to more recursive calls, and thus the stack problem. Please let me know if I should update my answer with any of this information.
    – jpiversen
    Mar 29, 2022 at 8:28
  • 1
    No need for updating your Answer, Thanks a lot for your effort! I appreciate it a lot
    – Agnosie
    Mar 29, 2022 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.