customized xtable in R

Hi all,

Is there a way to edit/change features inside xtable. For example, below code sets default as <table border=1> . Can we add/make the like this <table border=1; width=1000px> or <table border=5; width=1000px>.

So basically can we alter the features inside xtable?

print(xtable(head(iris,n = 2)),type="HTML")
<!-- html table generated in R 4.0.4 by xtable 1.8-4 package -->
<!-- Fri Jun 18 17:09:42 2021 -->
<table border=1>
<tr> <th>  </th> <th> Sepal.Length </th> <th> Sepal.Width </th> <th> Petal.Length </th> <th> Petal.Width </th> <th> Species </th>  </tr>
  <tr> <td align="right"> 1 </td> <td align="right"> 5.10 </td> <td align="right"> 3.50 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
  <tr> <td align="right"> 2 </td> <td align="right"> 4.90 </td> <td align="right"> 3.00 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
   </table>

Hello @vinayprakash808 ,

I did not know how to do that so I checked the documentation of the xtable::xtable function but did not find a solution. Then I did a Google search on xtable html border and found a solution by tertra : using the html.table.attributes argument of the xtable::print.xtable function.

library(xtable)

print(xtable(head(iris, 2)), type = "html", include.rownames = F, 
      html.table.attributes="border=1;width=1000px")
#> <!-- html table generated in R 4.1.0 by xtable 1.8-4 package -->
#> <!-- Sun Jun 20 21:17:54 2021 -->
#> <table border=1;width=1000px>
#> <tr> <th> Sepal.Length </th> <th> Sepal.Width </th> <th> Petal.Length </th> <th> Petal.Width </th> <th> Species </th>  </tr>
#>   <tr> <td align="right"> 5.10 </td> <td align="right"> 3.50 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
#>   <tr> <td align="right"> 4.90 </td> <td align="right"> 3.00 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
#>    </table>

print(xtable(head(iris, 2)), type = "html", include.rownames = F, 
      html.table.attributes="border=5; width=1000px")
#> <!-- html table generated in R 4.1.0 by xtable 1.8-4 package -->
#> <!-- Sun Jun 20 21:17:54 2021 -->
#> <table border=5; width=1000px>
#> <tr> <th> Sepal.Length </th> <th> Sepal.Width </th> <th> Petal.Length </th> <th> Petal.Width </th> <th> Species </th>  </tr>
#>   <tr> <td align="right"> 5.10 </td> <td align="right"> 3.50 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
#>   <tr> <td align="right"> 4.90 </td> <td align="right"> 3.00 </td> <td align="right"> 1.40 </td> <td align="right"> 0.20 </td> <td> setosa </td> </tr>
#>    </table>
Created on 2021-06-20 by the reprex package (v2.0.0)
1 Like

Really thanks for the efforts. It worked

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.