如何将图标或自定义图像添加到shinyWidgets的treeInput节点中?

huangapple go评论78阅读模式
英文:

How to add an icon or custom image to nodes in treeInput of shinyWidgets?

问题

这提供了一个基本的树结构。如何在这些节点上添加例如Font Awesome图标或相对文件路径中的自定义标志呢?

英文:

This gives a basic tree. How could I add for example font-awesome icons or custom flags saved in a relative file path to these nodes?

library(shiny)
library(shinyWidgets)

df <- data.frame(continent = c(rep("Europe", 2), rep("Asia", 2)),
                 country = c("France", "Germany", "China", "Japan"))

mytree <- create_tree(df, c("continent", "country"))

ui <- fluidPage(
  shinyWidgets::treeInput("mytree", "mytree", mytree)
)

server <- function(input, output, session) {}

shinyApp(ui, server)

答案1

得分: 3

  • 在R中,您可以在脚本中使用Unicode字符,参见Unicode问题
  • 使用emoji包,您可以在脚本中使用表情符号,详见cran emoji包
  • 在您的示例中,可以使用emoji中的flag函数来使用国旗表情符号。
  • 对于更复杂的任务,请考虑使用grImport2中实现的矢量图形。
library(shiny)
library(shinyWidgets)
library(emoji)

df <- data.frame(continent = c(rep("\U2727", 2), rep("\U24B8", 2)),
                 country = c(flag("France"), flag("Germany"), flag("China"), flag("Japan")))

mytree <- create_tree(df, c("continent", "country"))

ui <- fluidPage(
  shinyWidgets::treeInput("mytree", "mytree", mytree)
)

server <- function(input, output, session) {}

shinyApp(ui, server)

如何将图标或自定义图像添加到shinyWidgets的treeInput节点中?

英文:
  • In R you can use unicode characters in your script
    see this page Unicode question

  • With the emoji package you can do the same with emoji, see cran emoji package

    Flag emoji are suitable in your example using the flag function in emoji

  • For more complicated tasks consider vector graphics implemented in grImport2

library(shiny)
library(shinyWidgets)
library(emoji)

df &lt;- data.frame(continent = c(rep(&quot;\U2727&quot;, 2), rep(&quot;\U24B8&quot;, 2)),
                 country = c(flag(&quot;France&quot;), flag(&quot;Germany&quot;), flag(&quot;China&quot;), flag(&quot;Japan&quot;)))

mytree &lt;- create_tree(df, c(&quot;continent&quot;, &quot;country&quot;))

ui &lt;- fluidPage(
  shinyWidgets::treeInput(&quot;mytree&quot;, &quot;mytree&quot;, mytree)
)

server &lt;- function(input, output, session) {}

shinyApp(ui, server)

如何将图标或自定义图像添加到shinyWidgets的treeInput节点中?

答案2

得分: 1

如果使用其他包可以的话,{shinyTree} 是一个选择。

它可以使用 fontAwesome 图标或一组 {shinyTree} 内置图标。
您还可以使用 HTML 来设置项目标签,从而提供自己的图像来源。

请确保将静态内容(例如:图像文件)放在您的应用程序文件夹的子文件夹 www 中,以便 Shiny 可以找到它们。不要在路径中包含 www。例如:参考文件 www/flag_A.png 时,使用 &lt;img src = &#39;file.png&#39;&gt;

使用自定义图像的输出:

如何将图标或自定义图像添加到shinyWidgets的treeInput节点中?

  • 在这里找到国旗仓库 链接

  • 对于有很多符号的情况,将类添加到您的图像可能会更方便,例如 &lt;img class=&#39;flag&#39; ...&gt;,这样您可以通过 CSS 来应用大小、透明度等效果到所有的国旗 参考

英文:

If using another package is OK, {shinyTree} would be an option.

It can use fontAwesome icons or a set of {shinyTree} builtin icons.
You can also use HTML for item labels, thus offering to source your own images.

library(shiny)
library(shinyTree)

ui &lt;- fluidPage(
  shinyTree::shinyTree(&#39;myShinyTree&#39;,
                       checkbox = TRUE,
                       themeIcons = FALSE,
                       theme = &#39;proton&#39;
                       )
)

server &lt;- function(input, output, session) {
  df &lt;- data.frame(continent = c(rep(&quot;Letteria&quot;, 2), rep(&quot;Numberalia&quot;, 1)),
                   country = c(&quot;&lt;img src = &#39;flag_A.png&#39; width = &#39;50px&#39; /&gt; A-Land&quot;, 
                               &quot;&lt;img src = &#39;flag_B.png&#39; width = &#39;50px&#39; /&gt; B-Country&quot;,
                               &quot;&lt;img src = &#39;flag_1.png&#39; width = &#39;50px&#39; /&gt; Uni Kingdom&quot;
                               )
                   )

  output$myShinyTree &lt;-  renderTree({
    tree_list &lt;- dfToTree(df) ## dfToTree converts df into a hierarch. list
    attr(tree_list, &#39;stopened&#39;) &lt;- TRUE
    ## set the &#39;stopened&#39; attribute on each node to uncollapse the whole tree
    tree_list &lt;- tree_list |&gt;
      Map(f = \(.) {attr(., &#39;stopened&#39;) &lt;- TRUE; .})

  })
}

shinyApp(ui, server)

Remember to put static content (here: the image files) in a subfolder www of your app's folder, so that Shiny will find it. Do not include the www in your path though. Example: reference file www/flag_A.png as &lt;img src = &#39;file.png&#39;&gt;.

Output with custom images:

如何将图标或自定义图像添加到shinyWidgets的treeInput节点中?

  • find a repository of country flags here

  • With lots of symbols it might be convenient to add a class to your images, e.g. &lt;img class=&#39;flag&#39; ...&gt; so that you can apply size, transparency etc. to all flags via CSS.

huangapple
  • 本文由 发表于 2023年2月9日 00:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定