Comparing files

Hi, all,

is there any RStudio Addin to compare two files (.R or .Rmd)? I tried addinslist by Dean Attali, but I couldn't find anything. I know I could just build a GitHub repo and then use the Version Control feature to access Git file diff. But the point is, sometime I don't want to compare two different versions of the same file: I may just want to compare two different .Rmd files, because I want to see how the initial options differ among them (for example).

Also, while I do use version control for any serious project, sometimes I just want to have a little fun with R without the need to build a Git repo :slight_smile: I would welcome a feature which allows me to diff .R/.Rmd files independently from version control.

What operating system are you on? If you're on Mac or a Linux flavour (e.g. Ubuntu) you can use the diff command line program to compare two files line-by-line. It might be available on Windows, too (e.g. in Windows 10 bash console or Cygwin, or as part of GNU Tools for Windows).

1 Like

Not an RStudio solution, but Meld is a great visual diff tool and works most places: http://meldmerge.org/

2 Likes

You can look for difftool or mergetool like diffmerge, p4merge, kdiff, and others.
These tools helps you compare text files and even help merged them by helping to solve conflicts.

1 Like

@jim89 I'm running on Windows 10

@cderv I'm a big fan of DiffMerge & WinMerge. Sure, I can use these tools, but I was thinking that including diff capabilities in a IDE or text editor could be useful (see PyCharm or Sublime Text 3). On the other hand, it's true that introducing too many features in a tool makes maintenance harder, so maybe it's better if I continue to use an external tool.

In my opinion, a tool doesn't need to come with every functionality that exists and are cool if it integrates well with others solutions. And RStudio IDE integrates well with others for sure.

In the case of diff tool, Git integrates well with those, and RStudio integrates with GIT so you could leverage that using git command to launch a diff between 2 files. Also, RStudio IDE includes a diff viewer between files when you commit. I did not try to activate it for any comparison... do not know if it is possible...

Moreover, as RStudio has a great terminal now, it is also easy to call for your difftool with a command line. If useful, you can make a small R function that take 2 file paths and launch the third party app with an addin for better user experience.

Finally, you also have an R :package: for this that could help you but I did not try it myself.


It uses the javascript library codediff.js. Contrary to other difftools, you does not need to install one. It seems included inside the package. You can give it a try and tell us if it is interesting.

3 Likes

@cderv I definitely agree with you that "a tool doesn’t need to come with every functionality that exists". You correctly point to the integration with Git, but as far as I understand it (please correct me if I'm wrong), to use Git inside RStudio you must:

  1. create a local repository, at least (I don't think a remote repository is needed too)
  2. you can only compare files having the same name, i.e., two different commits of the same repo, but not two text files with different names

My use case was a bit more "casual" than that, but sure, I can use WinMerge, DiffMerge or try diffr. Thanks for the useful suggestions!

Just to add to what @cderv alluded to, you could actually run the GNU diff used by git in a terminal in RStudio. Open up a terminal, diff file1 file2, and you should get a comparison between the two. It should already be installed due to using git (and I think the default configuration in git will make it available without needing to use include the directory of the executable in the terminal call), but has the disadvantage of the output just being a screen of text that will hide the console.

2 Likes

@nick, this also sounds nice. I will try it! Thanks

as far as I understand it (please correct me if I’m wrong), to use Git inside RStudio you must:

  1. create a local repository, at least (I don’t think a remote repository is needed too)
  2. you can only compare files having the same name, i.e., two different commits of the same repo, but not two text files with different names

Actually, neither of those is true if you use the command-line git.

  1. If you have Git installed, you can use its diff functionality on files that aren't part of any repository - just use the --no-index option. I do this fairly frequently, just because I like the colorized (and sometimes word-level) output, and because I have git diff configured to use a pager without explicitly piping to less or similar.

  2. When using --no-index, you can diff two files just by listing both filenames on the command line.

So I'd recommend:

git diff --no-index file1.Rmd file2.Rmd

-- the only disadvantage of which is that you have to leave the Rstudio graphical environment.

2 Likes

Notepad++ with compare plugin.

2 Likes

@KenWilliams thanks, that's a nice tip. It would be interesting to see your git diff configuration, if that's ok for you. It's not a graphical solution, but thanks to the terminal this could all be done inside RStudio.

@samuelhuerga yeah, I guess I'll just continue to use a tool external to RStudio. I prefer Sublime Text 3 for diffing, but Notepad++ + Compare plugin is nice too.

@KenWilliams on second thoughts, better to use git outside the RStudio terminal - on Windows, you get colorization only in the git bash terminal but not in the standard Windows (cmd) terminal.

The RStudio terminal can be configured to use the Git bash, which will be colorized. I think it was the default for me, but if it isn't for you, it's under Global Options, Terminal.

1 Like

Excellent! I'll try that then. Thanks!

Here's my relevant setup:

[color]
        diff = auto
        status = auto
[core]
        pager = less -S
[log]
        decorate = short
        date = relative
[merge]
        conflictstyle = diff3
[diff "xlsx"]
        binary = true
        textconv = xlsx2csv
[diff "gz"]
        textconv = gzcat
        binary = true
[alias]
        lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'
        pu■■■■good = push --force-with-lease

I only included those last two aliases because they're way too useful not to broadcast as widely as possible. :grin: Not directly related to the 'diff' question.

1 Like

It would be great to have something like meld directly integrated on RStudio.
That way we wouldn't need to switch to external tools and we could easily compare two tabs.

1 Like

Hey, from Global Options, Terminal, I can also configure the RStudio terminal to use the Windows Subsystem for Linux. Great!!!