Get started puts the cards on your GitHub profile, where a GitHub Actions workflow draws them every week. rghcard plots the same cards in R, on your computer: this article collects the statistics of your GitHub account, keeps them as CSV files, and plots the cards from them as SVG or PNG files. Its code runs as it is in the console of RStudio, Positron or R.
Install rghcard
install.packages("rghcard", repos = c(
"https://sedzinfo.r-universe.dev",
"https://cloud.r-project.org"
))This also installs the packages that rghcard needs: gh, which reads from GitHub, and rsvg, which turns a card into a PNG. The README has the other ways to install it, and how to install R.
A token
GitHub shows the page views and clones of a repository only to those who can push to it, so rghcard reads them with a token of yours: a password that lets a program read from GitHub for you. The gh package, which rghcard uses, finds it by itself, if you have one. See whether you do:
gh::gh_whoami()It prints your user name and the scopes of your token, or says that
there is no token. Then create
one with this link, which fills in GitHub’s form: the name
rghcard and the scope repo, which the page
views and clones need. Choose when it expires, click Generate
token, copy the token, and give it to R:
gitcreds::gitcreds_set()It asks for the token and keeps it in the credential store of your
computer, where gh, and git, find it in every R session.
gitcreds_set() needs git; without it, put the token
into the file .Renviron of your home folder, which
file.edit("~/.Renviron") opens, as the line
GITHUB_PAT= followed by the token, and restart R. Never
write a token into a script, which others may read: anyone who has it
can act as you on GitHub. When the token expires, create another the
same way.
A fine-grained token, such as the one of the workflow in Get started, works too,
but GitHub then leaves out your private contributions, so the cards can
count fewer contributions than your profile does.
contributions_token of collect_stats() then
reads them with another token, such as a classic one.
Collect your statistics
library(rghcard)
stats <- collect_stats()
statscollect_stats() reads the repositories you own, the
public ones that are not forks, and your contributions since you joined
GitHub: a minute or two for a few dozen repositories. It returns the
statistics, for the cards to draw, and keeps them as CSV files in the
folder rghcard-data of the working directory, which
getwd() shows. There is a file for each of the 14 tables of
the statistics, and meta.csv, with your user name, the day
of the collection and the day you joined GitHub. These are the files of
the made-up user that this article draws:
list.files(system.file("extdata", "example", package = "rghcard"))
#> [1] "actions.csv" "clones.csv" "commits.csv" "contribution_types.csv" "contributions.csv" "languages.csv" "meta.csv"
#> [8] "pages.csv" "profile.csv" "referrers.csv" "releases.csv" "repos.csv" "stars.csv" "traffic.csv"
#> [15] "views.csv"A spreadsheet opens them, and read_stats() reads them
back into R, without asking GitHub, as in a new R session:
stats <- read_stats("rghcard-data")GitHub keeps the page views and clones for 14 days only, so collect
at least once a week, into the same folder: each collection adds the
days since the one before, and the history keeps them all.
include_private = TRUE and
include_forks = TRUE add your private repositories and your
forks; dir chooses another folder, and
dir = NULL keeps nothing on disk. The help page of
collect_stats() describes every argument, and Explore the data goes through the
tables.
The six cards
Each card has its function, which plots it into a file: an SVG file
when the name of the file ends in .svg, and a PNG file when
it ends in .png. Without a file name, it writes a PNG into
the working directory, such as rghcard-stats.png. Without
the statistics either, it reads them from the history in
rghcard-data, so after a collection,
plot_stats_card() alone plots the statistics card. The
cards here are those of get_example_stats(), the statistics
of a made-up user, so that they plot without a token:
library(rghcard)
stats <- get_example_stats()The help page of each function explains each number and chart of its card.
Contributions
The contributions, commits and streaks of the user, and the languages they commit in:
plot_contributions_card(stats, "rghcard-contributions.svg")Statistics
The stars, page views, clones, release downloads and GitHub Actions runs of their repositories:
plot_stats_card(stats, "rghcard-stats.svg")Activity
Their pull requests, issues, reviews and followers, and their contributions by type and by day of the week:
plot_activity_card(stats, "rghcard-activity.svg")Visitors
The unique visitors and cloners of the last 14 days, the sites that sent them and the pages they viewed:
plot_visitors_card(stats, "rghcard-visitors.svg")GitHub Actions
Their GitHub Actions runs: how many succeeded and failed, how long they took, and the busiest workflows:
plot_actions_card(stats, "rghcard-actions.svg")Code
The size of their code in each language, their largest repositories, their stars month by month, and their forks and watchers:
plot_code_card(stats, "rghcard-code.svg")SVG, PNG or SVG code
The end of the file name chooses the format:
-
.svg: sharp at any size, and about a tenth of the size of a PNG. Its text takes the fonts of the screen that shows it, as GitHub’s own pages do. -
.png: a picture, for places that take no SVG files, such as some social networks and slides. Its text takes the fonts of your computer, and it has twice as many pixels aswidth, so that it stays sharp on high-resolution screens.
plot_stats_card(stats, "rghcard-stats.svg")
plot_stats_card(stats, "rghcard-stats.png")A file name may hold a folder, such as
"cards/rghcard-stats.svg", and a folder that does not exist
yet is created. file = NULL writes no file but returns the
SVG code, to put the card into a web page that R makes, such as an R
Markdown or Quarto document, as this article does, or a Shiny app:
# in a chunk of an R Markdown or Quarto document
knitr::raw_html(plot_stats_card(stats, file = NULL))
# in the user interface of a Shiny app
shiny::HTML(plot_stats_card(stats, file = NULL))A card is 900 pixels wide, so on a narrower screen it needs this CSS, as in this article, to shrink with the page:
See a card in RStudio or Positron
view_card() shows a card in the Viewer pane of RStudio
or Positron:
view_card("rghcard-stats.svg")The Viewer shows it as a web page: sharp at any size, shrunk to fit
the pane, and telling the value of each column and bar when you point at
it. In R on its own, view_card() opens your web browser
instead. The Plots pane shows the card as a picture, as a GitHub profile
does, without the values:
view_card("rghcard-stats.svg", "plots")view_card() takes what the card functions return, so it
also shows a card as it is plotted, into a file or, with
file = NULL, without one:
view_card(plot_stats_card(stats, "rghcard-stats.svg"))
view_card(plot_stats_card(stats, file = NULL), "plots")Choose the charts
Under its key figures, a card has one to three charts, which
panels chooses. Any card can show any of the 23 charts,
whichever card they come from; its key figures stay its own. One chart
takes the whole width of the card:
plot_stats_card(stats, "rghcard-views.svg", panels = "views")Two or three share it, in the order given. Here the code card shows the bytes of code per language next to the commits per language, a chart of the contributions card:
plot_code_card(stats, "rghcard-code.svg",
panels = c("code_languages", "commit_languages"))These are the charts, under the card that shows each of them by default, whose help page explains them:
| Card | Chart | What it shows |
|---|---|---|
plot_contributions_card() |
"contributions" |
Contributions per month, in the last 12 months |
"commit_languages" |
Commits per language, as a ring | |
"languages" |
Repositories per main language, as a ring | |
plot_stats_card() |
"views" |
Page views per day |
"clones" |
Clones per day | |
"actions" |
GitHub Actions runs per day | |
"top_viewed" |
The five repositories with the most page views | |
"top_cloned" |
The five repositories with the most clones | |
"top_starred" |
The five repositories with the most stars | |
plot_activity_card() |
"contribution_types" |
Contributions by type, as a ring |
"weekdays" |
Contributions per day of the week, in the last 12 months | |
"top_committed" |
The five repositories with the most commits of the user | |
plot_visitors_card() |
"visitors" |
Unique visitors per day |
"referrers" |
The five sites that sent the most page views | |
"top_pages" |
The five most viewed pages | |
plot_actions_card() |
"top_workflows" |
The five workflows with the most runs |
"run_time" |
The five repositories whose runs took longest | |
"top_failed" |
The five workflows with the most failed runs | |
plot_code_card() |
"code_languages" |
Bytes of code per language, as a ring |
"stars_history" |
Stars at the end of each month, in the last 12 months | |
"largest" |
The five repositories with the most code | |
"top_forked" |
The five repositories with the most forks | |
"top_watched" |
The five repositories with the most watchers |
A name that rghcard does not know stops the card, with the list of them all:
plot_stats_card(stats, "rghcard-stats.svg", panels = "view")
#> Error:
#> ! `panels` must be one to three of: views, clones, actions, top_viewed, top_cloned, top_starred, languages, contributions, commit_languages, contribution_types, weekdays, top_committed, visitors, referrers, top_pages, top_workflows, run_time, top_failed, code_languages, stars_history, largest, top_forked, top_watched.Theme, days, title and size
These options change a card:
-
theme = "light"plots the card in the colors of GitHub’s light mode, and"dark", the default, in those of its dark mode. -
dayssets the days of the page views, clones and runs ofplot_stats_card()and of the runs ofplot_actions_card(): the last 30 by default, up to the day before the collection, the last day that GitHub had finished counting. -
titlereplaces the user name and the name of the card at the top left. -
widthis the width of the card in pixels, 900 by default; its height follows.
plot_actions_card(stats, "rghcard-actions-light.svg", theme = "light",
days = 14, title = "My workflows", width = 700)Another user’s cards
collect_stats() also reads the statistics of any other
GitHub user:
octocat <- collect_stats(user = "octocat", dir = NULL)
plot_contributions_card(octocat, "octocat-contributions.svg")GitHub shows the traffic of a repository only to its owner, so the cards of another user have no page views, clones or visitors, and the statistics card shows other figures and charts in their place. Without a token, GitHub answers 60 requests an hour, enough for the public statistics of a few repositories: each takes about four, and one with stars one more for every 30 weeks since it was created. A token of yours allows 5,000, and adds the contributions, which GitHub gives only to requests with a token.