awalker89 / openxlsx

R package for .xlsx file reading and writing.
Other
363 stars 79 forks source link

insert plot inside RStudio chunk fails #529

Closed splaisan closed 3 years ago

splaisan commented 3 years ago

Hi,

I tried this code in a Rscript and it works When used in a Rmd chunk, it does not and returns No plot to insert.NULL although the plot is shown below the chunk block after the print(p0) command (the plot is not added to the plot pane which is I think the reason for the fail)

Can I make this to work in RStudio in a Rmd file?

Thanks

p0a <- ggplot(read.data, aes(y=read, x=sampleID)) + 
  geom_bar(position="stack", stat="identity") +
  coord_flip() +
  theme(axis.text.y=element_text(size=rel(0.75))) +
  ggtitle("Demultiplexed read pair counts")

p0b <- ggplot(read.data, aes(y=read)) + 
  geom_boxplot(outlier.colour="black", outlier.shape=16, outlier.size=2, notch=TRUE) +
  theme(axis.title.y=element_blank(), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())

p0 <- ggarrange(p0a, p0b, nrow = 1, widths = c(6,1))

# add to excel file
wb <- createWorkbook()

# add worksheets
addWorksheet(wb, "read_count")
writeData(wb, "read_count", read.data[, c('sampleID', 'read')], startCol = 1, startRow = 1, rowNames = FALSE)
print(p0)
insertPlot(wb, "read_count", xy = c("D", 2), width = 5, height = 3.5, fileType = "png", units = "in")

addWorksheet(wb, "chain_count")
addWorksheet(wb, "clonotype_count", tabColour = "red")

saveWorkbook(wb, file = "runQC/results.xlsx", overwrite = TRUE)
splaisan commented 3 years ago

found a fix, changing the way plots are inserted as described in https://community.rstudio.com/t/plots-not-showing-up-in-the-plots-pane-in-rstudio/3981/5 did it

5a92b2d3ac92711d7c762a963fdd4efe8b4b7025_2_351x500