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")
Related
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?
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"))
I would like to be able to cross-reference a table or figure in a word document using the officer R package.
I have come across these materials so far but they do not seem to have a solution:
https://davidgohel.github.io/officer/articles/word.html#table-and-image-captions
and a similar question
add caption to flextable in docx
In both of these I can only insert a caption as a level 2 header and not a true table caption.
What I want to be able to do in Word is Insert -> Cross-reference and go to Reference type: Table and see my caption there. Right now I can only see the caption under Numbered item.
Does this functionality exist in officer or anywhere else?
In word, the table numbers use the { SEQ \\# arabic } pattern, but references to them use { REF bookmark \h }. We can use this to make new code which can reference a SEQ field.
code:
ft <- regulartable(head(iris)) # create flextable
str <- paste0(' REF ft \\h ') # create string to be used as reference to future bookmark
doc <- read_docx() %>%
body_add_par('This is my caption' , style = 'Normal') %>% # add caption
slip_in_seqfield(str = "SEQ Table \\# arabic",
style = 'Default Paragraph Font',
pos = "before") %>% # add number for table
body_bookmark('ft') %>% # add bookmark on the number
slip_in_text("Table ",
style = 'Default Paragraph Font',
pos = "before") %>% # add the word 'table'
body_add_flextable(value = ft, align = 'left') %>% # add flextable
body_add_break() %>% # insert a break (optional)
slip_in_text('As you can see in Table',
style = 'Default Paragraph Font',
pos = 'after') %>% # add the text you want before the table reference
slip_in_seqfield(str = str,
style = 'Default Paragraph Font',
pos = 'after') %>% # add the reference to the table you just added
slip_in_text(', there are a lot of iris flowers.',
style = 'Default Paragraph Font',
pos = 'after') %>% # add the rest of the text
print('Iris_test.docx') # print
Hope this helps :)
Just for the record, you can do this a bit easier now by using some helper functions from the {crosstable} package.
Disclaimer: I am the developer of that package and these functions were highly inspired by #morgan121's answer. Thanks Morgan!
Here is an example:
library(officer)
library(crosstable)
library(ggplot2)
options(crosstable_units="cm")
ft = regulartable(head(iris))
my_plot = ggplot(data = iris ) +
geom_point(mapping = aes(Sepal.Length, Petal.Length))
doc = read_docx() %>%
body_add_title("Dataset iris", 1) %>%
body_add_normal("Table \\#ref(table_iris) displays the 6 first rows of the iris dataset.") %>%
body_add_flextable(ft) %>%
body_add_table_legend("Iris head", bookmark="table_iris") %>%
body_add_normal("Let's add a figure as well. You can see in Figure \\#ref(fig_iris) that sepal length is somehow correlated with petal length.") %>%
body_add_figure_legend("Relation between Petal length and Sepal length", bookmark="fig_iris") %>%
body_add_gg2(my_plot, w=14, h=10, scale=1.5)
print(doc , 'Iris_test.docx')
More info on https://danchaltiel.github.io/crosstable/articles/crosstable-report.html.
As with morgan121's code, you have to select all the text in MS Word and press F9 twice for the numbers to update properly.
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")
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")