-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfav.R
More file actions
57 lines (55 loc) · 1.6 KB
/
fav.R
File metadata and controls
57 lines (55 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' Use Font Awesome icons as favicons
#'
#' Generate the html necessary to use a Font Awesome icon as the favicon (the
#' icon that appears on browser tabs) for a shiny app or other HTML document.
#'
#' @inheritParams .fav_encode
#' @inheritDotParams fontawesome::fa
#'
#' @return A `shiny.tag` (see [htmltools::tag()]) that can be used to embed a
#' favicon in a shiny app or other HTML document.
#' @export
#' @examplesIf interactive()
#' html_page <- htmltools::tags$html(
#' fav("earth-africa", fill = "blue"),
#' htmltools::tags$body(
#' htmltools::tags$h1("Hello world!"),
#' htmltools::tags$p("(on the browser tab)")
#' )
#' )
#' htmltools::html_print(html_page, viewer = utils::browseURL)
fav <- function(name, ...) {
fav_base64 <- .fav_encode(name, ...)
fav_href <- .fav_as_href(fav_base64)
htmltools::tags$head(
htmltools::tags$link(rel = "icon", type = "image/png", href = fav_href)
)
}
#' Load Font Awesome icon and encode
#'
#' @inheritParams fontawesome::fa
#' @inheritDotParams fontawesome::fa
#'
#' @return A base64-encoded character vector representing the icon png.
#' @keywords internal
.fav_encode <- function(name, ...) {
jsonlite::base64_enc(
rsvg::rsvg_png(
charToRaw(
fontawesome::fa(name, ...)
),
width = 32,
height = 32
)
)
}
#' Add data URI prefix to base64-encoded icon
#'
#' @param fav_base64 A base64-encoded character vector generated by
#' [.fav_encode()].
#'
#' @return The base64-encoded icon with the data URI prefix.
#' @keywords internal
.fav_as_href <- function(fav_base64) {
paste0("data:image/png;base64,", fav_base64)
}