4

R 中是否有任何函数允许将 HTML 表格作为 R Markdown 或相关编织文档的一部分导出,并允许对表格行边框进行详细控制?

例如,想象一个这样的矩阵:

x <- matrix(c("", "M", "F", "Good", 
"23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)

什么命令会输出一个正确的 HTML 表格,其特征如下:

       --------
       M     F
---------------
 Good  23    17
---------------
 Bad   23    4
---------------
4

1 回答 1

10

您可以尝试我在重度开发下名为 pander 的非常年轻的包,试图以 pandoc markdown 格式打印 R对象

懒惰的例子:

> x <- matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)
> pandoc(x)

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+

我只是在处理一些导致其他表语法的函数,例如“简单表”或“多行表”(请参阅​​ Pandoc 的自述文件)。


PS:您还可以使用(尚未记录的)Pandoc参考类轻松将此表导出为 HTML(除了 docx、odt 等其他格式),例如:

> myReport <- Pandoc$new()
> myReport$add(x)
> myReport

Anonymous's report
==================
 written by *Anonymous* at *Sun May 27 21:04:22 2012*

  This report holds 1 block(s). 

---

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+


---

Proc. time:  0.009 seconds. 

> myReport$format <- 'html'
> myReport$export()

Exported to */tmp/pander-4e9c12ff63a6.[md|html]* under 0.031 seconds.

PS第二:您还可以brew(如编织)一个文本文档,使用该文档将您的标签从内部R对象Pandoc.brew自动转换为Pandoc markdown格式。<%=...%>简短的例子(当然这也适用于文件输入,现在我只是brew一个 R 字符向量):

> t <- '# Title
+ 
+ A nice matrix:
+ 
+ <%=matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)%>
+ 
+ Bye-bye!'
> 
> Pandoc.brew(text=t)
# Title

A nice matrix:

+------+------+------+
|      | M    | F    |
+------+------+------+
| Good | 23   | 17   |
+------+------+------+
| Bad  | 23   | 4    |
+------+------+------+

Bye-bye!
于 2012-05-27T19:00:36.653 回答