在R Shiny中,如何自动显示图像而不是文本?

时间:2019-08-04 11:37:09

标签: r shiny

使用此数据,我创建了一个矩阵,现在需要用徽标替换列名和行名中的文本,以得到附加的结果image 这是一些徽标图标 cartv, 请使用此data

代码如下:

library(shiny)
library(DT)

testmatrix <- readxl::read_xlsx("testmatrix.xlsx")

ui <- fluidPage(
dataTableOutput("myTable")
)
server <- function(input, output, session) {
logoList = data.frame(
name = c("opel", "kia", "bmw"),
logo = c"<img height='50' src='https://cdn.iconscout.com/icon/free/png-256/opel-2-202862.png'></img>",
  "<img height='50' src='https://www.logospng.com/images/88/royal-azure-blue-kia-icon-free-car-logo-88484.png'></img>",
  "<img height='50' src='https://cdn.iconscout.com/icon/free/png-256/bmw-4-202746.png'></img>"
),
stringsAsFactors = FALSE
)
myData = reactiveVal( {
logo_name_match <- merge(
x = data.frame(
row_id = 1:length(colnames(testmatrix)),
cols = rownames(testmatrix), 
stringsAsFactors = FALSE
), 
y = logoList, 
by.x = "cols", 
by.y = "name", 
all.x = TRUE
)
logo_name_match <- logo_name_match[with(logo_name_match, order(col_id)), ]
row_colnames <- ifelse(!is.na(logo_name_match$logo), logo_name_match$logo, logo_name_match$cols)
rownames(testmatrix) <- new_row testmatrix
})
output$myTable = renderDataTable({
myData = myData()
datatable(myData, escape = FALSE)
})}

1 个答案:

答案 0 :(得分:1)

您的代码中有一些错误。一旦正确布置数据框,在DataTable列中使用徽标图像似乎不是问题。下一步是弄清楚如何在列名中使用图像或图标。尚不知道如何做,但是如果找到答案,我将更新下面的代码。注意,我添加了dplyr软件包,因为我重写了将图像链接与数据合并的部分。此外,我使用dput()将您的testmatrix数据放入代码内,因此其他用户无需下载数据即可重现您的示例。

更新:将图像添加到DataTable列名称比较容易,然后可以预期。开头的img标签出现了一些问题,所以我将其切断。似乎没有问题。

library(shiny)
library(DT)
library(dplyr)
library(readxl)

testmatrix <-
  structure(
    list(
      brand = c(
        "kia",
        "vw",
        "mit",
        "bmw",
        "audi",
        "lw",
        "lada",
        "RR",
        "opel",
        "LBGN",
        "Jeep"
      ),
      g.rank = c(1, 2, 3, 6,
                 5, 4, 8, 10, 9, 12, 11),
      `No. Of Cars` = c(180, 159, 156, 164,
                        198, 191, 192, 155, 167, 185, 156),
      generation_z = c(37, 66,
                       72, 37, 2, 32, 19, 88, 49, 83, 43),
      generation_x = c(80, 59,
                       56, 64, 98, 91, 92, 55, 67, 85, 56),
      generation_y = c(94, 4,
                       1, 7, 47, 99, 34, 68, 81, 69, 97),
      other = c(1, 2, 3, 6, 5, 4,
                8, 10, 9, 12, 11)
    ),
    row.names = c(NA,-11L),
    class = c("tbl_df",
              "tbl", "data.frame")
  )

shinyApp(

ui = fluidPage(
  dataTableOutput("myTable")
),

server = function(input, output, session) {

  logoList = data.frame(
    name = c("opel", "kia", "bmw"),
    logo = c("<img height='50' src='https://cdn.iconscout.com/icon/free/png-256/opel-2-202862.png'></img>",
    "<img height='50' src='https://www.logospng.com/images/88/royal-azure-blue-kia-icon-free-car-logo-88484.png'></img>",
    "<img height='50' src='https://cdn.iconscout.com/icon/free/png-256/bmw-4-202746.png'></img>"
  ),
  stringsAsFactors = FALSE
  )

myData = reactive({

  testmatrix %>% 
    inner_join(logoList, by = c("brand" = "name")) %>% 
    dplyr::select(
           manufacturer = logo,
           `<img height='25' src='https://image.flaticon.com/icons/svg/63/63337.svg'>` = generation_z,
           everything(),
           -brand)

})

output$myTable = renderDataTable({ 

  print(myData())

  datatable(myData(), escape = FALSE)

})
}
)