0

I am using officer for R to create a a Word (docx) document. Here is the code:

library(officer)
library(magrittr)

my_docx = read_docx() %>% 
  # cursor_begin() %<>% 
  body_add_par("Hello here is a test sentence to see if there is a blank line above it.", style = "Normal") %>% 
  print(target = "test.docx")

It creates a docx document that has a blank line at the top above the sentence. It isn't spacing before the font style that is set in the Style of the font. I have uncommented cursor_begin() but the blank line remains. How can I get rid of this?

Any help is appreciated!

Prevost
  • 677
  • 5
  • 20
  • There is a `pos` argument to `body_add_par` that I was missing and it can be set to `before` and that will adjust position of text in docx document...my bad. – Prevost Jan 20 '18 at 03:14

1 Answers1

1

To delete a paragraph, you need to use function body_remove(). See documentation here: https://davidgohel.github.io/officer/articles/word.html#remove-content

library(officer)
library(magrittr)

my_docx <- read_docx() %>% 
  cursor_begin() %>% body_remove() %>% 
  body_add_par("Hello here is a test sentence to see if there is a blank line above it.", 
               style = "Normal") %>% 
  print(target = "test.docx")
David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thank you! I didn't need to remove a paragraph since I was starting with a blank template...it was just the cursor position argument in the `body_add_par` that I had omitted which now seems silly as I did not fully read the documentation...and thanks for `officer` – Prevost Jan 22 '18 at 20:41
  • Could you validate my solution? This answer your original question. – David Gohel Jan 22 '18 at 21:46