5

I think this must be a duplicate but I can't seem to find the answer on stack. Is there a way to compare two R scripts in the same wd to see if they are identical?

Something like:

a <- source("script1.R")
b <- source("script2.R")

identical(a, b)

I don't just mean the functions in each script but all the other things like comments etc.

Thanks

Pete900
  • 2,016
  • 1
  • 21
  • 44
  • 3
    I think such things are better handled by the OS. In Linux systems, for instance, there is a command `$diff script1.R script2.R`. Or you can use a more user-friendly program with a graphical interface such as [meld](http://meldmerge.org/) for the comparison of files, which is available for several platforms. – RHertel Mar 31 '16 at 07:55
  • `identical(a, b)`will compare the outputs of the last lines of code in the two files, but not the scripts themselves. So, your code will definitely not be useful to figure out whether two scripts are identical. Especially, comments are irrelevant to what is compared. I think you should follow @RHertel's advice. – Stibu Mar 31 '16 at 07:57
  • Ok, thanks guys. I'm glad that it wasn't something simple I was missing. – Pete900 Mar 31 '16 at 08:00
  • 2
    If you want to stick to an `R` solution, though, it might be better to read in the files as text and compare these. See e.g. `?readLines` or the various other ways of reading in text files. In this case you would compare long character strings. – coffeinjunky Mar 31 '16 at 08:00
  • Ah yes, that did seem to work upon first inspection. Do you want to post an answer so I can accept. – Pete900 Mar 31 '16 at 08:04
  • 2
    I suspect the answer to your actual problem is recommending version control systems such as git or svn. – Roland Mar 31 '16 at 08:06
  • @Roland, I am yet to try git. Can you load up two scripts are compare differences? – Pete900 Mar 31 '16 at 08:10
  • If you used version control you probably wouldn't have two files with different names, but possibly identical content. And since git compares files all the time, I assume it could do this, but I haven't used it yet for only comparing files. As the first comment points out, there are more convenient ways. – Roland Mar 31 '16 at 08:14

2 Answers2

13

I made three files (each ending with a newline):

iscript.R

script

iscript2.R

script

niscript.R

not script

Using the md5sum function from tools, I got the hash of :

tools::md5sum(c("iscript.R", "iscript2.R", "niscript.R"))
                         iscript.R                         iscript2.R                         niscript.R 
"95d26f42dccb2ec048a30261e0e2863f" "95d26f42dccb2ec048a30261e0e2863f" "d4bef1be4af7baedd2d69e649feb01d1" 

The files with the same hash are identical.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • Thank you, this also worked. I guess I could store the hash for each script as a variable, then use 'identical' to compare them. – Pete900 Mar 31 '16 at 08:07
0

In case you need to compare two or three scripts side-by-side.

You can use open source software like WinMerge to get a one-to-one visual comparison of the actual text in a script. You will need to save the scripts text files to use as inputs

More info can be found here: https://winmerge.org/downloads/?lang=en

Another alternative is to use SublimeText: Comparing the contents of two files in Sublime Text

Both tools allow you to print diff file to PDF.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31363550) – Dandelion Mar 29 '22 at 14:08