Use hooks to format table in output

前端 未结 4 1153
情话喂你
情话喂你 2020-12-20 14:08

Using knitr and R Markdown, I can produce a tabularised output from a matrix using the following command:

```{r results=\'asis\'}
kable(head(x))
```
<         


        
相关标签:
4条回答
  • 2020-12-20 14:26

    I think other answers are from a time when the following didn't work, but now we can just do :

    ```{r results='asis', render=pander::pander}
    head(x)
    ```
    

    Or set this for all chunks in the setup chunk, for instance :

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE, render=pander::pander)
    ```
    
    0 讨论(0)
  • 2020-12-20 14:26

    Lacking a better solution I’m currently re-parsing the character string representation that I receive in the hook. I’m posting it here since it kind of works. However, parsing a data frame’s string representation is never perfect. I haven’t tried the following with anything but my own data and I fully expect it to break on some common use-cases.

    reparse <- function (data, comment, ...) {
        # Remove leading comments
        data <- gsub(sprintf('(^|\n)%s ', comment), '\\1', data)
        # Read into data frame
        read.table(text = data, header = TRUE, ...)
    }
    
    default_output_hook <- knit_hooks$get('output')
    
    knit_hooks$set(output = function (x, options)
        if (is.null(options$table))
            default_output_hook(x, options)
        else {
            extra_opts <- if (is.list(options$table)) options$table else list()
            paste(kable(do.call(reparse, c(x, options$comment, extra_opts))),
                  collapse = '\n')
        }
    )
    

    This will break if the R markdown comment option is set to a character sequence containing a regular expression special char (e.g. *), because R doesn’t seem to have an obvious means of escaping a regular expression.

    Here’s a usage example:

    ```{r table=TRUE}
    data.frame(A=1:3, B=4:6)
    ```
    

    You can pass extra arguments to the deparse function. This is necessary e.g. when the table contains NA values because read.table by default interprets them as strings:

    ```{r table=list(colClasses=c('numeric', 'numeric'))}
    data.frame(A=c(1, 2, NA, 3), B=c(4:6, NA))
    ```
    

    Far from perfect, but at least it works (for many cases).

    0 讨论(0)
  • 2020-12-20 14:34

    Nowadays one can set df_print in the YAML header:

    ---
    output:
      html_document:
        df_print: kable  
    ---
    
    ```{r}
    head(iris)
    ```
    
    0 讨论(0)
  • 2020-12-20 14:49

    Not exactly what you are looking for, but I am posting an answer here (that could not fit in a comment) as your described workflow is really similar to what my initial goal and use-case was when I started to work on my pander package. Although I really like the bunch of chunk options that are available in knitr, I wanted to have an engine that makes creating documents really easy, automatic and without any needed tweaks. I am aware of the fact that knitr hooks are really powerful, but I just wanted to set a few things in my Rprofile and let the literate programming tool its job without further trouble, that ended to be Pandoc.brew for me.

    The main idea is to specify a few options (what markdown flavour are you using, what's your decimal mark, favorite colors for your charts etc), then simply write your report in a brew syntax without any chunk options, and the results of your code would be automatically transformed to markdown. Then convert that to pdf/docx/odt etc. with Pandoc.

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