Programmatically creating Markdown tables in R with KnitR

前端 未结 8 1690
梦谈多话
梦谈多话 2020-11-29 15:25

I am just starting to learn about KnitR and the use of Markdown in generating R documents and reports. This looks to be perfect for a lot of the day to day reporting that I

相关标签:
8条回答
  • 2020-11-29 15:32

    To write / create Markdown tables in R, you can also use MarkdownReports' MarkDown_Table_writer_DF_RowColNames() or MarkDown_Table_writer_NamedVector() functions. You just pass on a data frame / matrix with dimension names, or a vector with names, and it parses & writes out the table in Markdown format.

    0 讨论(0)
  • 2020-11-29 15:35

    My function for Gitlab:

    to_markdown<-function(df) {
        wrap<-function(x,sep=" ") paste0("|", sep, paste(x, collapse=paste0(sep,"|",sep)), sep, "|", sep=sep)
        paste0(wrap(colnames(df)),
        "\n",
        wrap(rep("------", ncol(df)),sep=""),
        "\n",
        paste(apply(df, 1, wrap), collapse="\n"))
    }
    
    cat(to_markdown(head(iris[,1:3])))
    
    | Sepal.Length | Sepal.Width | Petal.Length | 
    |------|------|------|
    | 5.1 | 3.5 | 1.4 | 
    | 4.9 | 3 | 1.4 | 
    | 4.7 | 3.2 | 1.3 | 
    | 4.6 | 3.1 | 1.5 | 
    | 5 | 3.6 | 1.4 | 
    | 5.4 | 3.9 | 1.7 | 
    
    0 讨论(0)
  • 2020-11-29 15:36

    use a combination of knitr::kable and xtable in your markdown document.

    library("knitr","xtable")
    

    for a simple data.frame -

    kable(head(mtcars[,1:4]),format="markdown")
    kable(head(mtcars[,1:4]),format="pandoc",caption="Title of the table")
    

    format="pandoc" allows more options like caption.

    Now the combination for model summary.

    data(tli)
    fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
    kable(xtable(fm1), caption = "Annova table")
    

    for even more options look at stargazer package instead of xtable.

    example for personal use

    0 讨论(0)
  • 2020-11-29 15:39

    Now knitr (since version 1.3) package include the kable function for a creation tables:

    > library(knitr)
    > kable(head(iris[,1:3]), format = "markdown")
    |  Sepal.Length|  Sepal.Width|  Petal.Length|
    |-------------:|------------:|-------------:|
    |           5,1|          3,5|           1,4|
    |           4,9|          3,0|           1,4|
    |           4,7|          3,2|           1,3|
    |           4,6|          3,1|           1,5|
    |           5,0|          3,6|           1,4|
    |           5,4|          3,9|           1,7|
    

    UPDATED: if you get raw markdown in a document try setup results = "asis" chunk option.

    0 讨论(0)
  • 2020-11-29 15:43

    There are functions in the pander package:

    > library(pander)
    > pandoc.table(head(iris)[, 1:3])
    
    -------------------------------------------
     Sepal.Length   Sepal.Width   Petal.Length 
    -------------- ------------- --------------
         5.1            3.5           1.4      
    
         4.9             3            1.4      
    
         4.7            3.2           1.3      
    
         4.6            3.1           1.5      
    
          5             3.6           1.4      
    
         5.4            3.9           1.7      
    -------------------------------------------
    
    0 讨论(0)
  • 2020-11-29 15:53

    Two packages that will do this are pander

    library(devtools)
    install_github('pander', 'Rapporter')
    

    Or ascii

    pander is a slightly different approach to report construction, (but can be useful for this feature).

    ascii will allow you to print with type = 'pandoc (or various other markdown flavours)

    library(ascii)
    print(ascii(head(iris[,1:3])), type = 'pandoc')
    
    
    
        **Sepal.Length**   **Sepal.Width**   **Petal.Length**  
    --- ------------------ ----------------- ------------------
    1   5.10               3.50              1.40              
    2   4.90               3.00              1.40              
    3   4.70               3.20              1.30              
    4   4.60               3.10              1.50              
    5   5.00               3.60              1.40              
    6   5.40               3.90              1.70              
    --- ------------------ ----------------- ------------------
    

    Note that in both these cases, it is directed towards using pandoc to convert from markdown to your desired document type, however using style='rmarkdown' will create tables that are compatible with this markdown package and inbuilt conversion in rstudio.

    0 讨论(0)
提交回复
热议问题