Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check whether 2 R programs are identical

Tags:

r

Recently I learned that I can use identical or all.equal to check whether 2 data sets are identical.

Can I also use them to check whether 2 R programs are identical? Is there a better or more appropriate way than below?

program.1 <- readLines("c:/r stuff/test program 1.r")
program.2 <- readLines("c:/r stuff/test program 2.r")

identical(program.1, program.2)
all.equal(program.1, program.2)
isTRUE(all.equal(program.1, program.2))

Thank you for any thoughts or advice.

Here are the contents of the 2 test programs being compared:

a <- matrix(2, nrow=3, ncol=4)

b <- c(1,2,3,4,5,6,7,8,6,5,4,3,2)

table(b)

c <- runif(2,0,1)

a * b

# March 2012 Edit begins here #

Here is a small example program for which Josh's function below returns FALSE while identical and all.equal return TRUE. I name the two program files 'testa.r' and 'testb.r'.

set.seed(123)

y <- rep(NA, 10)

s <- matrix(ceiling(runif(10,0,100)), nrow=10, byrow=T)

a   <- 25
ab  <- 50
abc <- 75

for(i in 1:10) {
     if(s[i] >  a  & s[i] <= ab ) y[i] = 1
     if(s[i] >  ab & s[i] <= abc) y[i] = 2
}

s
y

Here is the R program I use to read the two files containing the above code.

program.1 <- readLines("c:/users/Mark W Miller/simple R programs/testa.r")

program.2 <- readLines("c:/users/Mark W Miller/simple R programs/testb.r")


identical(program.1, program.2)
all.equal(program.1, program.2)
isTRUE(all.equal(program.1, program.2))


parseToSame <- function(file1, file2) {
    a <- parse(file = file1)
    b <- parse(file = file2)
    attributes(a) <- NULL
    attributes(b) <- NULL
    identical(a,b)
}

parseToSame(

     "c:/users/Mark W Miller/simple R programs/testa.r",
     "c:/users/Mark W Miller/simple R programs/testb.r"

)
like image 714
Mark Miller Avatar asked Feb 27 '12 23:02

Mark Miller


1 Answers

Here is a function that might be slightly more useful, in that it tests whether the two files parse to the same expression tree. (It will thus find the code in two files to be equivalent even if they have different formatting, additional blank lines and spaces, etc., as long as they parse to the same object.)

parseToSame <- function(file1, file2) {
    a <- parse(file = file1)
    b <- parse(file = file2)
    attributes(a) <- NULL
    attributes(b) <- NULL
    identical(a,b)
}

Here's a demo of the function in action:

# Create two files with same code but different formatting
tmp1 <- tempfile()
tmp2 <- tempfile()
cat("a <- 4; b <- 11; a*b \n", file = tmp1)
cat("a<-4

     b    <-    11 
     a*b \n", file = tmp2)

# Test out the two approaches
identical(readLines(tmp1), readLines(tmp2))
# [1] FALSE
parseToSame(tmp1, tmp2)
# [1] TRUE
like image 88
Josh O'Brien Avatar answered Sep 28 '22 18:09

Josh O'Brien