0

I am creating a Word file from R using the officer package version 0.4.4. In the past I used the slip_in_text() function often but that function is removed: Error: 'slip_in_text' is defunct. Use 'fpar()' instead. My problem with fpar() is that it always starts at the beginning of a new line. With the slip_in_text() function you could append text to an existing line of text in Word. Is that impossible now?

docx <- docx %>%
  cursor_bookmark("Begin") %>% 
  body_add_fpar(fpar(ftext("John")), style = "Normal", pos = "on")

should give: Name: John but gives: John Name: Bookmark Begin is after the double dot of Name:

Gerben
  • 61
  • 3

1 Answers1

0

I would advise using package 'doconv' that contains a function to update all computed Word fields. Instead of inserting text, the idea is to replace references with values set in the document property (you need Word for that).

  1. add manually references in your Word document where you want to replace/set values.
  2. With officer, set the replacement values in the document properties with set_doc_properties().
  3. Run doconv::docx_update() that will do the replacements.
library(officer)
library(doconv)

read_docx() |> 
  # this step is programmed for the illustration instead of being inserted manually
  body_add_fpar(
    value = fpar(
      run_word_field("DOCPROPERTY \"coco\" \\* MERGEFORMAT"))) |>

  set_doc_properties(coco = "test") |>
  print(target = "output.docx") |>
  docx_update()
David Gohel
  • 9,180
  • 2
  • 16
  • 34