How to declare constants in R

7

When I was playing the example of the " How to transform a string in Date format in R? " the command

tab<-readHTMLTable(u,header=T,skip.rows=1)

failed. The error happened because in my environment it is natural to assign the T value of the period I am analyzing. For this reason, in my environment, T is not synonymous with TRUE and the command fails.

This made me realize that I run the serious risk of accidentally entering pi = 7 , which would lead to serious analysis errors.

The question is how to declare pi as a constant?

    
asked by anonymous 06.03.2014 / 15:46

1 answer

9

You can use the lockBinding function:

For example:

pi <- base::pi
lockBinding("pi", globalenv())

This will lock the variable pi in the constant base::pi internal in the global environment. So if you try to define a new variable pi in the global environment with another value, the system will not let:

pi <- 5
Error: cannot change value of locked binding for 'pi'
    
06.03.2014 / 16:01