1

I created the following function:

math_funct <- function(a ,b){  
  c <<- a+b
  print(c)

  d <<- a-b
  print(d)

  e = c*d  
  print(e)  
}

And when i call it with input parameters 10 and 20, I get the following error about variable 'c':

math_funct(a=10, b=20)

Error in math_funct(a = 10, b = 20) : 
cannot change value of locked binding for 'c'

However, when i run the very same function replacing 'c' with arbitrarily selected variable name of 'k', it runs just fine.

math_funct <- function(a ,b){  
k <<- a+b
print(k)

d <<- a-b
print(d)

e = k*d  
print(e)  
}

math_funct(a=10, b=20)
[1] 30
[1] -10
[1] -300
  • Why are you using `<<-` instead of `<-` here – Dason Apr 27 '18 at 23:56
  • Because i was initially sourcing the multiplication 'e = c*d' from another script. And that's when i noticed the error 'cannot change value of locked binding for 'c' '. I agree that i don't need to use global assignment in this particular case but i am curious how the variable name would make a difference ? – Suraj Tata Prasad Apr 28 '18 at 00:05
  • 1
    Well c is already already defined. No offense but it seems you don't really understand what `<<-` is doing and I would suggest you don't use it. I suggest that for everybody but if you don't really know what it's doing then it's especially a bad idea. – Dason Apr 28 '18 at 00:24
  • I just looked into it and i believe <<- looks for pre-existing definitions for the variable name. And variable assignment happens only if this pre-existing definition's binding is not locked. But i would really appreciate any more information regarding the DOs and DON'Ts of variable assignment. This error was clearly not something i would have seen coming. – Suraj Tata Prasad Apr 28 '18 at 00:49
  • Seriously though. There is absolutely no need for a global assign here. Change it to a local and be on your way learning the lesson that global assign isn't what you want. – Dason Apr 28 '18 at 01:42
  • Thank you @Dason – Suraj Tata Prasad Apr 28 '18 at 04:26
  • `c` is a function. Others on this site have encountered this same issue when they tried to global assign something [t](https://stackoverflow.com/questions/22697392/t-as-a-name-of-the-global-variable-in-r-impossible) (function to transpose a matrix), [df](https://stackoverflow.com/questions/44702134/r-error-cannot-change-value-of-locked-binding-for-df) (density function for F-distribution), [sum](https://stackoverflow.com/questions/42869577/r-error-cannot-change-value-of-locked-binding) (sum function), etc. – Z.Lin Apr 28 '18 at 12:02

1 Answers1

1

When you do c <<- a+b, you're doing a global assignment, which in turn attempt to overwrite c which is originally the function for concatenating values into a vector or list.

That explains:

when i run the very same function replacing 'c' with arbitrarily selected variable name of 'k', it runs just fine.

And also explains the error you're getting: cannot change value of locked binding for 'c'. If you try and globally assign a value of 5 to t, another one of R's function (for transpose), you will get the same error.

t <<- 5
Error: cannot change value of locked binding for 't'
onlyphantom
  • 8,606
  • 4
  • 44
  • 58