R:将多个图像添加到DataFrame/DataTable中?

发布于 2025-01-24 15:37:33 字数 2357 浏览 1 评论 0 原文

我正在尝试将几个图像添加到r中的数据框架中,以在r Shiny中复制DT :: DatatableOutput。

所需的输出看起来像:

图标 食物
” 葡萄
” Marrow

我抚养这个问题将图像添加到r 的数据词中,这使我可以添加我本地具有的图像,但是只有在路径中输入路径时


df <- read.csv("data.csv")

path = "/Users/name/Documents/remaining_path/"

df%<>%
  mutate(Icon = img_uri("/Users/name/Documents/remaining_path/grapes.png"))

,要添加的映像,所以我希望获得一些自动化的东西,例如:


df%<>%
  mutate(Image = paste(path, Food, ".png", sep = ""))%>%
  mutate(Icon = img_uri(Image))

这给了我这个错误

Error in `mutate()`:
! Problem while computing `Icon = img_uri(Image)`.
Caused by error in `file()`:
! invalid 'description' argument

,我也尝试创建一个带有for loop的向量:


for (i in df$Image) {
  
  Icon <- img_uri(i)
  
}

它会引发此错误

Error in file(con, "rb") : cannot open the connection

9. file(con, "rb")
8. readBin(file, what, n, ...)
7. read_bin(x)
6. base64_encode(x)
5. paste0("data:", mime::guess_type(x), ";base64,", base64_encode(x))
4. xfun::base64_uri(f)
3. knitr::image_uri(x)
2. sprintf("<img src=\"%s\"/>", knitr::image_uri(x))
1. img_uri(i)

并使用应用程序:

Icon <- apply(X = df$Image, MARGIN = 1, FUN = img_uri())

这给出了此错误,这

Error in knitr::image_uri(x) : argument "x" is missing, with no default

9. basename(file)
8. mime::guess_type(x)
7. paste0("data:", mime::guess_type(x), ";base64,", base64_encode(x))
6. xfun::base64_uri(f)
5. knitr::image_uri(x)
4. sprintf("<img src=\"%s\"/>", knitr::image_uri(x))
3. img_uri()
2. match.fun(FUN)
1. apply(X = df$Image, MARGIN = 1, FUN = img_uri())

将欣赏任何输入因此。

I'm trying to add several images to a dataframe in R to reproduce in R Shiny as DT::dataTableOutput.

The desired output would look something like:

Icon Food
First grapes
Second marrow

I fount this question Adding an image to a datatable in R which allows me to add images that I have locally, however only if I type in the path, as such:


df <- read.csv("data.csv")

path = "/Users/name/Documents/remaining_path/"

df%<>%
  mutate(Icon = img_uri("/Users/name/Documents/remaining_path/grapes.png"))

However I have hundreds of images to add so I was hoping to get something slightly more automated, like this:


df%<>%
  mutate(Image = paste(path, Food, ".png", sep = ""))%>%
  mutate(Icon = img_uri(Image))

which gives me this error

Error in `mutate()`:
! Problem while computing `Icon = img_uri(Image)`.
Caused by error in `file()`:
! invalid 'description' argument

I have also tried creating a vector with a for loop:


for (i in df$Image) {
  
  Icon <- img_uri(i)
  
}

which throws this error

Error in file(con, "rb") : cannot open the connection

9. file(con, "rb")
8. readBin(file, what, n, ...)
7. read_bin(x)
6. base64_encode(x)
5. paste0("data:", mime::guess_type(x), ";base64,", base64_encode(x))
4. xfun::base64_uri(f)
3. knitr::image_uri(x)
2. sprintf("<img src=\"%s\"/>", knitr::image_uri(x))
1. img_uri(i)

And with apply:

Icon <- apply(X = df$Image, MARGIN = 1, FUN = img_uri())

which gives this error

Error in knitr::image_uri(x) : argument "x" is missing, with no default

9. basename(file)
8. mime::guess_type(x)
7. paste0("data:", mime::guess_type(x), ";base64,", base64_encode(x))
6. xfun::base64_uri(f)
5. knitr::image_uri(x)
4. sprintf("<img src=\"%s\"/>", knitr::image_uri(x))
3. img_uri()
2. match.fun(FUN)
1. apply(X = df$Image, MARGIN = 1, FUN = img_uri())

Would appreciate any input on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-31 15:37:33

我会做:

library(DT)
library(base64enc)

imgs <- c("img1.png", "img2.png")
b64imgs <- vapply(imgs, function(img){
  dataURI(mime = "image/png", file = img)
}, character(1L))
b64array <- paste0(
  c("[", sprintf("%s", toString(paste0("'", b64imgs, "'"))), "]"), 
  collapse = ""
)

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  sprintf("var img = %s[data-1];", b64array),
  "    data = '<img src=\"' + img + '\"  width=100>';",
  "  }",
  "  return data;",
  "}"
)

dat <- data.frame(
  image = c(1, 2), 
  file = imgs
)

datatable(
  dat, 
  width = 500,
  options = list(
    columnDefs = list(
      list(targets = 1, render = JS(render)),
      list(targets = "_all", className = "dt-center")
    )
  )
)

“在此处输入图像描述”

I would do:

library(DT)
library(base64enc)

imgs <- c("img1.png", "img2.png")
b64imgs <- vapply(imgs, function(img){
  dataURI(mime = "image/png", file = img)
}, character(1L))
b64array <- paste0(
  c("[", sprintf("%s", toString(paste0("'", b64imgs, "'"))), "]"), 
  collapse = ""
)

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  sprintf("var img = %s[data-1];", b64array),
  "    data = '<img src=\"' + img + '\"  width=100>';",
  "  }",
  "  return data;",
  "}"
)

dat <- data.frame(
  image = c(1, 2), 
  file = imgs
)

datatable(
  dat, 
  width = 500,
  options = list(
    columnDefs = list(
      list(targets = 1, render = JS(render)),
      list(targets = "_all", className = "dt-center")
    )
  )
)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文