سؤال

I am trying to make a knitr-based program that reads rmarkdown from a character vector and writes to a textConnection. I am getting almost what I want but I find that knitr only produces html for chunks and just passes through the rmarkdown to html.

Here is the code:

text_input <- "Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page 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 cache=TRUE}
x <- cars
summary(x)
```

You can also embed plots, for example:

```{r fig.width=7, fig.height=6,cache=TRUE}
plot(x)
```
"
library(knitr)

outfile <- textConnection("foo.html", "w")

pat_md()
render_html()
knit(input=NULL,output=outfile,text=text_input)
close(outfile)
cat(foo.html,sep="\n")

Which outputs:

Title
========================================================

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the **MD** toolbar button for help on Markdown).

When you click the **Knit HTML** button a web page 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:

<div class="chunk" id="unnamed-chunk-1"><div class="rcode"><div class="source"><pre class="knitr r">x <- cars
summary(x)
</pre></div><div class="output"><pre class="knitr r">##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
</pre></div></div></div>


You can also embed plots, for example:

<div class="chunk" id="unnamed-chunk-2"><div class="rcode"><div class="source"><pre class="knitr r">plot(x)
</pre></div><div class="rimage default"><img src="figure/unnamed-chunk-2.png" title="plot of chunk unnamed-chunk-2" alt="plot of chunk unnamed-chunk-2" class="plot" /></div>
</div></div>

Is this the expected behavior or am I doing something wrong?

هل كانت مفيدة؟

المحلول

I solved this a different way (create md file with knitr and then md -> html):

text_input <- "markdown text"
library(knitr)

foo.html <- knit2html(text=text_input)

Thanks Yihui. However as I note in the comment below there are still some issues with the resulting html (only with character vector input not regular files).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top