I am trying to add a column with images to the table :
In this example, I use the same image in each row, but my actual data has a different image.
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
data = iris %>% mutate(path= img.file)
myft <- flextable( head(data))
I am following examples listed in fleaxtable table ,
myft <- compose( myft, j = "path",
value = as_paragraph(
as_image(src = path, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
when I run it, I get error :
I try to read the documentation, but I can't find any explanation of what to do to fix the error. In the code I provided, it actually adds an image and text "blah blah" to the cell, but all I want is to render a path as an image. I tried to shorten the code to the following:
myft <- compose( myft, j = "path",
value = as_paragraph(
as_image(src = path, width = .20, height = .15))
),
part = "body")
But it did not work.
Really appreciate your help!
Used a wrong function! The colformat_image() do teh trick
myft <- colformat_image( myft, j = "path", width = .20, height = .15)
Related
How do I add a hyperlink to the text in a cell of a flextable? The hyperlink should be underlined and open in a new tab.
In the R script below, This is a link to google. should be the hyperlink.
I included the hyperlink_text example from the documentation which results in an error for me.
library(tidyverse)
library(flextable)
df <- tibble(
desc = c("R1", "R2", "R3"),
A = c("This is a long sentence that should go over into B's column. This is a link to google.", "just one cell in colA", "just one cell in colA"),
B = c("", "just one cell in colB", "just one cell in colB"),
C = c("just one cell in colC", "just one cell in colC", "just one cell in colC")
)
flextable(df) |>
merge_at(i = 1, j = 2:3)
# hyperlink_text p.103
# From documentation: https://cran.r-project.org/web/packages/flextable/flextable.pdf
dat <- data.frame(
col = "Google it",
href = "https://www.google.fr/search?source=hp&q=flextable+R+package",
stringsAsFactors = FALSE)
ftab <- flextable(dat)
ftab <- compose( x = ftab, j = "col",
value = as_paragraph(
"This is a link: ",
hyperlink_text(x = col, url = href ) ) )
# Error in data.frame(x = x, url = url, stringsAsFactors = FALSE) :
# object 'href' not found
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_)
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:
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
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.