【发布时间】: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”。期望的结果是实际显示图像。
-
为了让图片显示在表格中,需要将其转换成HTML
img标签,请看下面我的回答。