0

I am currently working on a shiny app. This app generates ggsurvplot and I want to export them in a .docx report to avoid screenshots.

When I'm adding the ggsurvplot in my report with the function body_add_gg() i got this message error : inherits(value, "gg") is not TRUE.

I would like to know if I can incorporate ggsurvplot objects in my report with officeR or if I should create a classic R plot.

I tried to directly change the class of my ggsurvplot with class(p) <- "gg". But it's doesn't work(which is pretty obvious). ggsurvplot and ggplot objects are not exactly made in the same way, so just modifying the class name can't work.

I thank you in advance for your help.

stefan
  • 90,330
  • 6
  • 25
  • 51

1 Answers1

1

A ggsruvplot object is a list where the plot of the survival curves is stored as an element named plot. Hence, one option which works would be to export this element. If you also want the risk table then one option would be to export it separately.

Using a basic minimal reproducible example.

library(survminer)
library(survival)
library(officer)

fit <- survfit(Surv(time, status) ~ sex, data = lung)

gg <- ggsurvplot(fit, data = lung, risk.table = TRUE)

doc <- read_docx()

doc <- body_add_gg(doc, value = gg$plot, style = "centered")

print(doc, target = tempfile(fileext = ".docx"))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51