【问题标题】:Flextable output to .docx cannot be opened in Word无法在 Word 中打开到 .docx 的 Flextable 输出
【发布时间】:2021-12-10 10:44:37
【问题描述】:

我一直在尝试将 .csv 文件导入 R,然后将其转换为 flextable,然后将 flextable 导出为 Word .docx 文档。我使用以下代码

    library(tidyverse)
    library(readxl)
    library(scales)
    library(janitor)
    library(stringr)
    library(magrittr)
    library(officer)
    library(flextable)
    library(dplyr)
    
    # load data ---------------------------------------------------------------
    
    Rpops_survey <- read_csv("R-POPS Data Quant.csv")
    spec(Rpops_survey)
    
    # convert data ------------------------------------------------------------
    Rpops_survey_dframed <- as.data.frame(Rpops_survey)
    Rpops_ft1 <- flextable(Rpops_survey_dframed)
    Rpops_doctemp <- read_docx()
    Rpops_doctemp <- body_add_flextable(Rpops_doctemp, value = Rpops_ft1)
    
    # export data -------------------------------------------------------------
    fileout <- tempfile(fileext = ".docx")
    fileout <- "test.docx" 
    print(Rpops_doctemp, target = fileout)

文件已创建,但尝试在 Word 中打开它会产生以下错误:

【问题讨论】:

    标签: r docx flextable


    【解决方案1】:

    也许最好通过 R-Markdown 来实现。

    接下来你应该试试这个:创建一个.Rmd 文件并粘贴这段代码:

    ---
    title: "test"
    output:
      word_document: default
      pdf_document: default
    ---
    
    ```{r setup, include = FALSE}
    library(flextable)
    ft <- flextable(head(airquality))
    ```
    Here is the table: `r ft`
    

    然后编织。

    是否已创建输出 .docx 文件且没有任何错误?

    【讨论】:

      【解决方案2】:

      问题可能在于tempfile 如何创建 .docx 文件。 flextable documentation 显示为

      1. 使用空调用 officer::read_docx 创建一个空的 .docx 文件
      2. 然后使用body_add_flextable 添加您的flextable
      3. 最后调用print保存新文档

      以下是使用mtcars 数据集的示例:

      library("tidyverse")
      library("officer")
      library("flextable")
      
      # Do some calculations on an example data set
      my_data <- mtcars %>%
        group_by(cyl) %>%
        summarize(
          Mean = mean(mpg),
          SD = sd(mpg)
        ) %>%
        rename("Number of cylinders" = cyl)
      
      print(my_data)
      # A tibble: 3 x 3
      #   `Number of cylinders`  Mean    SD
      #                   <dbl> <dbl> <dbl>
      # 1                     4  26.7  4.51
      # 2                     6  19.7  1.45
      # 3                     8  15.1  2.56
      
      # Create and format the flextable object
      my_flextable <- my_data %>%
        flextable() %>%
        colformat_double(j = 2:3, digits = 2) %>%
        add_header_row(values = c("", "Miles per gallon"), colwidths = c(1, 2)) %>%
        vline(j = 1)
      
      # Save output by 
      read_docx() %>%                                  # creating a null .docx object
        body_add_flextable(value = my_flextable) %>%   # insert the flextable object
        print(target = "test.docx")                    # print the output
      

      该代码应生成包含下表的 Word 文档:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-14
        • 2022-06-03
        • 1970-01-01
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多