Export Plotly graphs into MS PowerPoint

Hi All,

I'm working on an R shiny interface wherein the requirement is to export a plot directly into MS-PowerPoint. I've searched everywhere on the Internet but couldn't find any relevant link.

Please note, I'm able to export the plot using "ggplot2" graphs but we are using a "plotly" graph as its UI appears to be more pleasing to the eye compared to a "ggplot2" graph.

I would request if anyone could help me with the above request.

Thanks.

Another question recently asked about saving a plotly chart to a png. Is this helpful?

library(plotly)
library(htmlwidgets)
library(webshot)
df <- mtcars
x <- plot_ly() %>%
  add_markers(data=mtcars, x=~wt, y=~disp)
x
saveWidget(x, "temp.html")
webshot("temp.html", "temp.png")

What package are you using for exporting the charts to Powerpoint?

1 Like

Hi Woodward,

Thanks for the above code. However, it's for exporting a plotly chart to png.

I am using "officer" and "svg" packages for exporting charts to power-point. And, it works fine for ggplot2 charts but not for plotly charts.

Code:
count.data <- data.frame(
class = c("1st", "2nd", "3rd", "Crew"),
n = c(325, 285, 706, 885),
prop = c(14.8, 12.9, 32.1, 40.2)
)

count.data <- count.data %>%
arrange(desc(class)) %>%
mutate(lab.ypos = cumsum(prop) - 0.5*prop)

test_donut1 <- ggplot(count.data, aes(x = "", y = prop, fill = class)) +
geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar("y", start = 0)+
geom_text(aes(y = lab.ypos, label = prop), color = "white")+
theme_void()

library("tidyverse")
library("officer")
library("rvg")

Create a new powerpoint document

doc <- read_pptx()
doc <- add_slide(doc, 'Title and Content', 'Office Theme')

Add the plot

doc <- ph_with_vg(doc, code = print(test_donut1), type = 'body')
print(doc, target = 'C:\plot7.pptx')

Hi,

I've figured out a workaround for exporting "Plotly" graphs to PowerPoint.
I'm listing the code below for anyone's reference:

library(officer)
library(magrittr)
library(plotly)
library(htmlwidgets)
library(webshot)

temp_file_proc <- tempfile(pattern = "", fileext = ".png")
outout_file_proc <- paste("C:\", Sys.getenv("USERNAME"), "_ ".pptx", sep = "")

df <- mtcars
x <- plot_ly() %>% add_markers(data=mtcars, x=~wt, y=~disp)

saveWidget(x, "temp.html")
webshot("temp.html", temp_file_proc)

doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(x = doc, value = "Title of the PPT", location = ph_location_type(type = "title"))
image_add <- external_img(temp_file_proc, width = 5, height = 5)
doc <- ph_with(x = doc, image_add,
location = ph_location(left = 2.5, top = 2), use_loc_size = FALSE)

print(doc, target = outout_file_proc)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.