在R中格式化html表 [英] Formatting html table in R

查看:149
本文介绍了在R中格式化html表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望改进使用包 xtable 在R中生成的 html 表的外观:

  library(xtable)

html.table = xtable(< mydataframe>)
数字(html.table)= 2

我使用以下内容打印表格:

  html.tab = print(html.table,type =html,floating = FALSE)
cat(html.tab,file = < html link>)

我希望能够证明表格中的文字,修改标题栏的颜色,更改字体,...

有什么办法可以在R中实现?



谢谢!

解决方案

这个想法是:


  • 使用一些css特性来格式化表格

  • 创建一个包含指向css文件和cre的链接的文件ated html table

  • 所以这里创建res.html文件的代码:

      ##虚拟data.frame用作示例
    library(xtable)
    n < - data.frame(x = c(1,1 ,1,1,1),y = c(0,1,0,1,0))
    ## html头文件
    ##这里我使用了mystyle.css的链接
    html.head< - paste(< head>,
    '< link rel =stylesheettype =text / csshref =mystyle.css/>',
    < / head>,sep ='\ n')
    ## html body
    html.table< - paste(print(xtable(n),type =' html','res.html'),
    collapse =\\\

    html.body< - paste(< body>,html.table,< / body> ;)
    ## html文件
    写(粘贴(html.head,html.body,sep ='\\\
    '),res.html)

    syle表单文件(mystyle.css)可以包含如下所示的内容:

     表格{
    最大宽度:95%;
    border:1px solid #ccc;
    }

    th {
    background-color:#000000; //表头的背景
    颜色:#ffffff;
    }

    td
    {
    text-align:right; //证明列
    background-color:#FF0000;
    }


    I would like to improve the look of an html table that I generate in R using the package xtable:

     library(xtable)
    
     html.table = xtable(<mydataframe>)
     digits(html.table) = 2
    

    I print the table using:

     html.tab = print(html.table, type = "html", floating = FALSE)
     cat(html.tab, file = <html link>)
    

    I would like to be able to justify the text in the table, modify the color of the header column, change the font, ...

    Is there any way i can achieve that in R?

    Thank you!

    解决方案

    The idea is to :

    1. Create a css where you format "stylize" your table using some css features
    2. Create a html table using print.xtable
    3. Create a file including a link to the css file and the created html table

    So here the code creating the "res.html" file:

    ## a dummy data.frame used as an example
    library(xtable)
    n <- data.frame(x = c(1,1,1,1,1), y = c(0,1,0,1,0))
    ## the html header  
    ## here I am using a link to mystyle.css 
    html.head <- paste("<head>" ,
                 '<link rel="stylesheet" type="text/css" href="mystyle.css"/>',
                 "</head>",sep='\n')
    ## the html body
    html.table <- paste(print(xtable(n),type='html','res.html'), 
                        collapse = "\n")
    html.body <- paste("<body>", html.table,"</body>")
    ## the html file
    write(paste(html.head,html.body,sep='\n'),"res.html")
    

    the syle sheet file(mystyle.css) can contain be something like this :

    table {
       max-width: 95%;
       border: 1px solid #ccc;
    }
    
    th {
      background-color: #000000; // background for table header 
      color: #ffffff;
    }
    
    td
    {
       text-align:right;        // justify column
       background-color: #FF0000;
    }
    

    这篇关于在R中格式化html表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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