Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用RMarkdown+;创建具有条件格式的表;克尼特_R_Knitr - Fatal编程技术网

使用RMarkdown+;创建具有条件格式的表;克尼特

使用RMarkdown+;创建具有条件格式的表;克尼特,r,knitr,R,Knitr,我有一个数据帧,我想通过knitr和RMarkdown将其输出到一个HTML文件中,作为一个带有条件格式的表。例如: 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 n xy 1 1 0 2 1 1 3 1 0 4 1 1 5 1 0 我希望高亮显示x和y值不同的行。在本例中,这将是第1、3和5行。如果HTML文件中的输出是一个HTML表格,那就

我有一个数据帧,我想通过knitr和RMarkdown将其输出到一个HTML文件中,作为一个带有条件格式的表。例如:

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
n
xy
1 1 0
2 1 1
3 1 0
4 1 1
5 1 0

我希望高亮显示x和y值不同的行。在本例中,这将是第1、3和5行。如果HTML文件中的输出是一个HTML表格,那就好了,但如果输出失败,图像也可以。

我一直想用这个功能扩展我的
pandoc.table
,但是没有时间。但这个问题真的很鼓舞人心,可能会在未来几天内解决。在那之前,关于:

  • 加载包:

    library(pander)
    
  • 加载您的数据:

    n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
    
  • 使用
    brew
    语法将标记转换为例如HTML:

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

    更新:
    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 
    ----------------------------------------------------
    

    请注意,这些新功能尚未在CRAN上发布,但您可以在上托管的最新版本中找到。

    这里是一个基于
    xtable
    和自定义
    css
    的解决方案。我认为这个解决方案是灵活的,因为一旦你成功了,你可以无限期地定制你的html表格,如果你知道一些css技巧的话

    我们开始吧。解决方案包含3个文件:

  • 一个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;
    }
    
  • 使用以下内容设置RStudio标记的R脚本文件:

    options(rstudio.markdownToHTML = 
          function(inputFile, outputFile) {      
            require(markdown)
            markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css')   
          }
    )
    
  • 使用以下内容创建新的标记:

    ```{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')
    ```
    
    `{r}
    源('initmd.R')
    ```
    ```{r,results='asis'}
    图书馆(xtable)
    n建议使用ReportRs软件包FlexTable
    
    > 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 
    ---------------------------------------------------------
    
    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;
    }
    
    options(rstudio.markdownToHTML = 
          function(inputFile, outputFile) {      
            require(markdown)
            markdownToHTML(inputFile, outputFile, stylesheet='customstyle.css')   
          }
    )
    
    ```{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')
    ```