【问题标题】:Embed images after query results are returned using R, Shiny, DT and DBI使用 R、Shiny、DT 和 DBI 返回查询结果后嵌入图像
【发布时间】:2019-09-11 16:54:10
【问题描述】:

我正在 R Shiny 中创建一个数据表,该数据表由从 SSMS 中的表中提取的 SELECT 语句填充。其中一列返回位于 R 从中提取的 www 文件夹中的图像文本。例如,如果我的查询是:

SELECT Fruit, Image, Description
FROM tblFruit

它可能会在 SQL 中返回类似这样的内容:

Fruit  |  Image      |  Description
-----------------------------------
apple  |  apple.png  |  red apple
orange |  orange.png |  orange orange
banana |  banana.png |  yellow banana

我在 R 中的数据表与此类似,但我的目标是将文本“apple.png”替换为位于 www 文件夹中的实际图像。这可能吗?

这是我的 app.R 的样机/sn-p:

library(dplyr)
library(DBI)
library(plotly)
library(shiny)
library(shinydashboard)
library(RJDBC)
library(readxl)
library(DT)
library(htmltools)
library(shinyBS)

conn <- dbConnect(drv, "jdbc:sqlserver://SQLServer;databaseName=DEV", Sys.getenv("userid"), Sys.getenv("pwd"))

sqlSelect = trimws("SELECT Fruit, Image, Description FROM tblFruit ")
sqlWhere = "WHERE Fruit in ('Apple','Orange','Banana')"

body <- dashboardBody(class="text-center",
                        tabItem("fruit", class ="text-center",
                                fluidRow(
                                  tabBox(title = "Fruit", id = "FruitID",
                                         tabPanel(title = 'Fruit',
                                                  fluidRow(
                                                    box(width=12,
                                                        div(DT::dataTableOutput(outputId = "DT_Fruit"), style="font-size:125%", class ="text-left")
                                                    )
                                                  )
                                         )
                                  ) # tabBox
                                ) # fluidRow
                        ) # tabItem fruit
                      ) # tabItems
) # body

server <- function(input, output){
    output$DT_Fruit <- DT::renderDataTable({
      DT::datatable(options = list(dom = 't'), rowname = FALSE, dbGetQuery(conn, paste(sqlSelect, sqlWhere)))
    })
}

编辑:根据此处的第一条评论,我在从 www 文件夹读取时没有问题。我想用 www 文件夹中名为 apple.png 的图像替换从我的查询返回的文本(例如“apple.png”)。我或多或少正在寻找有关如何最好地解决此问题的方向/建议。

【问题讨论】:

  • 您似乎只是在数据表中显示本地图像时遇到问题。 Display Image in a Data Table from a local path in R Shiny 的可能重复项
  • Yifu Yan,感谢您的回复,但也许我没有很好地描述我的问题。我在底部添加了一个编辑以进一步澄清,不幸的是,您提供的链接与我的问题/问题不同。
  • 我阅读了您更新的问题。 returns a image named app,你们是什么人?您要返回图像二进制文件的副本还是只在表格中显示图像?
  • 是的,在数据​​表中显示图像。留下我现在所拥有的只是在图像列中返回文本“apple.png”。期望的结果是实际显示图像。
  • 为了让图片显示在表格中,需要将其转换成HTMLimg标签,请看下面我的回答。

标签: r image shiny dbi dt


【解决方案1】:

你可以试试这个:

render <- c(
  "function(data, type, row, meta) {",
  "  if(type === 'display'){",
  "    var img = '<img src= \"' + data + '\">';",
  "    return img;",
  "  }else{",
  "    return data;",
  "  }",
  "}"
)

server

output$DT_Fruit <- renderDT({
  datatable(
    dbGetQuery(conn, paste(sqlSelect, sqlWhere)), 
    rownames = FALSE, 
    options = list(
      dom = "t",
      columnDefs = list(
        list(targets = 1, render = JS(render))
      )
    )
  )
})

【讨论】:

  • 宾果游戏!非常感谢你。像魅力一样工作:)
【解决方案2】:

如果这是您的问题,您似乎仍然在 DT 表中显示图像时遇到问题。

使用glue::glue()paste0()apple.png 转换为HTML 中的img 标记,并设置escapse = FALSE,以便DT 将其解析为HTML。

请看下面的示例,只需将src 中的值替换为您的图像路径即可。

library(DT)
DT::datatable(
    data.frame(
        images = c(
            "<img src='https://images-na.ssl-images-amazon.com/images/I/81xQBb5jRzL._SY355_.jpg' alt='apple.jpg' width='100px'>",
            "<img src='https://images-na.ssl-images-amazon.com/images/I/71gI-IUNUkL._SL1500_.jpg' alt='banana.jpg' width='100px'>"
        )
    ),escape = FALSE
)


【讨论】:

    猜你喜欢
    • 2015-08-20
    • 2017-12-26
    • 2020-11-08
    • 2016-01-17
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多