Give rghcard a GitHub user name, and it draws the cards of that user: it reads their statistics from GitHub and draws them, in a few lines of R. This article shows how, for your user name or anyone’s: in the console, in a function of your own and in a Shiny app, where anyone can type a user name and see the card.
Draw the cards in R
installs rghcard. The cards in this article are those of
example-user, the made-up user of
example_stats(), so that the article runs without asking
GitHub; a user name brings cards like them.
A user name in, a card out
library(rghcard)
user <- "octocat" # your GitHub user name
stats <- collect_stats(user = user, dir = NULL)
view_card(stats_card(stats, file = NULL))collect_stats() reads the statistics of the user from
GitHub, and dir = NULL keeps nothing on disk.
stats_card() draws the statistics card, and
file = NULL returns it as SVG code instead of a file.
view_card() shows it in the Viewer pane of RStudio or
Positron, or in your web browser elsewhere, and
view_card(..., "plots") in the Plots pane. To ask for the
user name as the code runs, write
user <- readline("GitHub user name: ") instead.
The card shows the repositories of the user, their stars and forks, the downloads of their release files and their GitHub Actions runs, with the runs per day, the most starred repositories and the languages. The page views and the clones are missing: GitHub shows them only to the owner of the repositories, which the next section explains.
What a user name brings
What collect_stats() reads depends on the token that it
finds, a password that lets a program read from GitHub:
| No token | A token of yours | The user’s own token | |
|---|---|---|---|
| Repositories, stars, forks, languages, release downloads, GitHub Actions runs, followers | yes | yes | yes |
| Contributions, commits, pull requests, issues and reviews | yes | yes | |
| Page views, clones and visitors | yes | ||
| When each star came | yes | ||
| Requests that GitHub answers in an hour | 60 | 5,000 | 5,000 |
Without a token, the statistics card and the GitHub Actions card are complete. A token of yours, for any user name, adds the contributions card and the activity card: GitHub gives the contributions only to requests with a token. The visitors card, the traffic on the statistics card and the chart of the stars on the code card need the user’s own token, since GitHub shows the traffic of a repository, and when each star came, only to its owner and collaborators.
rghcard asks GitHub about five times for each repository, so without a token, 60 requests an hour cover about a dozen repositories; the statistics of the repositories past them are left out. A token takes a minute to make and save, once, and the gh package finds it by itself after that. With it, the contributions card draws too:
view_card(contributions_card(stats, file = NULL))For your own user name, your own token brings everything:
collect_stats() without user reads the
statistics of the owner of the token, and Draw the cards in R
goes through it, with a history that keeps the traffic beyond the 14
days that GitHub keeps.
Every card of the user
One collection is enough for every card. In the Plots pane, its arrows go from one card to the next:
for (card in list(stats_card, contributions_card, activity_card,
actions_card, code_card)) {
view_card(card(stats, file = NULL), "plots")
}The visitors card is left out: it draws the traffic, which only the user’s own token reads. More cards explains each card.
A function of your own
A function of a few lines turns a user name into a card:
user_card <- function(user, card = stats_card, ...) {
stats <- collect_stats(user = user, dir = NULL)
view_card(card(stats, file = NULL, ...))
}
user_card("octocat")
user_card("octocat", contributions_card, theme = "light")... passes the options of the card on, such as
theme, panels and title. Each
call reads GitHub again, so for several cards of one user, collect once,
as in the section before.
An app: type a user name, see the card
A Shiny app takes the user name from a text box and draws the card in the page:
library(shiny)
library(rghcard)
ui <- fluidPage(
tags$style("svg[role='img'] { max-width: 100%; height: auto; }"),
textInput("user", "GitHub user name", "octocat"),
actionButton("draw", "Draw the card"),
uiOutput("card")
)
server <- function(input, output, session) {
card <- eventReactive(input$draw, {
stats <- collect_stats(user = input$user, dir = NULL)
stats_card(stats, file = NULL)
})
output$card <- renderUI(HTML(card()))
}
shinyApp(ui, server)Each click reads GitHub for the user, which takes a few seconds,
longer for a user with many repositories; a user name that GitHub does
not know shows an error instead of the card. On a server, everyone who
uses the app shares its requests, so give the server a token: in the
GITHUB_PAT environment variable, in the
.Renviron file of the server, never in the code of the app,
which others may read.
Save the card
A file name instead of NULL saves the card, and its end
chooses the format:
stats_card(stats, "octocat-stats.svg")
stats_card(stats, "octocat-stats.png")Draw the cards in R compares the formats, and Get started puts your own cards on your GitHub profile, where the rghcard action refreshes them.