Created
January 25, 2020 19:30
-
-
Save bryanhanson/e7736f4493571087438c6dfb353c6343 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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")) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use the
.Rmd
you'll need to have a Github token in yourR
workspace. See this gist for how to set that up.