如何正确重写 R Shiny 的函数 icon() 以包含 Font-Awesome Pro 图标?
How to correctly rewrite R Shiny's function icon() to include Font-Awesome Pro icons?
这是一个跟进问题,基于另一个提出相同问题的线程:。
我尝试了接受的答案,但它仅适用于免费图标。其他图标不渲染,也就是什么都不出现。
这是我从上面的链接线程重写自定义 my_icon()
、"forked" 的尝试,但没有成功。我的目标是正确计算专业图标 类 fas
、far
、fal
、fad
,假设 iconClass
应该是,例如,fas fa-alien
(如 https://fontawesome.com/icons/alien?style=solid)。
我的更改仍然没有效果。专业图标一直不出现。所以我一定是遗漏了一些基本的东西。
请注意,我将 ./www/shared
更改为 ./shared/
以避免警告:
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)
}
这个问题是一年前提出的,但如果其他人正在寻找答案:有一个模块 rstudio/fontawesome 可以解决这个问题。 shiny::icon()
实现在内部使用 fa_i()
,但如果您自己调用 fa_i()
,您应该能够使用“html_dependency
”参数添加“pro”图标。从文档中,参数:
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
.
这是一个跟进问题,基于另一个提出相同问题的线程:
我尝试了接受的答案,但它仅适用于免费图标。其他图标不渲染,也就是什么都不出现。
这是我从上面的链接线程重写自定义 my_icon()
、"forked" 的尝试,但没有成功。我的目标是正确计算专业图标 类 fas
、far
、fal
、fad
,假设 iconClass
应该是,例如,fas fa-alien
(如 https://fontawesome.com/icons/alien?style=solid)。
我的更改仍然没有效果。专业图标一直不出现。所以我一定是遗漏了一些基本的东西。
请注意,我将 ./www/shared
更改为 ./shared/
以避免警告:
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)
}
这个问题是一年前提出的,但如果其他人正在寻找答案:有一个模块 rstudio/fontawesome 可以解决这个问题。 shiny::icon()
实现在内部使用 fa_i()
,但如果您自己调用 fa_i()
,您应该能够使用“html_dependency
”参数添加“pro”图标。从文档中,参数:
Provides an opportunity to use a custom
html_dependency
object (created via a call tohtmltools::htmlDependency()
) instead of one supplied by the function (which uses Font Awesome's free assets and are bundled in the package). A customhtml_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 isNULL
where the function internally generates anhtml_dependency
.