fzenoni
fzenoni

Reputation: 128

How to correctly rewrite R Shiny's function icon() to include Font-Awesome Pro icons?

This is a follow up question based on this other thread asking the same question: R Shiny: how to use fontawesome pro version with the icon function?.

I tried the accepted answer, but it works only for the free icons. Other icons are not rendered, that is, nothing appears.

Here is my unsuccessful attempt at rewriting the custom my_icon(), "forked" from the linked thread above. My goal is to correctly account pro icons classes fas, far, fal, fad, under the assumption that iconClass supposed to be, for example, fas fa-alien (as in https://fontawesome.com/icons/alien?style=solid).

Still my change has no effect. Pro icons keep not appearing. So I must be missing something fundamental.

Note that I changed ./www/shared into ./shared/ to avoid the warning:

Warning: Found subdirectories of your app's www/ directory that conflict with other resource URL prefixes. Consider renaming these directories: 'www/shared'

my_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) & is.null(class)) {
    prefix_class <- prefix
    iconClass <- paste0(prefix_class, " ", prefix, "-", name)
  } else if (!is.null(name) & !is.null(class)) {
    iconClass <- paste0(prefix, '-', name)
    iconClass <- paste(class, iconClass)
  }

  # print(iconClass)
  iconTag <- tags$i(class = iconClass)
  if (lib == "font-awesome") {
    htmlDependencies(iconTag) <- htmlDependency("font-awesome", 
                                                "5.13.0", "./shared/fontawesome", 
                                                stylesheet = c("css/all.min.css"))
  }
  htmltools::browsable(iconTag)
}

Upvotes: 2

Views: 888

Answers (1)

Andy
Andy

Reputation: 71

This question was asked a year ago, but in case anyone else is looking for an answer: there is a module rstudio/fontawesome for this. The shiny::icon() implementation uses fa_i() internally, but if you call fa_i() yourself you should be able to add the "pro" icons using the "html_dependency" argument. From the docs, the argument:

Provides an opportunity to use a custom html_dependency object (created via a call to htmltools::htmlDependency()) instead of one supplied by the function (which uses Font Awesome's free assets and are bundled in the package). A custom html_dependency object is useful when you have paid icons from Font Awesome or would otherwise like to customize exactly which icon assets are used (e.g., woff, woff2, eot, etc.). By default, this is NULL where the function internally generates an html_dependency.

Upvotes: 2

Related Questions