![A GitHub repo showing some README badge, including one that has an RStudio logo and says 'shiny' on one half and 'shinyapps.io' on the other half.](/post/2021-03-23-shiny-badge_files/randoflag-badges.png)
A shields.io badge built with {badgr} in the ‘randoflag’ repo on GitHub
tl;dr
Use the {badgr} package to make a clickable README badge for a repo that contains an R Shiny app:
Badgr badgr badgr
I made the {badgr} R package to take advantage of the full flexibility of shields.io—a service that builds README badges from a supplied URL string—from within R itself.1 You can find the source for the package on GitHub, visit its site built with {pkgdown}, or read a blog post about its inception.
Wait, what’s a README badge? It’s one of those little clickable buttons that provides at-a-glance info about a code repository, like its test coverage or build status.
Turns out you can make custom badges by building a special URL to shields.io. For example, I use custom badges to tell people that a repo has an associated blog post. This one goes to the post about {badgr}:
Recently I added a badge to each of my GitHub repositories that contain a Shiny app. The purpose is to let visitors:
- know that the repo contains a Shiny app
- know whether the app is hosted on the internet
- click the badge to go directly to the live app
I put out a tweet about this that got a little traction, so I figured it would be worthwhile to record the idea more permanently.
Install
First, you can install the {badgr} package from GitHub using the {remotes} package.
# install.packages("remotes")
remotes::install_github("matt-dray/badgr")
At time of writing, the package should ‘just work’, though it’s dependent ultimately on the shields.io service, of course. Leave an issue if you find something wrong.
Template
The code is in a GitHub Gist, should you want to access or bookmark it there. It’s a call to the get_badge()
function:
badgr::get_badge(
# Badge label
label = "Shiny", # left-side text
label_color = "white", # left-side colour
# Badge message
message = "shinyapps.io", # right-side text
color = "blue", # right-side colour
# Logo
logo_simple = "RStudio", # named icon from simpleicons.org
logo_color = "blue", # colour of simpleicons.org icon
# Markdown link
md_link = "https://matt.dray.shinyapps.io/randoflag/", # clickable link URL
# Convenience arguments
browser_preview = TRUE, # preview badge in your browser
to_clipboard = TRUE # copies markdown to paste into readme
)
# Opening browser to display badge preview
# Badge Markdown added to clipboard
# [1] "[![](https://img.shields.io/badge/Shiny-shinyapps.io-blue?style=flat&labelColor=white&logo=RStudio&logoColor=blue)](https://matt.dray.shinyapps.io/randoflag/)"
The output is a Markdown string that you can paste into your README. You can see the string is actually a link within a link: the URL to the shields.io badge is wrapped by a link to the Shiny app itself.
Conveniently, the to_clipboard = TRUE
argument copies the string to your clipboard and browser_preview = TRUE
opens a browser window with a preview of your badge in a new tab. You’ll notice these outcomes are referenced in the output from the function.
Pasting that string into your Markdown or R Markdown README results in this badge when rendered:
In this example, the badge is from the repo for ‘randoflag’, which is a guessing-game Shiny app hosted on shinyapps.io, which serves a random emoji flag. You can read about that app in an earlier blog post.
Note also that {badgr} is capable of incorporating bespoke icons, but we didn’t need to provide a custom RStudio logo because shields.io can easily display any icon that’s already part of simpleicons.org.
Variants
I think the badge is a useful at-a-glance recognition that the repo contains a Shiny app, whether it’s hosted or not, and a convenient clickable link to the app itself.
I think the wording on the right-hand side of the badge is a good place to indicate the app’s status. Text variants could, for example, be:
shinyapps.io
(the app is live on RStudio’s shinyapps.io service)not hosted
(the repo contains a Shiny app, but it’s not hosted online)not yet hosted
(the Shiny app in the repo is in development, but not yet live on the internet)
I’ve used all three of these so far, but you can use whatever text you want, really.
I’ve already got use out of custom Shiny badges for my repos. I look forward to seeing some more in the wild.
Bonus: American badger in the actual wild (Jonathunder, CC BY-SA 3.0)
Session info
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.0.4 (2021-02-15)
## os macOS Big Sur 10.16
## system x86_64, darwin17.0
## ui X11
## language (EN)
## collate en_GB.UTF-8
## ctype en_GB.UTF-8
## tz Europe/London
## date 2021-03-23
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date lib source
## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
## badgr * 0.1 2021-03-13 [1] Github (matt-dray/badgr@97ae19c)
## base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.0.0)
## blogdown 1.2 2021-03-04 [1] CRAN (R 4.0.2)
## bookdown 0.21 2020-10-13 [1] CRAN (R 4.0.2)
## cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.2)
## clipr 0.7.1 2020-10-08 [1] CRAN (R 4.0.2)
## digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2)
## evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.0)
## glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
## htmltools 0.5.1.9000 2021-03-11 [1] Github (rstudio/htmltools@ac43afe)
## knitr 1.31 2021-01-27 [1] CRAN (R 4.0.2)
## magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.2)
## rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.2)
## rmarkdown 2.6 2020-12-14 [1] CRAN (R 4.0.2)
## sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.0)
## stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2)
## stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
## withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.2)
## xfun 0.21 2021-02-10 [1] CRAN (R 4.0.2)
## yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
##
## [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library
See also on CRAN the {badger} R package from Guangchuang Yu, which has a number of pre-baked badges for simplicity.↩︎