给定一个包含一列图像链接 ( <img src...>) 的数据框,是否可以使用 的reactable可扩展行功能来扩展行并查看图像?
这是一些基本数据:
library(tidyverse)
library(reachable)
img_df <- tribble(~image,
"<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg/450px-Kosaciec_szczecinkowaty_Iris_setosa.jpg'/>",
"<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Iris_versicolor_3.jpg/320px-Iris_versicolor_3.jpg'/>",
"<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/295px-Iris_virginica.jpg'/>"
)
iris %>%
group_by(Species) %>%
summarize(mean = mean(Sepal.Length)) %>%
add_column(img_df) -> df
reactable(df)
Run Code Online (Sandbox Code Playgroud)
因此,它不会显示“图像”列,而是显示为可扩展列,并在行扩展时显示图像。
我可以使用此代码显示 HTML,但图像本身不会显示:
reactable(df,
columns = list(
image = colDef(html = TRUE,
resizable = TRUE,
show=F)
),
details = function(index) {
if(df$image[index] != "") {
htmltools::HTML(df$image[index])
}
})
Run Code Online (Sandbox Code Playgroud)
我可以使用此代码显示图像,但它附带所有附加行信息。(这取自文档 [https://glin.github.io/reactable/articles/examples.html#expandable-row-details-1]
reactable(df, details = colDef(
name = "More",
details = JS("function(rowInfo) {
return 'Details for row: ' + rowInfo.index +
'<pre>' + JSON.stringify(rowInfo.row, null, 3) + '</pre>'
}"),
html = TRUE,
width = 60
))
Run Code Online (Sandbox Code Playgroud)
我认为这就是您所寻找的 - 您的图像是可见的超链接。我编辑了我的答案;我想我错过了你最初寻找的部分内容。
首先,您需要删除 Web 链接中的 HTML 标记。符号正在被转换(即,<to <、>to>等)。相反,使用包htmltools来添加标签。
我坚持使用您的原始代码,并对我更改的内容添加了注释。
一探究竟 - :
library(tidyverse)
library(reactable)
library(htmltools)
#---------------- removed the tags in the data frame ----------------
img_df <- tribble(~image,
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg/450px-Kosaciec_szczecinkowaty_Iris_setosa.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Iris_versicolor_3.jpg/320px-Iris_versicolor_3.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/295px-Iris_virginica.jpg"
)
#---------------- this is unchanged ----------------
iris %>%
group_by(Species) %>%
summarize(mean = mean(Sepal.Length)) %>%
add_column(img_df) -> df
#-------------------- your table --------------------
reactable(df, columns = list(
image = colDef(show = F,
resizable = T,
html = T)),
details = function(index) {
# essentially <a><img /></a>
htmltools::tags$a(href = df[index,]$image,
target = "_blank",
htmltools::tags$img(src = df[index,]$image,
width = "100",
height = "100"))
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
619 次 |
| 最近记录: |