Mutliple formatted text on pptx by using officer package on R - r

Ciao,
I'm writing a code to generate an automatic report using officer package. I was wondering how and if I can write some text with different font. In my case I'd like to write some normal text and some bold words.
Let me show you what I get. I use these functions to generate fp_text objects:
fp_normal <- function(){
return( fp_text(color = "black", font.size = 16,
bold = FALSE, italic = FALSE,
underlined = FALSE, font.family = "Arial",
shading.color = "transparent") )
}
fp_bold <- function(){
return( fp_text(color = "black", font.size = 16,
bold = TRUE, italic = FALSE,
underlined = FALSE, font.family = "Arial",
shading.color = "transparent") )
}
I used to use combination of pot function by using sum operator and function textProperties:
pot("not bold ") + pot("and bold", textProperties(font.weight = "bold") )
My question is: how should I combine fp_normal and fp_bold functions with ph_with_text function?

I have updated the package to make that kind of operation easier. Usage of id_chr is not easy and the code below give the advantage to not use it :)
library(magrittr)
library(officer)
fp_normal <- fp_text(font.size = 24)
fp_bold <- update(fp_normal, bold = TRUE)
fp_red <- update(fp_normal, color = "red")
pars <- block_list(
fpar(ftext("not bold ", fp_normal), ftext("and bold", fp_bold)),
fpar(ftext("red text", fp_red))
)
my_pres <- read_pptx() %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
ph_with(pars, location = ph_location_type(type = "body") )
print(my_pres, target = "test.pptx")

Ok, at the end I think I've got it.
To apply different styles is in enough to combine ph_with_text function with ph_add_text function.
Namely ph_add_text do the same sum operator do for pot function. Keep in mind that, in order to refer to a certain line you have to provide id_chr argument. You can deduce the correct value by using slide_summary(ppt) command just after you run ph_with_text.
ppt <- read_pptx()
ppt <- add_slide(ppt, "Title and Content", master = "Office Theme")
ppt <- ph_with_text(ppt, "Some NOT bold text ", type = "body", index = 1)
slide_summary(ppt) # I see that the id is 2. Now I can delete this line.
ppt <- ph_add_text(ppt, "and some bold text", type = "body", style = fp_bold(), id_chr = 2)
print(ppt, target = "boldTest.pptx")
For the fp_bold() function see above in the question.
Ad this point we can add other pieces of text with different formats by keeping using ph_add_text (and maybe "\n" if we want write in new lines.
Ciao

Related

Can i change the formatting of specific words In my docx file with officer package in R?

I'm trying to change some specific words formatting to bold but can't find the the way to do it.
For example:
I would like to change the word "text" to bold in this code:
doc <- read_docx() %>%
body_add_par("This is a sample text", style = "centered")
How could I do it?
You can use body_add_fpar to achieve that. From the documentation:
bold_face <- shortcuts$fp_bold(font.size = 30)
bold_redface <- update(bold_face, color = "red")
fpar_ <- fpar(ftext("Hello ", prop = bold_face),
ftext("World", prop = bold_redface ),
ftext(", how are you?", prop = bold_face ) )
doc <- read_docx() %>% body_add_fpar(fpar_)

Errors when making tables with the flextable package

So before with reporters i made a matrix consisting purely of text and then made it into a flextable and at last added it to my Word document. That way i created a header for my document. Example:
Header <- matrix("",1,3)
Header[1] <- paste("bla bla")
Header[2] <- paste("blu blu")
Header[3] <- paste("ble ble")
myheader <- FlexTable(data = Header, header.columns = F,
add.rownames = F,
header.cell.props = cellProperties( background.color = "white" ),
header.text.props = textProperties( color = "black",
font.size = 11),
body.text.props = textProperties( font.size = 11,
font.weight = "bold"))
Now when i try to convert it into a flextable with the new function:
myheader <- flextable(Header)
It says
" Error in flextable(Header) : is.data.frame(data) is not TRUE"
or by:
myheader <- flextable(Header, col.keys = names(Header))
It says
" Error in flextable(Header, col.keys = names(Header)) :
unused argument (col.keys = names(Header)"
What am i doing wrong?
Thank you in advance!
Ps. Feel free to modify my visual presentation (im still trying to figure it out on here).
Also i just noticed, that i get the same problem with data tables, so theres definitely something i've missed.
Your Header object is a matrix:
class(Header)
[1] "matrix"
You can convert it to a data.frame using as.data.frame()
library(flextable)
flextable(as.data.frame(Header))
Alternatively, you could use tableHTML which also accepts matrices:
library(tableHTML)
Header %>%
tableHTML(rownames = FALSE,
theme = "scientific")
The result here is:

table caption stays below for xtableFtable in R

I'm trying to use xtableFtable with print to make a LaTeX table with the caption on top. I've tried several different variations of what the documentation says about how to change the caption placement within the print function, but print (as well as print.xtableFtable) ignores what I enter for this parameter and uses its default value (which is "bottom").
Here's my code:
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl,
levels = c("4", "6", "8"),
labels = c("four", "six", "eight"))
tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am,
col.vars = c(2, 3),
dnn = c("Cylinders", "V/S", "Transmission"))
xftbl <- xtableFtable(tbl,
method = "non.compact",
label = "my_table",
caption = "My Table")
print(xftbl, booktabs = TRUE,
caption.placement = getOption("xtable.caption.placement", "top"))
I've also tried these two different variations for the caption placement parameter, but got the same result as above:
print(xftbl, booktabs = TRUE, caption.placement = "top")
and
print(xftbl, booktabs = TRUE, xtable.caption.placement = "top")
All the other parameters appear to work properly, including booktabs. What am I doing wrong with the caption.placement parameter?

officer method ph_empty_at add an empty placeholder specifying also an ID that can be used later to add content to it

I want to add several placeholders to a Power Point slide, in different (fixed) positions, using a predefined template of my company. I use that template only to get the font style, the header and footer of the slides,but not for the content of each slide. For this, I can add text and images wherever I want.
Then I need to add, in each of these placeholders, some text paragraphs.
I can do this, for example, with the code below. But the bad thing is that if I want to add a paragraph to the first placeholder, I have to specify the id_chr of the placeholder (in this example are "2" and "3") which is known only after I add the empty placeholders in the presentation, at runtime.
Is there a way to add an empty placeholder specifying also an ID that can be used later to add content to it using that ID instead of the id_chr that is known only at runtime? Maybe using also ph_label?
require(magrittr)
require(officer)
TARGET_FILE = "ph_add_fpar.pptx"
setwd(file.path("E:", "Work", "Reporting"))
file.remove(TARGET_FILE)
MASTER_LAYOUT = "Blank"
PICTURE_AND_TEXT_PAGE_LAYOUT = "Internal Slide - Picture and Text"
bold_face <- shortcuts$fp_bold(font.size = 30)
bold_redface <- update(bold_face, color = "red")
fpar_1 <- fpar(ftext("Hello ", prop = bold_face),
ftext("World", prop = bold_redface ),
ftext(", \r\nhow are you?", prop = bold_face ) )
fpar_2 <- fpar(ftext("Hello ", prop = bold_face),
ftext("World", prop = bold_redface ),
ftext(", \r\nhow are you again?", prop = bold_face ) )
doc <- read_pptx(path = file.path(getwd(), "Template.pptx")) %>%
add_slide(layout = PICTURE_AND_TEXT_PAGE_LAYOUT, master = MASTER_LAYOUT) %>%
ph_empty_at(left = 1,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
ph_empty_at(left = 4,top = 1,width = 3,height = 2,template_type = "body",template_index = 4) %>%
ph_add_fpar(value = fpar_1, type = "body", level = 1, id_chr = "2") %>%
ph_add_fpar(value = fpar_2, type = "body", level = 1, id_chr = "3")
print(doc, target = TARGET_FILE)
system("cmd.exe", input = TARGET_FILE)
Thank you

Indent a Flextable made with R-ReporteRs

I want to indent a (flex)table created with the ReporterRs package. Here's an example:
library(ReporteRs)
df <- data.frame(Column1 = 1:5, Column2 = c("a","b","c","d","e"))
Mydoc = docx(title="A testdoc for testing tables")
options('ReporteRs-fontsize'=10, 'ReporteRs-default-font'='Arial')
FTab = FlexTable( data = df, add.rownames = FALSE, header.columns = TRUE,
body.text.props = textProperties( font.size = 10,font.family = "Arial" ),
header.text.props = textProperties( font.size = 10,font.family = "Arial", font.weight = "bold"))
FTab = setFlexTableBorders(FTab,inner.vertical = borderNone(),inner.horizontal = borderNone(),
outer.vertical = borderNone(),
outer.horizontal = borderProperties(color = "black", style = "solid", width = 1))
Mydoc = addFlexTable(Mydoc, FTab)
nu <- format(Sys.time(), "%Y%m%d%H%M")
writeDoc(Mydoc, paste0("testreport_",nu,".docx"))
This creates a docx with a left aligned table. I want the table to move 1.5 cm to the right. So no center or right alignment, but an indentation of 1.5 cm. Is this possible? For text, I can use a pre-defined style that indents 1.5 cm, but for tables that doesn't seem possible. Or is it?
As a workaround, I could add an extra column at the left, without any borders or text. But I prefer a neat solution.
From the package documentation, unless you want to write a patch, it seems you'd better go with the invisible column:
Function addFlexTable
add FlexTable to document objects.
Word and html document
Add a FlexTable object in a document object with the function addFlexTable.
Expected arguments are:
the document object
the FlexTable object
eventually a parProperties object to define alignement. Note that with docx objects, only alignment will be used, if you’d like to add space around a table, specify padding on preceding and or following paragraph.

Resources