Reputation: 21
First, I'm using R 3.6.0 and Rstudio 1.2 with Windows 10.
I am using flextable and Officer to create a Word document. Into this table I insert some images. To do this I am using flextable. When I use this code with a R script and officer that work. But, When I use this code in Rmarkdown for generate a Word document, that doesn't work. The code Under Rmardown:
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
myft <- flextable( head(iris))
myft <- compose( myft, i = 1:3, j = 1,
value = as_paragraph(
as_image(src = img.file, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
myft
I have a message that tell me: "Sorry, we can't open the document because we have discovered a problem with its content.
I think there is a problem with the image in flextable. When I remove these image, that work.
Upvotes: 2
Views: 1887
Reputation: 10675
Yes, insertion of images in flextable is not supported with rmarkdown::word_document
.
You will need package officedown
to be able to embed images in flextable with R Markdown for Word. You only need to replace output: rmarkdown::word_document
by
output: officedown::rdocx_document
.
---
date: "`r Sys.Date()`"
author: "Your Name"
title: "Untitled"
output:
officedown::rdocx_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.cap = TRUE)
library(officedown)
```
```{r}
library(flextable)
library(officer)
img.file <- file.path( R.home("doc"), "html", "logo.jpg" )
myft <- flextable( head(iris))
myft <- compose( myft, i = 1:3, j = 1,
value = as_paragraph(
as_image(src = img.file, width = .20, height = .15),
" blah blah ",
as_chunk(Sepal.Length, props = fp_text(color = "red"))
),
part = "body")
autofit(myft)
```
To install the package, run the following command (not yet on CRAN): remotes::install_github("davidgohel/officedown")
Upvotes: 3