gistr is a light interface to GitHub’s gists for R.

See also:

  • rgithub an R client for the Github API by Carlos Scheidegger
  • git2r an R client for the libgit2 C library by Stefan Widgren
  • gistfo for turning your untitled RStudio tabs into gists!

Quick start

Install

Stable version from CRAN

Or dev version from GitHub.

devtools::install_github("ropensci/gistr")
library("gistr")

Authentication

There are two ways to authorise gistr to work with your GitHub account:

Using the PAT is recommended.

Using the gist_auth() function you can authenticate separately first, or if you’re not authenticated, this function will run internally with each function call. If you have a PAT, that will be used, if not, OAuth will be used.

Workflow

In gistr you can use pipes, introduced perhaps first in R in the package magrittr, to pass outputs from one function to another. If you have used dplyr with pipes you can see the difference, and perhaps the utility, of this workflow over the traditional workflow in R. You can use a non-piping or a piping workflow with gistr. Examples below use a mix of both workflows. Here is an example of a piping workflow (with some explanation):

file <- system.file("examples", "alm.md", package = "gistr")
gists(what = "minepublic")[[1]] %>% # List my public gists, and index to get just the 1st one
  add_files(file) %>% # Add a new file to that gist
  update() # update sends a PATCH command to the Gists API to add the file to your gist online

And a non-piping workflow that does the same exact thing:

file <- system.file("examples", "alm.md", package = "gistr")
g <- gists(what = "minepublic")[[1]]
g <- add_files(g, file)
update(g)

Or you could string them all together in one line (but it’s rather difficult to follow what’s going on because you have to read from the inside out)

file <- system.file("examples", "alm.md", package = "gistr")
update(add_files(gists(what = "minepublic")[[1]], file))

List gists

Limiting to a few results here to keep it brief

Since a certain date/time

Request different types of gists, one of public, minepublic, mineall, or starred.

Create gist

You can pass in files

Or, wrap gist_create() around some code in your R session/IDE, with just the function name, and a {' at the start and a }' at the end.

knit code from file path, code block, or gist file

knit a local file

knit a code block (knitr code block notation missing, do add that in) (result not shown)

knit a file from a gist, has to get file first (result not shown)

gists('minepublic')[[1]] %>% run() %>% update()

working with images

The GitHub API doesn’t let you upload binary files (e.g., images) via their HTTP API, which we use in gistr. There is a workaround.

If you are using .Rmd or .Rnw files, you can set imgur_inject = TRUE in gistr_create() so that imgur knit options are injected at the top of your file so that images will be uploaded to imgur. Alternatively, you can do this yourself, setting knit options to use imgur.

A file already using imgur

A file NOT already using imgur

Open a gist in your default browser

gists()[[1]] %>% browse()

Opens the gist in your default browser

Example use case

Working with the Mapzen Pelias geocoding API

The API is described at https://github.com/pelias/pelias, and is still in alpha they say. The steps: get data, make a gist. The data is returned from Mapzen as geojson, so all we have to do is literally push it up to GitHub gists and we’re done b/c GitHub renders the map.

And here’s that gist

pelias img

Meta

rofooter