Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
---
title: "Explore Github Topics"
author: "Bryan Hanson"
date: 2020-01-24
---
<!-- Following code to keep printed version cleaner; see https://stackoverflow.com/a/24467913/633251 -->
<style type="text/css" media="print">
a:link:after, a:visited:after {
content: normal !important;
}
</style>
```{r SetUp, echo = FALSE, eval = TRUE, results = "hide"}
keep <- "github_token"
rm(list = ls()[!(ls() %in% keep)])
suppressPackageStartupMessages(library("httr"))
suppressPackageStartupMessages(library("knitr"))
suppressPackageStartupMessages(library("kableExtra"))
opts_chunk$set(echo = FALSE)
```
```{r searchGHtopics}
search_terms <- c("NMR", "infrared", "Raman", "Ultraviolet", "Visible", "XRF", "spectroscopy")
nt <- length(search_terms)
results <- list()
for (i in 1:nt) {
search_string <- paste0("https://api.github.com/search/repositories?q=topic:", search_terms[i])
request <- GET(search_string, config(token = github_token))
stop_for_status(request) # converts http errors to R errors or warnings; use inside functions
results[[i]] <- content(request)
}
names(results) <- search_terms
# compute total number of results/items found
nr <- 0L
for (i in 1:nt) {
nr <- nr + length(results[[i]]$items)
}
```
```{r organizeResults}
DF <- data.frame(term = rep(NA_character_, nr),
repo_name = rep(NA_character_, nr),
repo_url = rep(NA_character_, nr),
stringsAsFactors = FALSE)
k <- 1L
for (i in 1:nt) {
ni <- length(results[[i]]$items)
for (j in 1:ni) {
DF$term[k] <- names(results)[[i]]
DF$repo_name[k] <- results[[i]]$items[[j]]$name
DF$repo_url[k] <- results[[i]]$items[[j]]$html_url
k <- k + 1L
}
}
DF <- DF[-which(duplicated(DF$repo_name)),]
```
```{r createTable}
namelink <- paste0("[", DF$repo_name, "](", DF$repo_url, ")")
DF2 <- data.frame(DF$term, namelink, stringsAsFactors = FALSE)
names(DF2) <- c("Search Term", "Link to Repo")
```
```{r printTable, results = "asis"}
options(knitr.kable.NA = '')
kable(DF2) %>%
kable_styling(c("striped", "bordered"))
```
@bryanhanson

This comment has been minimized.

Copy link
Owner Author

@bryanhanson bryanhanson commented Jan 25, 2020

To use the .Rmd you'll need to have a Github token in your R workspace. See this gist for how to set that up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment