Created
June 7, 2020 18:42
-
-
Save jimbrig/e5b6cce90fddd84a1d80f9b831fe8cd8 to your computer and use it in GitHub Desktop.
Generate a random fontawesome icon for quick prototyping. Can also use a "term" to limit random icon choice to a specific category.
This file contains hidden or 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
library(jsonlite) | |
library(fontawesome) | |
library(rlang) | |
library(purrr) | |
library(tidyverse) | |
# https://kapeli.com/cheat_sheets/Font_Awesome.docset/Contents/Resources/Documents/index | |
# https://github.com/rstudio/fontawesome/blob/master/R/fa.R | |
url <- "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/metadata/icons.json" | |
fa_list <- jsonlite::fromJSON(url) | |
icon_table <- fontawesome:::fa_tbl %>% | |
dplyr::mutate( | |
terms = ( | |
purrr::map_chr( | |
icon_table$name, | |
function(name) { | |
hold <- fa_list[[name]][["search"]][["terms"]] | |
if (length(hold) == 0) return(NA_character_) | |
knitr::combine_words(hold) | |
}) | |
), | |
terms = dplyr::coalesce(terms, "Unknown") | |
) | |
#' Random Icon Generator | |
#' | |
#' This function generates a random `fontawesome` icon for quick usage | |
#' while prototyping shiny apps. | |
#' | |
#' @param term (optional) term to limit number of icons available to a specific | |
#' category. For a list of available terms see the [icon_table]. | |
#' | |
#' @return An HTML icon element returned via [shiny::icon()] | |
#' @export | |
#' | |
#' @examples | |
#' random_icon() # should display in R Viewer | |
#' random_icon("animal") | |
#' shiny::fluidRow(random_icon()) | |
random_icon <- function(term = NULL, | |
class = NULL) { | |
icon_tbl <- icon_table # fontawesome:::fa_tbl | |
if (!is.null(term)) { | |
filt <- icon_tbl %>% | |
dplyr::filter(grepl(term, .data$terms)) | |
if (nrow(filt) == 0) { | |
warning(term, " not found. Reverting to full library of icons.") | |
icon_tbl <- icon_tbl | |
} else { | |
icon_tbl <- filt | |
} | |
} | |
row <- runif(n = 1, min = 0, max = nrow(icon_tbl) + 1) %>% trunc() | |
out <- icon_tbl[row, "name"] | |
message("Random Icon Selected: ", out) | |
shiny::icon(out, class = class) | |
} | |
random_icon() | |
random_icon("file") | |
terms <- icon_table$terms %>% | |
gsub(",", "", .) %>% | |
stringr::str_split(., pattern = " ") %>% | |
purrr::flatten_chr() %>% | |
unique() %>% | |
sort() | |
# HTML text | |
paste0('<i class="fa fa-', out, '"></i>') | |
# return as HTML/SVG | |
htmltools::HTML(icon_tbl[row, "svg"]) | |
# return as PNG | |
fontawesome::fa_png() | |
# shiny::icon | |
# function (name, class = NULL, lib = "font-awesome") | |
# { | |
# prefixes <- list(`font-awesome` = "fa", glyphicon = "glyphicon") | |
# prefix <- prefixes[[lib]] | |
# if (is.null(prefix)) { | |
# stop("Unknown font library '", lib, "' specified. Must be one of ", | |
# paste0("\"", names(prefixes), "\"", collapse = ", ")) | |
# } | |
# iconClass <- "" | |
# if (!is.null(name)) { | |
# prefix_class <- prefix | |
# if (prefix_class == "fa" && name %in% font_awesome_brands) { | |
# prefix_class <- "fab" | |
# } | |
# iconClass <- paste0(prefix_class, " ", prefix, | |
# "-", name) | |
# } | |
# if (!is.null(class)) | |
# iconClass <- paste(iconClass, class) | |
# iconTag <- tags$i(class = iconClass) | |
# if (lib == "font-awesome") { | |
# htmlDependencies(iconTag) <- htmlDependency("font-awesome", | |
# "5.13.0", "www/shared/fontawesome", package = "shiny", | |
# stylesheet = c("css/all.min.css", "css/v4-shims.min.css")) | |
# } | |
# htmltools::browsable(iconTag) | |
# } | |
# <bytecode: 0x000001f5fb68eeb0> | |
# <environment: namespace:shiny> | |
# library(knitr) | |
# | |
# | |
# reverse_words <- purrr::negate(~ knitr::combine_words) | |
# | |
# reverse_words("a, b, c, and d") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment