2013-03-14 131 views
17

我有一個數據框,並且我想通過knitr和RMarkdown以HTML形式將其輸出爲帶有條件格式的表格。示例:使用條件格式創建帶有RMarkdown + knitr的表格

n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0)) 
> n 
    x y 
1 1 0 
2 1 1 
3 1 0 
4 1 1 
5 1 0 

我希望突出顯示具有不同x和y值的行。因此,在這種情況下,這將是第1,3,5行。如果HTML文件中的輸出是HTML表格,那麼效果會很好,但如果圖片失敗,那麼效果也不錯。

+0

http://stackoverflow.com/questions/25315309/conditional-formatting-tables-in-rmarkdown-documents暗示ReportRs包,FlexTable – rescdsk 2015-04-21 17:03:39

回答

22

我一直想使用此功能在我的pander package中擴展pandoc.table,但沒有得到時間。但這個問題真的很令人振奮,可能會在未來幾天內做到這一點。在此之前,何談:

  1. 加載包:

    library(pander) 
    
  2. 裝入數據:

    n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0)) 
    
  3. 更新您的線被標記爲Pandoc

    for (i in c(1, 3, 5)) 
        n[i, ] <- pandoc.strong.return(n[1, ]) 
    
  4. 顯示你的表的降價版本:

    pandoc.table(n) 
    pander(n)  # S3 method 
    
  5. 隱性降價到例如與HTML語法brew

    Pandoc.brew(text = '<%=n%>', output = tempfile(), convert = 'html') 
    

更新:我已經更新pander採取一些新的論點很容易突顯行/列/單元格。雖然我仍然在一些進一步的輔助功能的工作,使這個過程更容易,在這裏不用一個快速演示,這樣你可能會看到它如何能幫助您的工作流程:

> pandoc.table(n, emphasize.rows = c(1, 3, 5)) 

------- 
x y 
--- --- 
*1* *0* 

1 1 

*0* *1* 

1 1 

*1* *0* 
------- 

> pandoc.table(n, emphasize.strong.cells = which(n == 1, arr.ind = TRUE)) 

----------- 
    x  y 
----- ----- 
**1** 0 

**1** **1** 

**1** 0 

**1** **1** 

**1** 0 
----------- 

更新:pander積累了一些輔助功能,突出的表中的細胞更容易:

> t <- mtcars[1:3, 1:5] 
> emphasize.cols(1) 
> emphasize.rows(1) 
> pandoc.table(t) 

---------------------------------------------------- 
     &nbsp;   mpg cyl disp hp drat 
------------------- ------ ----- ------ ----- ------ 
    **Mazda RX4**  *21* *6* *160* *110* *3.9* 

**Mazda RX4 Wag** *21* 6 160 110 3.9 

    **Datsun 710** *22.8* 4 108 93 3.85 
---------------------------------------------------- 
pander方法

或者直接:

> emphasize.strong.cells(which(t > 20, arr.ind = TRUE)) 
> pander(t) 

--------------------------------------------------------- 
     &nbsp;   mpg  cyl disp  hp  drat 
------------------- -------- ----- ------- ------- ------ 
    **Mazda RX4**  **21** 6 **160** **110** 3.9 

**Mazda RX4 Wag** **21** 6 **160** **110** 3.9 

    **Datsun 710** **22.8** 4 **108** **93** 3.85 
--------------------------------------------------------- 

請注意,這些新功能尚未發佈在CRAN上,但您可以在GitHub上託管最新版本。

17

這裏是一個基於xtable的定製css的解決方案。我認爲解決方案是靈活的,因爲一旦你得到它的工作,你可以無限制地定製你的HTML表,如果你知道一些CSS技巧。

我們走吧。該解決方案包含3個文件:

  1. 一個css文件,其中我交替錶行顏色。

    table { 
        max-width: 95%; 
        border: 1px solid #ccc; 
    } 
    
    th { 
        background-color: #000000; 
    color: #ffffff; 
    } 
    
    table tr:nth-child(odd) td{ 
        background-color: #FF0000; 
    } 
    table tr:nth-child(even) td{ 
        background-color: #00FFFF; 
    } 
    
  2. 的R腳本文件來設置RStudio降價,其內容如下:

    options(rstudio.markdownToHTML = 
         function(inputFile, outputFile) {  
         require(markdown) 
         markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css') 
         } 
    ) 
    
  3. 創建具有以下新的降價:

    ```{r} 
    source('initmd.R') 
    ``` 
    
    
    ```{r,results='asis'} 
    library(xtable) 
    n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0)) 
    print(xtable(n),type='html') 
    ``` 
    

終於轉換markdwon使用knit HTML按鈕的HTML,你應該得到這樣的東西:

enter image description here