2

Here is an example and the output is a png file hsa04110.gse16873.png. My question is how to get the plot displayed directly rather than save it as a file.

library(pathview)
data(gse16873.d)
data(demo.paths)
data(paths.hsa)
pathview(gene.data = gse16873.d[, 1], pathway.id = demo.paths$sel.paths[1],
                   species = "hsa", out.suffix = "gse16873", kegg.native = T)
#> 'select()' returned 1:1 mapping between keys and columns
#> Info: Working in directory D:/github/RNASeq-KEGG
#> Info: Writing image file hsa04110.gse16873.png
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
David Z
  • 6,641
  • 11
  • 50
  • 101
  • It would have been good to actually be able to download and test the data you use, so that we can reproduce the issue (and any answers). – not2qubit Feb 01 '21 at 09:31

1 Answers1

7

It looks like pathview only renders PNGs, so the trick is to define a function that will show the created PNG in the plot window and by default delete the file itself afterwards as if you were viewing it in memory. As an option, you can keep a copy of the PNG.

require(pathview)
require(grid)
require(png)

see_pathview <- function(..., save_image = FALSE)
{
  msg <- capture.output(pathview::pathview(...), type = "message")
  msg <- grep("image file", msg, value = T)
  filename <- sapply(strsplit(msg, " "), function(x) x[length(x)])
  img <- png::readPNG(filename)
  grid::grid.raster(img)
  if(!save_image) invisible(file.remove(filename))
}

Now when you do:


data(gse16873.d)
data(demo.paths)
data(paths.hsa)

see_pathview(gene.data = gse16873.d[, 1], 
             pathway.id = demo.paths$sel.paths[1],
             species = "hsa", 
             out.suffix = "gse16873", 
             kegg.native = T)

You don't get any messages informing you about a written file, there is no new png in your working directory, and this picture pops up in the viewer pane:

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87