想要在数据表中有一个包含图像的列;它适用于 URL 但不适用于本地路径

Want to have a column with images in a datatable; it works with URL but does not work with local paths

我正在尝试将只有图像的列添加到数据表中。 然而,这是迄今为止我得到的最好的:

我不是编程专家,所以我一直在网上寻找解决方案,但似乎没有任何效果。我已经尝试过 , , Adding logos/images to the side of a data table 和其他人中的解决方案...

这是一个闪亮的应用程序,图像存储在我的本地计算机中。我不知道为什么这么多帖子都引用了 www 文件夹,因为我认为 RStudio 会查找使用 setwd() 设置的目录,我已经这样做了。但我什至用图像创建了这个 www 文件夹,我把我的 app.R 放在里面(也在外面试过),但没有作品。但是,如果我输入 URL,就会显示图像,但我现在确实需要使用本地路径来完成。也许稍后我会使用 URL 但这让我很烦,我真的很感激一个解决方案(对不起,我是个菜鸟)。我还在某处看到可以通过按 'Run App' 而不是选择整个代码然后 运行 来解决它,但这也没有用。

最后,这是代码的相关部分(数据已经加载到 RStudio 中):

library('shiny')
library('shinydashboard')
library('tidyverse')
library('data.table')
library('plotly')
library('DT')

setwd("C:/Users/directory/with/images/www")

ui <- dashboardPage(
  dashboardHeader(title="Visuals"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Units table w/ links", tabName = "tabU", icon = icon("table"))
)
tabItem(tabName="tabU",
              fluidRow(
                box(
                  DT::dataTableOutput("data"),
                  width=12),
              ))
)
)

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

df <- reactiveValues(data = data.frame(
      Name = readUnits$label,
      Icon=c('<img src="img1.png" height="52"></img>',
             '<img src="img1.png" height="52"></img>',
             '<img src="img2.png" height="52"></img>',
             '<img src="img3.png" height="52"></img>',
             '<img src="img4.png" height="52"></img>',
             '<img src="img4.png" height="52"></img>',
             '<img src="img5.png" height="52"></img>',
             '<img src="img6.png" height="52"></img>',
             '<img src="img7.png" height="52"></img>',
             '<img src="img7.png" height="52"></img>',
             '<img src="img8.png" height="52"></img>',
             '<img src="img9.png" height="52"></img>'),
      Actions = shinyInput(actionButton, 12, 'button_', label = "Linked to", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
      stringsAsFactors = FALSE,
      row.names = 1:12
    ))
    
    
    output$data <- DT::renderDataTable(
      df$data, server = FALSE, escape = FALSE, selection = 'none'
    )

...

这应该是代码中唯一相关的部分(不包括其他选项卡以及服务器代码),所以如果缺少某些括号或类似内容,请不要介意。我只是想有一种在列中显示本地图像的正确方法。 那么,有人可以帮助我吗? 提前致谢!

确实,那行不通。这种方式有效,图像在 www 子文件夹中:

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  "    var tag = '<img src=\"' + data + '.png\" width=\"100\"/>';",
  "    return tag;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)

df <- reactiveValues(data = data.frame(
  Name = readUnits$label,
  Icon=c("img1",
         "img1",
         "img2",
         .........),
  Actions = shinyInput(
    actionButton, 12, 'button_', label = "Linked to", onclick = 'Shiny.onInputChange(\"select_button\", this.id)'
  ),
  stringsAsFactors = FALSE,
  row.names = 1:12
))

output$data <- renderDT({
  datatable(
    df$data, 
    selection = 'none',
    options = list(
      columnDefs = list(
        list(targets = 2, render = JS(render))
      )
    )
  )
}, server = FALSE)