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"))
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 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.
How can I change the font family of a title in R with officer? I'm trying this with the function fp_text(font.family = "Arial"), but the problem is that the title which I define with fp_text does not end up in the table of contents.
It's kind of a pain, but the way I do it is:
Add an empty placeholder in the title slot with ph_empty()
Add formatted text with ph_add_fpar(), using fpar(), ftext(), and fp_text() to create the formatted text object.
Here is an example of how to change the title on a Title Slide and on a Title and Content slide, assuming the fonts you want to use are "Rage Italic" and "Goudy Stout":
library(officer)
library(magrittr)
# the formatting you want to use goes here -- check your fonts()
format_main_title <- fp_text(font.family='Rage Italic', font.size=72)
format_page_title <- fp_text(font.family='Goudy Stout', font.size=24)
read_pptx() %>%
add_slide(layout = 'Title Slide', master='Office Theme') %>%
ph_empty(type='ctrTitle') %>%
ph_add_fpar(fpar(ftext('Fancy Main Title', prop=format_main_title)),
type = 'ctrTitle') %>%
add_slide(layout = 'Title and Content', master='Office Theme') %>%
ph_empty(type='title') %>%
ph_add_fpar(fpar(ftext('Fancy Page Title', prop=format_page_title)),
type = 'title') %>%
ph_with_text(type = 'body', str = 'Boring stuff goes here') %>%
print('test.pptx')
produces:
You can see these titles in the table of contents:
Having said that -- If you find yourself consistently changing title fonts to the same new formatting you would probably be better off creating a template deck with your own Slide Master (versus the default "Office Theme") that uses your desired font and beginning your chain with that (i.e., read_pptx('your_template.pptx') %>% etc.)
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")
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")