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"
)