Dynamic variables in base R

后端 未结 4 1864
闹比i
闹比i 2020-12-21 05:12

How to create please dependent variables in R ? For example

a <- 1
b <- a*2
a <- 2
b
# [1] 2

But I expect the result 4. How can R

相关标签:
4条回答
  • 2020-12-21 05:27

    Warning: This isn't a good idea and task callbacks really should only be used if you know what you're doing.

    You can do something like this but it's tedious and there are better ways to achieve your goal. You can make a function that will be called after every top level evaluation that basically does the reassignment for you.

    modified <- function(expr, value, ok, visible){
      if(exists("a")){
        assign("b", a*2, env = .GlobalEnv)
      }
      return(TRUE)
    }
    
    addTaskCallback(modified)
    

    After running that you should be able to get this...

    > a
    Error: object 'a' not found
    > b
    Error: object 'b' not found
    > a <- 2
    > a
    [1] 2
    > b
    [1] 4
    > a <- 3
    > a
    [1] 3
    > b
    [1] 6
    

    Note that if you want to emulate a spreadsheet it would probably just be better to define a function to take your input and do all the necessary calculations to get your desired output. R isn't Excel and it would be best if you don't treat it like Excel.

    0 讨论(0)
  • 2020-12-21 05:38

    R doesn't work like that. Variables only change when assigned new values. This is a good thing, because it means things don't change magically. Suppose in 20 lines time you want to know the value of b? When did it change? What does it depend on?

    R is not a spreadsheet.

    Just to spell it out a bit more.

    sales = 100
    costs = 90
    profit = sales - costs
    

    now profit has the value 10.

    sales = 120
    

    Only sales has changed.

    profit = sales - costs
    

    That changes profits to 30.

    If you have a complex calculation you would normally write a function:

    computeProfit = function(sales, costs){return(sales - costs)}
    

    and then do:

    profit = computeProfit(sales, costs)
    

    whenever you want to compute the profits from the sales and the costs.

    0 讨论(0)
  • 2020-12-21 05:40

    Although what you want to do is not completely possible in R, with a simple modification of b into a function and thanks to lexical scoping, you actually can have a "dependent variable" (sort of).

    Define a:

    a <- 1
    

    Define b like this:

    b <- function() {
        a*2
    }
    

    Then, instead of using b to get the value of b, use b()

    b() ##gives 2
    a <- 4
    b() ##gives 8
    
    0 讨论(0)
  • 2020-12-21 05:45

    It sounds like you're looking for makeActiveBinding

    a <- 1
    makeActiveBinding('b', function() a * 2, .GlobalEnv)
    b
    # [1] 2
    a <- 2
    b
    # [1] 4
    

    The syntax is simpler if you want to use Hadley's nifty pryr package:

    library(pryr)
    b %<a-% (a * 2)
    

    Most people don't expect variables to behave like this, however. So if you're writing code that others will be reading, I don't recommend using this feature of R. Explicitly update b when a changes or make b a function of a.

    0 讨论(0)
提交回复
热议问题