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?
You have at least two options to accomplish this:
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
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')
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)
}