The fastest way to return versioned code to RStudio

I use git as a version control system in RStudio. I have several different versions of the R script stored in git. Suppose I deleted a block of code, but now I decided that I want to paste the code into the current R script again. I know that the code includes the ddply function. This is my current workflow:

I open a terminal in RStudio and type:

 git grep ddply $(git rev-list --all) 

This causes hundreds of lines of code, for example:

 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count)) 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count)) 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count)) 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count)) 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count)) 

Sifting through the code and finally finding the bit I was looking for. I copy the corresponding bit of code from the terminal and paste it back into my R script. Before using the code, I need to remove this bit: 2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:

Right now this seems like a rather slow and awkward way to reuse old versioned code, and I might be better off using Rhistory.

Is there a faster way to get version-driven code from git and back to an R script in RStudio?

+7
source share
1 answer

RStudio's interface for Git is extremely simple and does not provide a direct method for working with history outside of a simple revert , as well as with branches that go beyond switching.

If all you know is that ddply exists in a function that you now want to reuse, your search method is really the best you can do, or perhaps the -G grep flag for git log . If you know something else (that is, some value of the time period, the name of the file or path, or the branch on which you performed this work), you can do it much easier by filtering the commit log with some criteria.

Once you find the commit, you can simply interactively check the patch fragments using

git checkout -p

and use it just as if you were using interactive commit. This allows you to split the pieces to get to the part you want. Or, if you really want to paste the code again using cut and paste, then git show && copy / paste.

Do not forget that you are not locked only in the RStudio interface. There are many easy-to-use graphical interfaces for Git that allow you to work with history and look for diagrams of explicit fragments in it, and none of them will interfere with Rstudio. All of them just work on the code, and the RStudio interface will be updated as needed.

Git Interfaces

I personally partially relate to the terminal, the gitGUI and gitk commands, TortoiseGit and Git Extensions.

+2
source

All Articles