Using Gists to share Rscripts

Sharing code among collaborators is an important step in obtaining meaningful results. It is also helpful for troubleshooting non-working code. To share code effectively, GitHub has a “gists” feature that allows GitHub users to send and source code easily. In order to get started, you must first download the required packages.

First, download the package via BiocInstaller

if(require(BiocInstaller)==FALSE){
  source("https://bioconductor.org/biocLite.R")
  biocLite()
}
if(require(gistr)==FALSE){
BiocInstaller::biocLite(c("gistr", "dyplr"))
library(gistr)
library(dplyr)
}

Setting up your account to share and read Gists

GitHub requires an authentication token in order for you to read your gists from within R.

In order to generate a token, please see the short tutorial on GitHub.

Copy the token to your clipboard and add it to the a new line in the ~/.Renviron file. For Windows, this file may be called _Renviron. The line should look like as follows:

GITHUB_PAT=mytokenvalue

Pulling your gists from GitHub to R

Now that you have added the token to your R environment, the functions inside the gistr package can use it to pull your gists.

gists(what = "minepublic")
## [[1]]
## <gist>8c2292d3cf6ae82ed052
##   URL: https://gist.github.com/8c2292d3cf6ae82ed052
##   Description: Commands and code to run for TT repo
##   Public: TRUE
##   Created/Edited: 2015-07-01T23:33:41Z / 2015-08-29T14:24:06Z
##   Files: addGHrepo.txt
##   Truncated?: FALSE
## 
## [[2]]
## <gist>26a470449b8acc296ee9
##   URL: https://gist.github.com/26a470449b8acc296ee9
##   Description: Code for downloading TCGA data
##   Public: TRUE
##   Created/Edited: 2015-07-01T17:35:53Z / 2015-08-29T14:24:05Z
##   Files: exampleTutorial.Rmd
##   Truncated?: FALSE
## 
## [[3]]
## <gist>f5579c17e9951d09b702
##   URL: https://gist.github.com/f5579c17e9951d09b702
##   Description: getting started with gistr
##   Public: TRUE
##   Created/Edited: 2015-06-18T20:29:02Z / 2015-08-29T14:23:20Z
##   Files: gistR.R
##   Truncated?: FALSE
## 
## [[4]]
## <gist>f7a957019e2c7d556275
##   URL: https://gist.github.com/f7a957019e2c7d556275
##   Description: Code to check if all rows in columns are duplicates
##   Public: FALSE
##   Created/Edited: 2015-06-12T19:37:04Z / 2015-08-29T14:22:57Z
##   Files: isDups.R
##   Truncated?: FALSE
## 
## [[5]]
## <gist>180046ecbeaa1382ac1f
##   URL: https://gist.github.com/180046ecbeaa1382ac1f
##   Description: COAD Mutation Example
##   Public: FALSE
##   Created/Edited: 2015-06-10T21:19:58Z / 2015-08-29T14:22:53Z
##   Files: coadMUT.txt
##   Truncated?: FALSE
## 
## [[6]]
## <gist>0122cef6d984b041207f
##   URL: https://gist.github.com/0122cef6d984b041207f
##   Description: read.delim vs fread microbenchmark results
##   Public: FALSE
##   Created/Edited: 2015-03-13T20:17:58Z / 2015-08-29T14:17:04Z
##   Files: results.r
##   Truncated?: FALSE
## 
## [[7]]
## <gist>51f014838a023fc55f0c
##   URL: https://gist.github.com/51f014838a023fc55f0c
##   Description: Extract all caps between brackets PipeR
##   Public: FALSE
##   Created/Edited: 2015-03-06T21:08:53Z / 2015-08-29T14:16:41Z
##   Files: pipexpl
##   Truncated?: FALSE

Available in browser

gists(what = "minepublic")[[1]] %>% browse

Create and upload gist (from local file)

gist_create("isDups.R", description = "Code to check if all rows in columns are duplicates", public = FALSE)

Source the current gist by ID

The devtools package has a convenient function to source any gist from GitHub. In this example, I source my own gist. The first one in my list of Gists is sourced ([[1]]).

gists(what="minepublic")[[1]]$id %>% devtools::source_gist()

Update current gist

If you make further changes to your gist, you may want to update the uploaded gist from within R.

gists(what="minepublic")[[1]] %>% update_files("isDups.R") %>% update()

Here, I create another gist from a file that I had already worked on. I can add a description to the Gist and tell GitHub to knit the file. As an example, GitHub will not be able to knit an R file.

gist_create("~/Documents/SupportTCGA/exampleTutorial.r", description = "Code for downloading TCGA data", public = TRUE, knit=TRUE) 

In order to fix the previous error, I can update the files to the Gist with the update_files function and the correct file name. This time, GitHub will knit the document.

gists(what="minepublic")[[2]] %>% update_files("exampleTutorial.Rmd") %>% update(knit=TRUE)

If I create another gist, the first one in the list will be the last one I have uploaded. They are sorted from recent to old.

gist_create("addGHrepo.txt", description = "Commands and code to run for TT repo", public = TRUE) 
gists(what = "minepublic")[[1]] %>% update_files("addGHrepo.txt") %>% update

See your gist online!

In order to see your own gist, you will have to enter the appropriate GitHub username and the Gist ID.

baseURL <- "https://gist.github.com"
gitusername <- "LiNk-NY"
gistID <- gists(what="minepublic")[[1]]$id
browseURL(paste(baseURL, gitusername, gistID, sep = "/"))