0
library(rsvg)


str <- charToRaw('<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
  <style>
    circle {
      fill: gold;
      stroke: maroon;
      stroke-width: 10px;
    }
  </style>

  <circle cx="150" cy="150" r="100" />
</svg>')

rsvg_png(str, file = 'ex1.png') # repeat. I want to remove the save but render on GUI

How do I have image in a pop-up? Everytime I make change, I have to save an image, open it and repeat. with ggplot2 if there is a plot object once typing it on GUI console an image is shown.

I've tried

str

plot.new()
str
dev.off()

I've attempted various combinations of plot and printing the string but in vain. Any suggestions that can render the SVG in a pop-up with R GUI console?

Death Metal
  • 830
  • 3
  • 9
  • 26

1 Answers1

1

You have at least two options to accomplish this:

  1. create a new plot, read in the image file, and draw it on the plot. This will be shown on the image device, e.g., x11, a pdf, the Rstudio image viewer pane ("Plots"), etc depending on which application you are using; see f below

  2. generate an html file to link to the image file. This can then be opened in your default browser or the Rstuio viewer pane ("Viewer") depending on which you are using; see g below


library('rsvg')
str <- charToRaw('<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg">
  <style>
    circle {
      fill: gold;
      stroke: maroon;
      stroke-width: 10px;
    }
  </style>

  <circle cx="150" cy="150" r="100" />
</svg>')

rsvg_png(str, file = '~/desktop/ex1.png')

## open in the R/RGui/Rstudio image viewer
f('~/desktop/ex1.png')

## open in Rstudio viewer or browser in R/Rgui
g('~/desktop/ex1.png')

enter image description here

functions:

## image viewer
f <- function(img) {
  img <- png::readPNG(img)
  plot.new()
  plot.window(0:1, 0:1, asp = 1)
  rasterImage(img, 0, 0, 1, 1)
}

## html viewer/browser
g <- function(img, use_viewer = TRUE) {
  file.copy(img, tempdir(), overwrite = TRUE)
  tmp <- tempfile(fileext = '.html')
  writeLines(sprintf('<img src="%s">', basename(img)), con = tmp)
  
  if (use_viewer)
    tryCatch(
      rstudioapi::viewer(tmp),
      error = function(e) browseURL(tmp)
    )
  else browseURL(tmp)
}
rawr
  • 20,481
  • 4
  • 44
  • 78
  • Yes, it is really so close to what I want. Wha I'm not using Rstudio but R GUI (console in windows and OS X). How do I achieve the same with console? – Death Metal Aug 01 '20 at 14:42
  • 1
    @DeathMetal I haven't used the r gui in a while, I'm not sure if it has a window to view html pages, that code should open in your default browser if you don't use rstudio. The window you refer to I think is just the one for plots (this is a separate pane in rstudio), and you would need to look up how to insert a specific image format on an existing plot. for example, since you create a png, you can use `img <- png::readPNG('~/desktop/ex1.png'); plot.new(); p <- par('usr'); rasterImage(img, p[1], p[3], p[2], p[4])` is that closer to what you need? – rawr Aug 01 '20 at 19:43
  • 1
    or maybe `img <- png::readPNG('~/desktop/ex1.png'); plot.new(); plot.window(0:1, 0:1, asp = 1); rasterImage(img, 0, 0, 1, 1)` would look nicer – rawr Aug 01 '20 at 19:45
  • Thank you so much. `rasterImage(img, 0, 0, 1, 1)` worked on R GUI and this was what I had wanted. I've not tried the earlier one. Thank you very much for your kin replies and time. – Death Metal Aug 03 '20 at 18:22
  • If you can make changes with the `png::` line of code addition to your initial post, I'll accept the answer :) – Death Metal Aug 03 '20 at 18:35
  • @DeathMetal sure, I'll just leave the other option up in case it is useful to someone else – rawr Aug 03 '20 at 19:19