OK - looks like there indeed is a way to achieve what you want (if what you want is what I think it is).
As your function f is simple enough and well behaved in the interval of interest, you can use "root" to find the zero of the derivative of f and assign the output globally directly to beta.crit.

Nevertheless I would still vote against using those global assignments the way you do 😉
You may soon reach a point where no workaround is available.
WE