简体   繁体   English

如何使用 R 将 HTML 编织成 PDF?

[英]How to knit HTML into PDF using R?

I would like to transform HTML into PDF using R我想使用 R 将 HTML 转换为 PDF

I have tried using cat and rmarkdown.我试过使用 cat 和 rmarkdown。

Below is an example from this answer , and my goal is to make it look the same as what it appears in the web下面是这个答案的一个例子,我的目标是让它看起来和它在网络上出现的一样

<p>You can use the <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/order.html" rel="noreferrer"><code>order()</code></a> function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the <code>example(order)</code> code:</p>

<pre><code>R&gt; dd[with(dd, order(-z, b)), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
</code></pre>

<p><em>Edit some 2+ years later:</em>  It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the <code>order()</code> function:</p>

<pre><code>R&gt; dd[order(-dd[,4], dd[,1]), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
R&gt; 
</code></pre>

<p>rather than using the name of the column (and <code>with()</code> for easier/more direct access).</p>

Maybe R -e "rmarkdown::render('input.Rmd',output_file='output.pdf')"也许R -e "rmarkdown::render('input.Rmd',output_file='output.pdf')"

So assuming you have this in a Rmakrdown file:所以假设你在 Rmakrdown 文件中有这个:

---
title: "R Notebook"
output:
  pdf_document: default
  html_notebook: default
---


You can use the [`order()`](http://stat.ethz.ch/R-manual/R-devel/library/base/html/order.html) function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the `example(order)` code:

```{r eval=FALSE}
  R> dd[with(dd, order(-z, b)), ]
    b x y z
  4 Low C 9 2
  2 Med D 3 1
  1  Hi A 8 1
  3  Hi A 9 1 
_``` 

Knitting it will result in a pdf that looks like this:编织它会产生一个看起来像这样的pdf: 在此处输入图片说明

Similarly, if you don't want to create a rmd but use the html, you can do: R -e "rmarkdown::pandoc_convert('input.html', output = 'output.pdf')"同样,如果您不想创建 rmd 而是使用 html,则可以执行以下操作: R -e "rmarkdown::pandoc_convert('input.html', output = 'output.pdf')"

Which will result in a pdf looking like this:这将导致 pdf 看起来像这样: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM