Knitr:只有块输出是html(rest保持标记)
我正在尝试制作一个基于knitr的程序,从字符向量中读取rmarkdown并写入textConnection。我几乎得到了我想要的,但我发现knitr只生成块的html,只是通过rmarkdown转换成html 代码如下:Knitr:只有块输出是html(rest保持标记),r,knitr,r-markdown,R,Knitr,R Markdown,我正在尝试制作一个基于knitr的程序,从字符向量中读取rmarkdown并写入textConnection。我几乎得到了我想要的,但我发现knitr只生成块的html,只是通过rmarkdown转换成html 代码如下: text_input <- "Title ======================================================== This is an R Markdown document. Markdown is a simple for
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")
text\u input我用另一种方法解决了这个问题(使用knitr创建md文件,然后使用md->html):
text\u输入这就是knit2html()
所做的:knit2html(text=text\u输入)
事实上,我现在正试图决定是否创建一个后续问题。此输出对于常规文件非常有效,但对于文本输入,如果字符向量包含换行符“\n”,则会出现问题。基本上,生成的html字符串有许多“\n”似乎会导致一些html解析问题。您只会在屏幕上看到\n
,但这不是后面跟着n
的反斜杠,而是换行符。您不需要解析任何内容。这个字符串就是你想要的。例如,您可以使用cat()
将其写入文件。如果您给knit2html()
一个文本输入,您将得到一个文本输出。如果你给它一个输入文件,你就会得到一个输出文件。明白了。是的,在这种情况下,从RStudio复制字符串是错误的。在我在预期的领域(一个闪亮的应用程序)中测试之后,一切都很好。再次感谢您的基本帮助。
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>
text_input <- "markdown text"
library(knitr)
foo.html <- knit2html(text=text_input)