Code Development: Diffing R Code

2024-04-18


One of the purposes of writing a function is to avoid repetition in our code. In order to identify repetition, including what changes and what stays the same, it is useful to be able to generate a “diff” of two sets of code.

For example, the following output shows differences between two pieces of code that calculate moving averages. We can see that only the size of the “window” (the size of the subset of the data over which the moving average is calculated) changes - 4 becomes 6 - plus the name of the symbol that holds the averages also changes - avg to avg7.

@@ 1,6 @@
@@ 1,6 @@
 
n <- length(b1temp)
 
n <- length(b1temp)
<
avg <- numeric(n - 4)
>
avg7 <- numeric(n - 6)
<
for (i in 1:(n - 4)) {
>
for (i in 1:(n - 6)) {
<
window <- i:(i + 4)
>
window <- i:(i + 6)
<
avg[i] <- mean(b1temp[window])
>
avg7[i] <- mean(b1temp[window])
 
}
 
}

The {diffobj} package provides some nice functions for generating “diff”s like this. The only difficulty is how we provide the code that we want to compare to {diffobj}:


Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.