Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling print(ls.str()) in function affect behavior of rep

Tags:

r

rep

Begin a new R session with an empty environment. Write a series of functions with a parameter that is to be used as the value of the times parameter in a call to rep().

f <- function(n) {
  rep("hello", times = n)
}
f(x)

One expect this to fail, and indeed one gets:

# Error in f(x) : object 'x' not found

Modify the function a bit:

f2 <- function(n) {
  ls.str()
  rep("hello", times = n)
}

f2(x)

As expected, it still fails:

# Error in f2(x) : object 'x' not found

Modify a bit more (to see the environment in the console):

f3 <- function(n) {
  print(ls.str())
  rep("hello", times = n)
}

f3(x)

I still expect failure, but instead get:

## n : <missing>
## [1] "hello"

It is as if the call to print() makes rep work as though times were set to 1.

like image 624
Homer White Avatar asked Sep 18 '17 13:09

Homer White


1 Answers

I received earlier today a report that the bug has been fixed in R-devel and R-patched.

The issue was that the test for missingness in the R sources did not consider the case of an interrupted promise evaluation. A fix has been committed by Luke Tierney and can be seen on GitHub.

like image 142
Homer White Avatar answered Sep 30 '22 18:09

Homer White