How to export HTML table in R and have control over line borders?

后端 未结 1 1564
悲&欢浪女
悲&欢浪女 2021-01-03 05:25

Are there any functions in R that permit the exporting of HTML tables as part of an R Markdown or related weaved document and that permit detailed control over table line bo

相关标签:
1条回答
  • 2021-01-03 06:26

    You might try my really young package under heavy development named to pander which tries to print R objects in pandoc markdown format.

    Lazy example:

    > x <- matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)
    > pandoc(x)
    
    +------+------+------+
    |      | M    | F    |
    +------+------+------+
    | Good | 23   | 17   |
    +------+------+------+
    | Bad  | 23   | 4    |
    +------+------+------+
    

    I am just working on some functions resulting in other table syntax like "simple table" or "multiline table" (see Pandoc's readme).


    P. S.: you could also export this table easily to HTML (besides other formats like docx, odt etc.) with the (not yet documented) Pandoc reference class like:

    > myReport <- Pandoc$new()
    > myReport$add(x)
    > myReport
    
    Anonymous's report
    ==================
     written by *Anonymous* at *Sun May 27 21:04:22 2012*
    
      This report holds 1 block(s). 
    
    ---
    
    +------+------+------+
    |      | M    | F    |
    +------+------+------+
    | Good | 23   | 17   |
    +------+------+------+
    | Bad  | 23   | 4    |
    +------+------+------+
    
    
    ---
    
    Proc. time:  0.009 seconds. 
    
    > myReport$format <- 'html'
    > myReport$export()
    
    Exported to */tmp/pander-4e9c12ff63a6.[md|html]* under 0.031 seconds.
    

    P.S. second: you could also brew (like sweave) a text document with Pandoc.brew which would auto-transform your <%=...%> tags from internal R object to Pandoc markdown format. Short example (of course this would work with file input too, now I just brew an R character vector):

    > t <- '# Title
    + 
    + A nice matrix:
    + 
    + <%=matrix(c("", "M", "F", "Good", "23", "17", "Bad", "23", "4"), nrow=3, byrow=TRUE)%>
    + 
    + Bye-bye!'
    > 
    > Pandoc.brew(text=t)
    # Title
    
    A nice matrix:
    
    +------+------+------+
    |      | M    | F    |
    +------+------+------+
    | Good | 23   | 17   |
    +------+------+------+
    | Bad  | 23   | 4    |
    +------+------+------+
    
    Bye-bye!
    
    0 讨论(0)
提交回复
热议问题