R-使用xtable或kable创建的HTML表中的条件行突出显示 [英] R - Conditional row highlighting in HTML table created using xtable or kable

查看:67
本文介绍了R-使用xtable或kable创建的HTML表中的条件行突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎是通过编程来格式化R输出的初学者,但是我对knitrxtable,Markdown和Pandoc将一种标记格式转换为另一种格式的能力有基本的了解.我想做的是将R数据帧df写到HTML表中,然后将一种特定的颜色应用于满足条件的每一行(例如df$outcome == 1).但是,我不确定哪个程序包会以简单有效的方式完成此任务,而是通过浏览一些表格式化线程来实现( xtable线程2 kable文档1 ),我已经收集到kablextable可能能够实现我想要的结果.

I'm pretty much a beginner at programmatically formatting R output, but I've got a basic understanding of knitr, xtable, Markdown, and Pandoc's ability to convert one markup format to another. What I want to do is write an R dataframe df to an HTML table, and apply a particular color to each row that meets a condition (e.g., df$outcome == 1). However, I'm not sure which package would accomplish this in a simple and efficient way, but from browsing a few table formatting threads (xtable thread 1, xtable thread 2, kable documentation 1), I've gathered that kable and xtable might be capable of accomplishing my desired result.

为澄清起见,这是我的可复制示例(使用xtable,但对使用kable或其他软件包的答案也很感兴趣):

To clarify, here is my reproducible example (using xtable, but am interested in an answer using kable or another package as well):

set.seed(123)
df <- data.frame(id       = sample(1:100, 20, replace = TRUE),
                 inputval = sample(seq(0, 1, by=0.01), 20, replace = TRUE),
                 outcome  = sample(1:4, 20, replace = TRUE))

library(xtable)
dfxt <- xtable(df)

knit2html(input      = "~/rowcolor_ex.Rmd",
          output     = OUTPUTHERE
          stylesheet = "STYLESHEET.css")

with knit2html引用名为"rowcolor_ex.Rmd"的文件,如下所示:

withknit2html referencing the file named "rowcolor_ex.Rmd", shown below:

```{r,echo=FALSE,results='asis',warning=FALSE,message=FALSE}
print(dfxt, 
      type = "html",
      include.rownames = FALSE,)
```

我了解,如果要使用xtable,我会在Rmd文档的函数调用的print(dfxt,部分之后包括一个或多个参数,并且有意义的add.to.row参数,但目前尚不清楚代码将如何更改以用于HTML输出.另外,我不确定在knit2html中引用CSS样式表是否会覆盖HTML表的格式.

I understand that if I'm to use xtable, I'd include one or more arguments after the print(dfxt, part of the function call in the Rmd document, and this thread shows the add.to.row argument that makes sense for type = "latex", but it isn't clear how the code would change for HTML output. Also, I'm not sure if referencing a CSS stylesheet in knit2html would override the HTML table's formatting.

推荐答案

以下是使用Gmisc::htmlTable

set.seed(123)
df <- data.frame(id       = sample(1:100, 20, replace = TRUE),
                 inputval = sample(seq(0, 1, by=0.01), 20, replace = TRUE),
                 outcome  = sample(1:4, 20, replace = TRUE))

cols <- with(df, ifelse(outcome == 1, 'magenta', 'white'))

library(Gmisc)
htmlTable(as.matrix(df), altcol = cols, 
          rgroup = '', n.rgroup = rep(1, length(cols)))

编辑

由于htmlTable此后已移至软件包htmlTable,并且不再位于Gmisc> = 1.0中,因此实现此目的的新方法将是

Since htmlTable has since been moved to the package, htmlTable, and is no longer in Gmisc >= 1.0, the new way to do this would be

library('htmlTable')
htmlTable(as.matrix(df), col.rgroup = cols)

这还提供:

您的降价代码就是

```{r, results='asis'}
htmlTable(as.matrix(df), altcol = cols, 
          rgroup = '', n.rgroup = rep(1, length(cols)))
```

我的.Rmd看起来像:

And my .Rmd would look like:

---
output: 
  html_document:
    css: ~/knitr.css
---

```{r, results='asis', message=FALSE}
set.seed(123)
df <- data.frame(id       = sample(1:100, 20, replace = TRUE),
                 inputval = sample(seq(0, 1, by=0.01), 20, replace = TRUE),
                 outcome  = sample(1:4, 20, replace = TRUE))

cols <- with(df, ifelse(outcome == 1, 'magenta', 'white'))

library(Gmisc)
htmlTable(as.matrix(df), altcol = cols, 
          rgroup = '', n.rgroup = rep(1, length(cols)))
```

这篇关于R-使用xtable或kable创建的HTML表中的条件行突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆