将 R 表导出为 HTML

发布于 2024-09-13 07:05:07 字数 34 浏览 2 评论 0原文

有没有办法轻松将 R 表导出到简单的 HTML 页面?

Is there a way to easily export R tables to a simple HTML page?

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

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

发布评论

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

评论(5

儭儭莪哋寶赑 2024-09-20 07:05:07

xtable包中的xtable函数可以将R表导出为HTML表。 此博客条目< /a> 描述如何从 Sweave 文档创建 HTML 页面。

The xtable function in the xtable package can export R tables to HTML tables. This blog entry describes how you can create HTML pages from Sweave documents.

温柔嚣张 2024-09-20 07:05:07

值得一提的是,有一个新的包专门设计用于以简单直观的方式将 data.frames(或表格)转换(并使用 css 样式)为 HTML 表格。它称为tableHTML。您可以在下面看到一个简单的示例:

library(tableHTML)
#create an html table 
tableHTML(mtcars)

#and to export in a file
write_tableHTML(tableHTML(mtcars), file = 'myfile.html')

您可以在此处查看详细教程 也是如此。

It might be worth mentioning that there has been a new package specifically designed to convert (and style with css) data.frames (or tables) into HTML tables in an easy and intuitive way. It is called tableHTML. You can see a simple example below:

library(tableHTML)
#create an html table 
tableHTML(mtcars)

#and to export in a file
write_tableHTML(tableHTML(mtcars), file = 'myfile.html')

You can see a detailed tutorial here as well.

强辩 2024-09-20 07:05:07

除了 @nullglob 提到的 xtable 之外,还有另外三个包可能会在这里派上用场:

Apart from xtable mentioned by @nullglob there are three more packages that might come handy here:

帅气尐潴 2024-09-20 07:05:07

表包 gt 的语法也是一个选项。

以下是文档中用于生成 HTML 表的示例

library(gt)

tab_html <-
  gtcars %>%
  dplyr::select(mfr, model, msrp) %>%
  dplyr::slice(1:5) %>%
  gt() %>%
  tab_header(
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) %>%
  as_raw_html()

The grammar of tables package gt is also an option.

Here's the example from the docs for generating a HTML table:

library(gt)

tab_html <-
  gtcars %>%
  dplyr::select(mfr, model, msrp) %>%
  dplyr::slice(1:5) %>%
  gt() %>%
  tab_header(
    title = md("Data listing from **gtcars**"),
    subtitle = md("`gtcars` is an R dataset")
  ) %>%
  as_raw_html()
—━☆沉默づ 2024-09-20 07:05:07

在 DT 包的问题中,有人发布了如何使用 DT 包获取表格中的 html< /a>.我已粘贴相关示例代码,并对其进行修改以引用所有列:targets = "_all"

library(DT)

render <- c(
  "function(data, type, row){",
  "  if(type === 'sort'){",
  "    var parser = new DOMParser();",
  "    var doc = parser.parseFromString(data, 'text/html');",
  "    data = doc.querySelector('a').innerText;",
  "  }",
  "  return data;",
  "}"
)

dat <- data.frame(
  a = c("AAA", "BBB", "CCC"), 
  b = c(
    '<a href="#" id = "Z" onclick = "Ztest">aaaaa</a>', 
    '<a href="#" id = "A" onclick = "Atest">bbbbb</a>',
    '<a href="#" id = "J" onclick = "Jtest">jjjjj</a>'
  )
)

datatable(
  dat, 
  escape = FALSE,
  options = list(
    columnDefs = list(
      list(targets = "_all", render = JS(render))
    )
  )
)

我希望这有帮助。

In an issue for the DT package, someone posted how to use the DT package to get html in tables. I've pasted the relevant example code, modifying it to reference all columns with: targets = "_all".

library(DT)

render <- c(
  "function(data, type, row){",
  "  if(type === 'sort'){",
  "    var parser = new DOMParser();",
  "    var doc = parser.parseFromString(data, 'text/html');",
  "    data = doc.querySelector('a').innerText;",
  "  }",
  "  return data;",
  "}"
)

dat <- data.frame(
  a = c("AAA", "BBB", "CCC"), 
  b = c(
    '<a href="#" id = "Z" onclick = "Ztest">aaaaa</a>', 
    '<a href="#" id = "A" onclick = "Atest">bbbbb</a>',
    '<a href="#" id = "J" onclick = "Jtest">jjjjj</a>'
  )
)

datatable(
  dat, 
  escape = FALSE,
  options = list(
    columnDefs = list(
      list(targets = "_all", render = JS(render))
    )
  )
)

I hope this helps.

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