library(officer) library(flextable) library(ggplot2)
I'm creating a shiny app that generates a word document, and I can do that but I also want the users to preview the results before they generate it. I am using pandoc_convert but the plot doesn't display.
I'm not putting my shiny app here as the only problem is with converting the word file to html format.
# Create a ggplot
plot_ggplot <- function() {
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
labs(title = "Scatter Plot of Sepal Length vs Sepal Width",
x = "Sepal Length",
y = "Sepal Width")
}
# Create a FlexTable
flex_table <- flextable(head(iris))
# Create a Word document
doc <- read_docx()
# Add the FlexTable and ggplot to the document
doc <- body_add_flextable(doc, value = flex_table)
doc <- body_add_gg(doc, value = plot_ggplot())
# Save the Word document
output_file <- "iris_visualizations.docx"
print(doc, target = output_file)
# Convert Word document to HTML
output_html_file <- "iris_visualizations.html"
pandoc_convert(output_file, to = "html", output = output_html_file)