Search code examples
rfunctionglobal-variables

Access global variable within function


I have two data.frames df1 and errors and a function add_to_errors(). The argument of the function is a vector of row-numbers (of df1) and the function should add add the row selected to a new data.frame called errors.

df1 <- data.frame(id=1:10,
                  var1=rep(NA, 2),
                  var2=rep(NA, 2))

errors <- data.frame()

add_to_errors <- function(ids){
    rows_to_add <- df1[ids,]
    errors <- rbind(errors, rows_to_add)
    return(errors)
} 

add_to_errors(c(1,2))
add_to_errors(c(6,7))

When I execute add_to_errors(1, 2) and add_to_errors(6,7), it looks as if errors was overwritten each time the function is called. How can I access errors as a global variable within the function?

The output should look like this:

  id var1 var2
1  1   NA   NA
2  2   NA   NA
3  6   NA   NA
4  7   NA   NA 

Solution

  • One way would be to use <<-, like this:

    add_to_errors <- function(ids){
      rows_to_add <- df1[ids,]
      errors <<- rbind(errors, rows_to_add)
      return(errors)
    } 
    
    add_to_errors(c(1,2))
    #  id var1 var2
    #1  1   NA   NA
    #2  2   NA   NA
    add_to_errors(c(6,7))
    #  id var1 var2
    #1  1   NA   NA
    #2  2   NA   NA
    #6  6   NA   NA
    #7  7   NA   NA
    

    Changing the global environment through <<- within a function is not considered good practice (this is called side effect and it's better to be avoided - if possible - as it creates bugs).