0

I am new to officeR and I was able to learn it just a few hours. I am running R (R 4.3.0 GUI 1.79 Big Sur Intel build (8225)) on an Apple macOS Big Sur version 11.7.6 and I wrote a small Word document, 4 pages with images, tables, and graphs. I then sent the Word document to a friend running Windows 10 and MS Office 2013 version, they were able to open the document. I then sent the same document to another, who is running Windows 11 an MS Office 365, and they were unable to open the document. Is there any know issues? I am using MS Office for MAC version 16.72 and the document is okay for me to view on my Apple

    setwd("~/Documents/R/officer")
    rm(list=ls())
    ######################
    #   https://www.r-bloggers.com/2020/07/how-to-read-and-create-word-documents-in-r/
    #   https://www.youtube.com/watch?v=RkWoi57p-Bo
    ######################
    if (!require(officer)) install.packages("officer")
    if (!require(tidyverse)) install.pakages("tidyverse")
    if (!require(dplyr)) install.packages("dplyr")
    if (!require(readxl)) install.packages("readlx")
    ####
    library(officer)
    library(tidyverse)
    library(dplyr)
    library(readxl)
    ######################
    theme_set(theme_classic() + theme(axis.line = element_line(linewidth = 0.25, color = "black", linetype = "solid")))
    ######################
    # create empty Word file
    sample_doc <- read_docx()
    ######
    heading.style <- fp_text(color = "blue",
                    bold = T,
                    font.size = 16,
                    underline = F,
                    italic = T,
                    font.family = "Times New Roman")
    format.heading <- ftext("Steyer Consulting Engineering Resources LLC", heading.style) |>
                        fpar()
    sample_doc <- sample_doc |> body_add(format.heading, style = "centered") |>
    body_add_par("")
    logo <- "LetterLogoRed.png"
    sample_doc <- sample_doc |> body_add_img(logo, width = 4, height = 1, style = "centered", pos = "after")
    ######
    sample_doc <- sample_doc |> body_add_par("Introduction", style = "heading 2") |>
    body_add_par("") |>
    body_add_par("This concept paper addresses the need to update annex 1 (manufacture of sterile medicinal products) of the good manufacturing practice (GMP) guide. Annex 1 is common to the member states of the European Union/European Economic Area as well as to the participating authorities of the Pharmaceutical Inspection Co-operation Scheme (PIC/S)1. The original version was partially revised in 1996, 2003 and 2007; however, there has not been a complete review of the document since it was originally issued. Since this time there have been changes in technologies and significant changes in GMP following the adoption of the ICH Q9 and Q10 guidelines.
    ") |>
    body_add_par("") |>
    body_add_par("The current annex 1 is therefore being reviewed in order to facilitate implementation of the principles in these ICH guidelines, to extend the underlying concepts to include new areas of technology and processing not previously covered and also to clarify areas that have been highlighted as ambiguous due to the age of the document.") |>
    body_add_par("") |>
    body_add_par("The current guideline, including its title, is focused on the manufacture of sterile medicinal products. However, this annex is the only source of guidance in EU-PIC/S GMP for the conditions of manufacture of some non-sterile finished products and for the early stages in the manufacture of a range of products.") |>
    body_add_par("") |>
    body_add_par("Discussion", style = "heading 2") |>
    body_add_par("") |>
    body_add_par("Since annex 1 was published, introduction of the relevant ICH concepts and consequential regulatory changes and technological advancements are not reflected in the current GMP guideline. In addition, and in keeping with greater international convergence, opportunities will be taken where appropriate to align this guideline with international requirements. The opportunity will also be taken to ensure maintenance of coherence with other EU or PIC/S pharmaceutical guideline documents.") |>
    body_add_par("") |>
    body_add_par("The current GMP guideline on the manufacture of sterile medicinal products was developed before the development of the ICH Q9 risk management concepts that offer a systematic approach to quality risk management, Q10 describes a modern quality system in order to establish and maintain a state of control, the realisation of product quality and to facilitate continual improvement over the entire life cycle.") |>
    body_add_par("") |>
    body_add_par("The revised guideline will clarify to what extent Q9 and Q10 should be followed in the design and implementation of facilities, equipment and processes for the manufacture of sterile medicinal products. Other changes that may require new GMP guidance include those for the revision to the Ph.Eur. monograph on methods other than distillation for the production of water for injection.") |>
    body_add_par("") |>
    body_add_par("Number Microspheres per Liter", style = "heading 2") |>
    body_add_par("")
    #   Now, we can add a table to our document using the body_add_table function. Before we do that, we just need to have a data frame ready, so we’ll create a sample one like below.
    # create sample data frame
    #df <- data.frame(a = 1:10, b = 11:20, c= 21:30)
    df <- read_excel("Number Microspheres per Liter (version 1).xlsx")
    # add table containing the data frame's contents
    sample_doc <- sample_doc |> body_add_table(df, style = "table_template",
                    header = F,
                    alignment = c("l", "c", "c")) |>
    body_add_break()
    #   We can also add images to our Word Document. This is done by creating a temp file with an R plot and then adding the image to our document object. Though we’re using base R for plotting here, ggplot could also be used.
    set.seed(0)
    # create a temp file
    src <- tempfile(fileext = ".png")       # {base} function
    # create PNG object
    png(filename = src, width = 6, height = 6, units = 'in', res = 400)
    # create plot
    x <- seq(-100, 100, 2)
    y <- x^3
    plot(x, y, pch = 20, type = "b", col = "blue", main = "Test Plot", xlab = "Time (min)", ylab = "Response")
    # save PNG file
    dev.off()
    # add PNG image to Word document
    sample_doc <- sample_doc %>% body_add_img(src = src, width = 6, height = 6, style = "centered")
    #   Lastly, we can save our Word Document using print.
    colors <- c("red", "green", "blue")
    my.mtcars <- mtcars |>
            mutate(cyl = as.factor(cyl))
    gg <- ggplot(my.mtcars, aes(mpg, wt)) +
                geom_point(aes(color = cyl),
                            size = 3,
                            shape = 20) +
                scale_color_manual(values = colors) +
                labs(title = "MPG vs Weight",
                        x = "Miles per (US) gallon",
                        y = "Weight of Car (1000 lb)",
                        color = "# of Cylinders") +
                theme(plot.title = element_text(size = 14, hjust = 0.6))    
    ggsave("mtcars.png", gg, width = 6, height = 6, units = "in")
    sample_doc <- sample_doc |> body_add_img(src = "mtcars.png", width = 6, height = 6)
    print(sample_doc, target = "sample_file.docx")
    
    
Kirk
  • 1
  • 1
  • Not sure it's the cause of your issue, but you have a typo: `"readlx"` should be `"readxl"` – r2evans May 02 '23 at 21:54
  • Thanks r2evans for your time, I too do not think that that would cause an issue with opening a "Word" file. – Kirk May 02 '23 at 22:51
  • Are you able to reproduce in a way the doesn't need your XLSX file? – r2evans May 03 '23 at 01:10
  • Thanks to all who have helped on this post. I was informed by the Window 11 MS Office 365 user that they restarted their computer and now are able to open and read the document. I suspect this was a Windows issue and not a code or R packages issue. – Kirk May 04 '23 at 14:17

0 Answers0