How to add line breaks? - r

I'm trying to add line breaks using body_replace_all_text or body_add_par but am having no joy. Using \r\n shows correctly in OSX TextEdit, but not in Word.
An example:
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline\r\n\r\nAnother line") %>%
print(target = "example.docx")
is there a right way to do this?

You will have to call body_add_par each time you want to add a paragraph (a paragraph of text ends with a new line):
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline") %>%
body_add_par("Another line") %>%
print(target = "example.docx")

An alternative way that I found was to modify in Word.
library(officer)
library(tidyverse)
read_docx() %>%
body_add_par("Oneline(LineBreak)Another line") %>%
print(target = "example.docx")
Then in Word, press Ctrl + H and change all "(LineBreak)" to "^p".
Not a fancy idea but it worked for me as a band aid solution.

Here is a simple workaround with splitting the string into a vector and writing each line separately. Unfortunately it is very slow.
library(officer)
library(tidyverse)
doc <- read_docx()
text <- "Oneline\r\n\r\nAnother line" %>% strsplit('\r\n') %>% unlist()
for (t in text){
body_add_par(doc, t)
}
print(doc, target = "example.docx")

Related

Using officer in R to hyperlink to another slide within a flextable cell

Using officer in R, I've used ph_slidelink() to hyperlink a text box to another slide in the presentation, and I've used compose() and hyperlink_text() to hyperlink a cell within a flextable. My question is: is there a way to combine these, and to hyperlink to another slide in the presentation within the cell of a flextable?
Here's a very simple example of code I'd like to transform:
library(officer)
library(flextable)
library(magrittr)
ft <- data.frame(slide_number = seq(3)) %>%
flextable() %>%
width(width = 3)
doc <- read_pptx() %>%
add_slide() %>%
ph_with("Table of Contents", location = ph_location_label("Title 1")) %>%
ph_with(ft, location = ph_location_label("Content Placeholder 2"))
for (i in seq(3)) {
doc <- doc %>%
add_slide() %>%
ph_with(paste("Slide", i), location = ph_location_label("Title 1"))
}
print(doc, target = "~/Desktop/officer_example.pptx" )
...and in this case I'd like the 1/2/3 in the table of contents (here) to link to slides 1/2/3.
Is this possible?

Setting line height in table created using flextable inserted into powerpoint using officer

I have drafted some dataframes with text inside that linebreaks are present in each cell, the dataframe is converted to flextable and then inserted into a powerpoint slide using officer. I found the line height a bit too much, I tried using the height_all function in flextable to make reduce the line height, but it is not working. Please find the sample code as below:
library(officer)
library(dplyr)
pptx.output.st00 <- read_pptx()
data(iris)
data.df <- head(iris) %>%
as_tibble %>%
mutate_all(.,as.character) %>%
mutate_all(.,~paste0(.,'\ntesting'))
pptx.tbl <- data.df %>%
flextable %>%
height_all(height = 0.01) # this line is not working
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,value=pptx.tbl,location=ph_location(type='body'))
print(pptx.output.st01,'presentation.output.pptx')
Currently I need to manually change the paragraph settings for table as the screen capture below:
Is there a way in officer of flextable to set up line height for table? Thanks!
I didn't satisfied by this ad hoc way, but padding(padding.top = 0, padding.bottom = 0.5) and height_all(0.45) gives a bit better output.
pptx.output.st01 <- pptx.output.st00 %>%
add_slide(.,layout = 'Title and Content',master = 'Office Theme') %>%
ph_with(.,
value=pptx.tbl %>%
padding(padding.top = 0, padding.bottom = 0.5) %>%
height_all(0.45),
location=ph_location(type='body'))

Second line of text in pptx subtitle

I have added a Title Slide to a pptx object, and want to add a second line in the subtitle location.
I've tried using ph_add_fpar function with no success. I'm still new with the package so probably not using the correct functions!
mypowerpoint <- read_pptx() %>%
add_slide("Title Slide","Office Theme") %>%
ph_with("Flashy Title",ph_location_type("ctrTitle",position_right = TRUE)) %>%
ph_with("Catchy Subtitle 1",ph_location_type("subTitle")) %>%
ph_with("Catchy Subtitle 2",ph_location_type("subTitle"))
Running the above gives me two subtitles overlaid on top of each other, rather than the second subtitle text as a new line in the same objects as the first. Can anyone please tell me what I'm missing, or a better function to use for this?
ph_with support vector with length > 1
library(magrittr)
library(officer)
mypowerpoint <- read_pptx() %>%
add_slide("Title Slide","Office Theme") %>%
ph_with("Flashy Title",ph_location_type("ctrTitle",position_right = TRUE)) %>%
ph_with(c("Catchy Subtitle 1", "Catchy Subtitle 2"),ph_location_type("subTitle"))

insert an external docx using "officer" pakage

Why doesn't work body_add_docx method in package "officer"? Where did I make mistake?
library(officer)
library(magrittr)
read_docx(path = "/home/user/page1.docx") %>% # load page1.docx as base document
body_add_break() %>% # add page break
body_add_docx(src="/home/user/page2.docx") %>% #FIXME: This method doesn't work
print(target = "/home/user/out.docx") # out.docx conteins only page1.docx !?
Code below works only for Windows, MS Word and only without page break.
For Linux, LibreOffice, google document it doesn't work.
library(officer)
library(magrittr)
read_docx(path = "/home/user/page1.docx") %>%
# body_add_break() %>% # with page break it doesn't work
body_add_docx(src="/home/user/page2.docx") %>% # only for Widows and MS Word
print(target = "/home/user/out.docx")
The function body_add_docx is using a MS Word feature. When the document is edited, the content of the file is copied in the main document, but that only happens when the document is edited by Word. LibreOffice and gdoc probably don't have this feature implemented (at least I am not aware of them).
The script below is producing the expected document only when edited with Word:
library(officer)
library(magrittr)
read_docx() %>%
body_add_par("hello world 1", style = "Normal") %>%
print(target = "doc1.docx")
read_docx() %>%
body_add_par("hello world 2", style = "Normal") %>%
print(target = "doc2.docx")
read_docx(path = "doc1.docx") %>%
body_add_break() %>%
body_add_docx(src="doc2.docx") %>%
print(target = "out.docx")

How to delete blank line at beginning of docx from officer

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!
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")

Resources