Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

r - How to replicate Knit HTML in a command line?

I know this question is similar to this one. But I couldn't get a solution there so posting it here again.

I want to get the exact same output as I get by clicking "Knit HTML" but via a command. I tryied using knit2html but it messes with the formatting and does not include the title, kable does not work etc.

Example:

This is my test.Rmd file,

---
title: "test"
output: html_document
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
library(knitr,quietly=T)
kable(summary(cars))
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Output:

Knit HTML

enter image description here

knit2html

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The documentation tells us:

If you are not using RStudio then you simply need to call the rmarkdown::render function, for example:

rmarkdown::render("input.Rmd")

Note that in the case using the “Knit” button in RStudio the basic mechanism is the same (RStudio calls the rmarkdown::render function under the hood).

In essence, rmarkdown::render does a lot more setup than knitr::knit2html, although I don’t have an exhaustive list of all differences.

The most flexible way of rendering the output is, at any rate, to supply your own stylesheet to format the output according to your wishes.

Please note that you need to set up Pandoc manually to work with rmarkdown::render on the command line.


That said, here are two remarks that would improve the knitr::knit2hmtl output, and that are superior to using rmarkdown::render in my opinion:

  • To include the title, use a Markdown title tag, not a YAML tag:

    # My title
    
  • To format tables, don’t use the raw kable function. In fact, this is also true when using rmarkdown::render: the alignment of the table cells is completely off. Rmarkdown apparently uses centering as the default alignment but this option is almost never correct. Instead, you should left-align text and (generally) right-align numbers. As of this writing, Knitr cannot do this automatically (as far as I know) but it’s fairly easy to include a filter to do this for you:

    ```{r echo=FALSE}
    library(pander)
    
    # Use this option if you don’t want tables to be split
    panderOptions('table.split.table', Inf)
    
    # Auto-adjust the table column alignment depending on data type.
    alignment = function (...) UseMethod('alignment')
    alignment.default = function (...) 'left'
    alignment.integer = function (...) 'right'
    alignment.numeric = function (...) 'right'
    
    # Enable automatic table reformatting.
    opts_chunk$set(render = function (object, ...) {
        if (is.data.frame(object) ||
            is.matrix(object)) {
            # Replicate pander’s behaviour concerning row names
            rn = rownames(object)
            justify = c(if (is.null(rn) || length(rn) == 0 ||
                            (rn == 1 : nrow(object))) NULL else 'left',
                        sapply(object, alignment))
            pander(object, style = 'rmarkdown', justify = justify)
        }
        else if (isS4(object))
            show(object)
        else
            print(object)
    })
    ```
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...