美文网首页
officedown 生成 word

officedown 生成 word

作者: 鲁瑞瑞 | 来源:发表于2020-09-30 16:48 被阅读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文件时,主要功能包括:
    • 各级标题自动编号;
    • 支持自定义word中图/表格式(包括图表标题格式);
    • 支持自定义文本格式(字体设置、文本居中等);
    • 一键生成目录,包括文章目录、图形目录、表格目录;
    • 自动生成页码;
    • 支持图表的交叉引用;
    • 实现页面分栏;
    • 实现横向页面;
    • 支持自定义有序列表、无序列表格式;

    一些问题

    1. 如何安装?
    remotes::install_github("davidgohel/officedown")
    或直接
    install.packages('officedown')
    
    1. 如何创建一个officedown文件?
    • 创建R markdown文件;
    • 从from_template :Advance Word document创建(模板中包含各种预置样式);


      创建officedown文件
    • yaml header 中设置output:officedown::rdocx_document;
    ---
    date: "`r Sys.Date()`"
    author: "Your Name"
    title: "officedown template"
    output: 
      officedown::rdocx_document:
        mapstyles:
          Normal: ['First Paragraph']
    ---
    
    1. 如何为各级标题自动设置编号及相关格式?
      officedown对标题预置了样式,样式中设置了自动编号:


      预置标题样式

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

    • 自定义生成模板文件,修改标题样式, 模板文件保存为template.docx;


      自定义标题样式
    • 通过yaml header 中设置reference_docx参数为template.docx使用自定义样式;
    ---
    date: "`r Sys.Date()`"
    author: "Your Name"
    title: "officedown template"
    output: 
      officedown::rdocx_document:
        reference_docx : template.docx
    ---
    
    1. 如何设置word中表格格式?
    • 表格标题内容(tab.cap)、表格id(tab.id):
      在knitr chunk options 中设置如下所示,其中表格id可以用来生成交叉引用;
    ```{r tab.cap="cars", tab.id="cars"}
    head(cars)```
    
    • 表格相关格式在yaml header 中设置:
    ---
    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
    ---
    
    • 参数含义:
      以tables作为一级参数,设置包括样式(style)、布局(layout)、宽度(width)、标题(caption)、其他(conditional),上图所示为默认设置,各参数详细含义参见:参数
    • 输出结果示例:


      表格示例
    • 一些疑问:
      • style:Table并不在预置的样式中;
      • 表格标题自动编号:table 1 / table 2, 在caption下 style:Table Caption样式中尚未找到相关设置;
    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.

    • fp_text函数可以用来设置字体格式:
      • 预先设置字体格式(大小、颜色、字体、是否加粗):
      library(officedown)
      library(officer)
      ft <- fp_text(shading.color='#EFEFEF', bold = TRUE)
      
      • 使用时利用ftext将文本与预设格式联系在一起
      This document presents most of the features of the package `r ftext("officedown", ft)`.
      #段落中的'officedown'会被加粗
      
    • fp_par函数可以用来设置段落的位置格式:
      • 预先设置位置格式:
      library(officedown)
      library(officer)
      fp <- fp_par(
             text.align = "center", 
             padding.bottom = 20, padding.top = 120, 
             border.bottom = fp_border())
      
      • 使用时将格式标量放在段落结尾即可:
      This document presents most of the features of the package `r   ftext("officedown", ft)`. 
      `r fp`
      #整段文本会居中,并且添加下划线
      
    • 效果如下:


      文本格式设置效果
    • 更多关于officer包参见:officer
    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. 有序列表、无序列表问题?
    • Officedown在列表上比rmarkdown有何优势?
      能够自定义有序、无序列表的样式;

    • 如何自定义有序列表和无序列表样式?

      • 在R markdown中生成有序无序列表参见 markdown列表
      • 在template.docx中自定义列表样式,并设置样式名称,具体方法参见:word自定义列表样式
        自定义列表样式
      • 在 reference_docx中使用模板 template.docx;并设置lists参数为自定义列表样式的名称(ol: ordered list;ul: unordered list)
    ---
    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'
    ---
    
    • 自定义样式效果:


      image.png
    • 注意问题:列表形式是全局的,对文档中的所有列表生效,无法针对同一篇文档中的不同列表设置不同格式。
    • 注意问题:在生成reference_docx时,模板需要使用officedown生成的docx文件(包含officedown的各种预置样式),直接创建新docx文档会导致预置样式不可用。
    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")
    

    参考

    相关文章

      网友评论

          本文标题:officedown 生成 word

          本文链接:https://www.haomeiwen.com/subject/ubqeuktx.html