Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are my R scripts identical?

Tags:

r

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

like image 612
Pete900 Avatar asked Mar 31 '16 07:03

Pete900


People also ask

How do I compare two codes in R?

The function comparedf() is used to compare two dataframes in R.

How do R scripts work?

R scripts are that solution. A script is simply a text file containing a set of commands and comments. The script can be saved and used later to re-execute the saved commands. The script can also be edited so you can execute a modified version of the commands.

What do you mean by Rscript file?

An R script is simply a text file containing (almost) the same commands that you would enter on the command line of R. ( almost) refers to the fact that if you are using sink() to send the output to a file, you will have to enclose some commands in print() to get the same output as on the command line.

How do I run a .R file?

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.


1 Answers

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.

like image 143
sebastian-c Avatar answered Oct 13 '22 01:10

sebastian-c