PDF和DOCX中看起来不错

PDF和DOCX中看起来不错

本文介绍了简单的RMarkdown手动表格,在HTML,PDF和DOCX中看起来不错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能手动简单地在RMarkdown中格式化表格,当转换为HTML(使用knitr和markdown软件包),PDF(使用pandoc和miktex)和docx(使用pandoc)时,表格看起来会很好?

How can I manually and simply format a table in RMarkdown that will look good when converted to HTML (using the knitr and markdown packages), PDF (using pandoc and miktex) and docx (using pandoc)?

我希望能够用RMarkdown编写小的表,这些表不是R函数的结果,R函数在我最常使用的三种格式中看起来不错.到目前为止,我发现3种格式中有2种看起来不错的格式,是否有3/3可能?

I want to be able to write small tables in RMarkdown that are not a result of R functions that look good in the three formats I use most often. So far I've found a format that looks good in 2 of the 3 formats, is 3/3 possible?

一个.在编织HTML之后看起来不错,但在PDF或docx中效果不佳

One. This looks good after Knit HTML but not good in the PDF or docx

<table>
<tr>
<td>Eggs</td>
<td>Ham</td>
</tr>
<tr>
<td>Basil</td>
<td>Tomato</td>
</tr>
</table>

两个.在编织HTML之后看起来不错,但在PDF或docx中效果不佳

Two. This one looks good after Knit HTML but not good in the PDF or docx

| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |

三个.在编织Knit HTML之后,此外观看起来不太好,但在PDF和docx中效果很好(到目前为止是最好的选择)

Three. This one does not look good after Knit HTML but is good in the PDF and docx (best option so far)

V1         Tweedledee       Tweedledum
--------   --------------   ----------------
Age        14               14
Height     3'2"             3'2"
Politics   Conservative     Conservative
Religion   "New Age"        Syrian Orthodox
---------  --------------   ----------------

四个.在编织HTML之后看起来不错,并且可以制作PDF和docx(赢家!),但不是我想要的手动格式.

Four. This looks good after Knit HTML and make PDF and docx (winner!) but is not the manual formatting I'm after.

```{r table1, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
require(pander)
panderOptions('table.split.table', Inf)
set.caption("Data on cars")
pander(mtcars, style = 'rmarkdown')
```

这就是我制作PDF和docx文件的方式:

This is how I'm making the PDF and docx files:

filen <- "table" # name of my RMarkdown file without suffix
knit(paste0(filen,".Rmd"))

# make PDF
system(paste0("pandoc -s ", paste0(filen,".md"), " -t latex -o ", paste0(filen,".pdf"), " --highlight-style=tango  -S"))

# make docx
system(paste0("pandoc -s ", paste0(filen,".md"), " -o ", paste0(filen,".docx"), " --highlight-style=tango  -S"))

推荐答案

受daroczig的评论启发,尤其是pander转换为pandoc管道语法的线索,我仔细研究了pander文档并找到了对cat.经过一番实验,我找到了获胜者:

Inspired by daroczig's comments, especially the clue that pander translates to pandoc's pipe syntax, I took a closer look at the pander documentation and found reference to cat. After some experimentation, I found the winner:

```{r table2, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
tabl <- "  # simple table creation here
| Tables        | Are           | Cool  |
|---------------|:-------------:|------:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |
"
cat(tabl) # output the table in a format good for HTML/PDF/docx conversion
```

这在我的测试中以HTML,PDF和docx生成了外观一致的表格.现在,我要就其他问题向daroczig投票,以感谢他帮助我解决问题.

This produces uniformly good looking tables in HTML, PDF and docx in my tests. Now I'm off to upvote daroczig on some other questions to thank him for getting me to the solution.

如果您需要为表格添加标题... ,则需要做一些不同的事情.请注意,标题仅在PDF中可见,而在HTML中不可见:

If you need a caption for your table... then you'll need to do it a bit differently. Note that the caption will only be visible in the PDF, not in the HTML:

```{r table-simple, echo=FALSE, message=FALSE, warnings=FALSE, results='asis'}
require(pander)
panderOptions('table.split.table', Inf)
set.caption("My great data")
my.data <- " # replace the text below with your table data
  Tables        | Are           | Cool
  col 3 is      | right-aligned | $1600
  col 2 is      | centered      |   $12
  zebra stripes | are neat      |    $1"
df <- read.delim(textConnection(my.data),header=FALSE,sep="|",strip.white=TRUE,stringsAsFactors=FALSE)
names(df) <- unname(as.list(df[1,])) # put headers on
df <- df[-1,] # remove first row
row.names(df)<-NULL
pander(df, style = 'rmarkdown')
```

这篇关于简单的RMarkdown手动表格,在HTML,PDF和DOCX中看起来不错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 20:37