使用withTags()在R DT的页脚中添加下标

tyg4sfes  于 2023-03-27  发布在  其他
关注(0)|答案(2)|浏览(133)

我试图在一个Shiny应用程序(similar as here to the rownames/body)中向DataTables页脚添加上标。

library(shiny)
library(DT)

ui <- fluidPage(dataTableOutput("table"))

server <- function(input, output) {
  output$table <- renderDataTable({

    sketch <- htmltools::withTags(table(
      tableFooter(c(("Footer<sup>1</sup>"),
                     "Footer<sup>2</sup>"))
    ))

    data <- datatable(data.frame(c(1, 2),
                                 row.names = c("A<sub>1</sub>",
                                               "A<sub>2</sub>")),
                      rownames = TRUE,
                      container = sketch,
                      escape = FALSE)
  })
}

shinyApp(ui = ui, server = server)

但是,标记不是呈现为supscript,而是按字面意思打印:

看一下sketch,可以看出大于/小于符号被转换成了它们的HTML字符实体:

> sketch
<table>
  <tfoot>
    <tr>
      <th>Footer&lt;sup&gt;1&lt;/sup&gt;</th>
      <th>Footer&lt;sup&gt;2&lt;/sup&gt;</th>
    </tr>
  </tfoot>
</table>

我试图用\\\来转义它们,但没有成功,而htmltool's manual on withTags()是相当稀疏的。那么,如何才能转义更大/更小的符号来将它们保留为标签呢?已经谢谢你了!:)

z0qdvdin

z0qdvdin1#

最好在tableFooter函数中使用额外的escape参数。

library(shiny)
library(DT)

ui <- fluidPage(dataTableOutput("table"))

server <- function(input, output) {
  output$table <- renderDataTable({
    sketch <- htmltools::withTags(
      table(
        tableFooter(c(
          "Footer<sup>1</sup>",
          "Footer<sup>2</sup>"
        ), escape = FALSE)
      )
    )

    data <- datatable(
      data.frame(c(1, 2),
        row.names = c(
          "A<sub>1</sub>",
          "A<sub>2</sub>"
        )
      ),
      rownames = TRUE,
      container = sketch,
      escape = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

9njqaruj

9njqaruj2#

我们可以使用tags代替withtags

library(shiny)
library(DT)

ui <- fluidPage(dataTableOutput("table"))

server <- function(input, output) {
  output$table <- renderDataTable({
    
    sketch <- tags$table(
      tags$tfoot(
        tags$tr(
          tags$th(
            tags$p(
              HTML("Footer<sup>1</sup>",
                   "Footer<sup>2</sup>"),
              style = "display: inline;"
            )
          )
        )
      )
    )
    
    data <- datatable(
      data.frame(c(1, 2),
                 row.names = c("A<sub>1</sub>", "A<sub>2</sub>")),
      rownames = TRUE,
      container = sketch,
      escape = FALSE
    )
  })
}

shinyApp(ui = ui, server = server)

相关问题