数据表中的图标闪亮 [英] Icons in data table in Shiny

查看:89
本文介绍了数据表中的图标闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一列图标添加到Shiny应用程序中显示的数据表中.本质上,我只想在数据上升时使用向上箭头,在数据下降时使用向下箭头.但是,我不确定如何显示图标.当我添加一列仅用于例如icon("arrow-up"),出现以下错误:

I am trying to add a column of icons to a data table that is shown in a Shiny app. Essentially I just want an up arrow when the data has gone up, and a down arrow when it has gone down. However, I'm not sure how to display an icon. When I add a column just using for e.g. icon("arrow-up"), I get the following error:

错误:列表"类型未实现默认方法

Error: default method not implemented for type 'list'

我可以看到,如果我在Shiny之外尝试这种方法,那么它将显示有关图标的数据,而不是显示图标.

I can see that if I try this approach outside of Shiny, it is displaying the data about the icon rather than displaying the icon.

一个选择可能是使用将其添加为图像的方法-但是感觉会有更直接的方法吗?我也不确定如何使用真棒字体"图标来做到这一点.

One option might be to use the approach of adding it as an image - but it feels like there would be a more direct way? I'm also not sure how to do that with Font Awesome icons.

很抱歉,如果这很简单-我找不到答案!

Apologies if this is basic - I couldn't find an answer!

这是我正在做的事情的简化版本:

Here is a simplified version of what I'm doing:

library(shiny)
library(shinydashboard)

number_compare <- data.frame(replicate(2, sample(1:100, 10, rep=TRUE)))
number_compare$direction <- ifelse(number_compare$X1 < number_compare$X2, "Up", "Down")
number_compare$direction <- ifelse(number_compare$X1 == number_compare$X2, "", number_compare$direction)


sidebar <- dashboardSidebar()

body <- dashboardBody(
  fluidRow(box(width = 12, solidHeader = TRUE,
               tableOutput("example_table"))
           )
  )

ui <- dashboardPage(dashboardHeader(title = "Example"),
                    sidebar,
                    body
)


server <- function(input, output) {
  output$example_table <- renderTable(number_compare)
}

shinyApp(ui, server)

推荐答案

您没有说要使用什么图标,因此我假设Fontawesome中的angle-upangle-down,但是您也可以使用字形图标.

You don't say what icons you want to use, so I will assume angle-up and angle-down from fontawesome, but you can also use glyphicons.

正如您所指出的,icon()的输出是一个列表.在ifelse中分配该值将使一列重复i,iconname和NULL值.

As you point out, the output of icon() is a list. Assigning that in your ifelse would give a column repeating the values i, iconname, and NULL.

相反,请尝试将icon()包装在as.character()中,以提供图标的原始HTML.例如:

Instead, try wrapping icon() in as.character() to give the raw HTML for the icon. E.g:

number_compare$direction <- ifelse(
    number_compare$X1 < number_compare$X2,
    as.character(icon("angle-up")),
    as.character(icon("angle-down"))
)

这将在列中填充类似<i class="fa fa-angle-up"></i>的值,您可以在应用程序中将它们打印为原始HTML.

This will fill the column with values like <i class="fa fa-angle-up"></i> which you can print in the app as raw HTML.

或者,您可以使用HTML转义码来打印箭头,而无需使用图标.有关列表,请参见 https://www.w3schools.com/charsets/ref_utf_arrows.asp HTML转义代码.

Alternatively, you can use HTML escape codes to print the arrows without using icons. See https://www.w3schools.com/charsets/ref_utf_arrows.asp for a list of HTML escape codes.

编辑:每当在表中包含原始HTML时,请确保闪亮不会逃避它.用renderTable(number_compare, sanitize.text.function = function(x) x)替换对renderTable(number_compare)的呼叫 (基于 r闪亮的表格未呈现html )

Whenever you include raw HTML in a table, make sure that shiny doesn't escape it. Replace your call to renderTable(number_compare) with renderTable(number_compare, sanitize.text.function = function(x) x) (based on r shiny table not rendering html)

这篇关于数据表中的图标闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆