Update variable value after overriding

Question for newbies to you R:

Random scenario:

  • I define the variable A: A = number

  • I define other variables based on A: B = number * A

  • I am changing the definition of A: A = another number

[Q]. How can I get R to automatically update the value of B without overriding it again?

etc .: 1. A = 1000; 2. B = A / 10; (B = 100) 3. Changed my mind: A = 1100 in the end;

>A 1100 >B 100 

B should be 110 (1100/10), but its value is not updated - therefore, it reads 100. Without overriding B, how can I update its value?

Thanks!

+4
source share
2 answers

Try the following:

 A <- 1000 makeActiveBinding("B", function() A/10, .GlobalEnv) B ## [1] 100 A <- 1100 B ## [1] 110 
+6
source

You suggest making B a function of A (and possibly the โ€œnumbersโ€ in this second expression)

 A=10 B <- function(Number=3.5) { A*Number } B() # [1] 35 A <- 15 B() # [1] 52.5 
+2
source

All Articles