如何解决R Shiny table 如何在可扩展行中正确格式化 html 代码
在我闪亮的应用程序中,我显示了一个带有可扩展行的表格(可响应)。我想更改某些单词的背景颜色,因此我使用 html span。它适用于常规行中的文本,在可扩展行中但只显示纯 html 代码。
我为两列都设置了 html = TRUE
,但没有正确显示。我如何使它工作?
app.R
library(shiny)
library(htmltools)
library(reactable)
ui <- fluidPage(
reactableOutput("table")
)
server <- function(input,output) {
output$table <- renderReactable({
df = data.frame("title" = c("This is the <span style='background-color:yellow;'>Title</span>","This is a longer Title"),"abstract" = c("This is the <span style='background-color:yellow;'>abstract</span>","This is an even longer abstract"))
reactable(
df,columns = list(
abstract = colDef(show = F,html = TRUE),title = colDef( html = TRUE)
),details = function(index) {
htmltools::div(style= "background-color:white",htmltools::tags$div(style= "background-color:#eee; padding: .9em; border-color: #ffe;",df[index,"abstract"])
)
}
)
})
}
解决方法
使用 here 中的 html
函数,我们可以做到 -
library(shiny)
library(htmltools)
library(reactable)
html <- function(x,inline = FALSE) {
container <- if (inline) htmltools::span else htmltools::div
container(dangerouslySetInnerHTML = list("__html" = x))
}
ui <- fluidPage(
reactableOutput("table")
)
server <- function(input,output) {
output$table <- renderReactable({
df = data.frame("title" = c("This is the <span style='background-color:yellow;'>Title</span>","This is a longer Title"),"abstract" = c("This is the <span style='background-color:yellow;'>abstract</span>","This is an even longer abstract"))
reactable(
df,columns = list(
abstract = colDef(show = F,html = TRUE),title = colDef( html = TRUE)
),details = function(index) {
htmltools::div(style= "background-color:white",htmltools::tags$div(style= "background-color:#eee; padding: .9em; border-color: #ffe;",html(df[index,"abstract"]))
)
}
)
})
}
shinyApp(ui,server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。