officedown 生成 word

2020-09-30  本文已影响0人  鲁瑞瑞

功能简介

  1. officedown是在R markdown的基础上添加了一些office的功能( R markdown中文本没有格式, officedown提供文本居中功能等)。

{officedown} is bringing some officer features into R markdown documents.
officedown : Enhanced R markdown Format for Word and Powerpoint.

  1. officedown可以在R markdown的基础上生成docx/pptx文件;也可以在bookdown的基础上生成docx文件。
  2. 生成docx文件时,主要功能包括:

一些问题

  1. 如何安装?
remotes::install_github("davidgohel/officedown")
或直接
install.packages('officedown')
  1. 如何创建一个officedown文件?
---
date: "`r Sys.Date()`"
author: "Your Name"
title: "officedown template"
output: 
  officedown::rdocx_document:
    mapstyles:
      Normal: ['First Paragraph']
---
  1. 如何为各级标题自动设置编号及相关格式?
    officedown对标题预置了样式,样式中设置了自动编号:


    预置标题样式

    上图为officedown生成的word文件中包含的样式,可以看到标题样式中设置了自动编号。
    如需修改预置的样式可以:

---
date: "`r Sys.Date()`"
author: "Your Name"
title: "officedown template"
output: 
  officedown::rdocx_document:
    reference_docx : template.docx
---
  1. 如何设置word中表格格式?
```{r tab.cap="cars", tab.id="cars"}
head(cars)```
---
output: 
  officedown::rdocx_document:
    tables:
      style: Table
      layout: autofit
      width: 1.0
      caption:
        style: Table Caption
        pre: 'Table '
        sep: ': '
      conditional:
        first_row: true
        first_column: false
        last_row: false
        last_column: false
        no_hband: false
        no_vband: true
---
  1. 如何设置word中图形格式?
    图形的标题及相关设置方式同表格,图形标题内容通过fig.cap设置,图形标题id通过fig.id设置,相关参数在yaml header中设置。
---
output: 
  officedown::rdocx_document:
    plots:
      style: Normal
      align: center
      caption:
        style: Image Caption
        pre: 'Figure '
        sep: ': '
---
  1. 图表标题图和自动生成编号?
    默认自动生成,不知道在何处设置。

  2. 如何设置word中文本的格式,字体?
    Rmarkdown中文本没有格式,使用officedown是可通过officer 包(安装officedown时自动安装,使用时需要加载)提供了一些额外的officer support。

Package {officer} provides functions to produce document blocks and runs elements.

  1. 如何生成目录?
<!---BLOCK_TOC{level: 2}--->     #文章目录,level设置目录级别
<!---BLOCK_TOC{seq_id: 'fig'}--->   #图形目录
<!---BLOCK_TOC{seq_id: 'tab'}--->   #表格目录

效果如图:


目录
  1. 能否生成页码?
    默认生成页码, 并且预置样式中包含页码样式:


    页码样式
  2. 如何使用交叉引用?

{officedown} bring this feature: caption are autonumbered and a bookmark is set on the chunk containing the number; cross-references are Word references hyperlinked to the captions they are related to.

\@ref(fig:tsplot)  
\@ref(tab:mtcars)

@ref()为固定格式;fig/tab表示图形/表格;tsplot与mtcars 是在knitr chunk options中设置的fig.id/tab.id(唯一标识图表的id)。

This is a linked reference to a figure: \@ref(fig:tsplot)
交叉引用
  1. 如何实现页面分栏?
<!---BLOCK_MULTICOL_START--->

This text is on column 1. This text is on column 1.
This text is on column 1. This text is on column 1.
This text is on column 1. This text is on column 1.
This text is on column 1. This text is on column 1.
This text is on column 1. This text is on column 1.`r fp_par(text.align = "justify")`
`r run_columnbreak()`

This text is on column 2.
This text is on column 2.
This text is on column 2.
This text is on column 2.
This text is on column 2.
This text is on column 2.`r fp_par(text.align = "justify")`

<!---BLOCK_MULTICOL_STOP{widths: [3,3], space: 0.2, sep: true}--->

r run_columnbreak(是officer包中的函数)表示分栏。STOP中的参数:widths表示分栏左右的宽度, space表示分栏中文本与中心线的距离, sep表示是否显示分栏线。
效果如下图:

分栏效果
  1. 如何实现页面横向排布?
<!---BLOCK_LANDSCAPE_START--->

blah blah blah.

<!---BLOCK_LANDSCAPE_STOP--->

效果如下:


页面横排
  1. 有序列表、无序列表问题?
---
date: "`r Sys.Date()`"
author: "Your Name"
title: "officedown template"
output: 
  officedown::rdocx_document:
    mapstyles:
      Normal: ['First Paragraph']
    reference_docx : template.docx
    lists:
      ol.list : 'self ol'
      ul.list : 'self ul'
---
  1. 如何实现另起一页?
\newpage
# 另起一页
  1. 页眉页脚设置?
    officedown有定义页眉页脚的样式;具体生成页眉页脚是R markdown的功能。

  2. Officedown还提供生成ppt功能;Officedown也可以从bookdown生成word。
    生成ppt没实践过,从bookdown生成word可参考官方示例如下:

dir <- system.file(package = "officedown", "examples", "bookdown")
file.copy(dir, getwd(), recursive = TRUE, overwrite = TRUE)
rmarkdown::render_site("bookdown")
browseURL("bookdown/_book/bookdown.docx")

参考

上一篇下一篇

猜你喜欢

热点阅读