Closed vbakella closed 8 years ago
I don't believe that the 'rstudioapi' package has any function to do this. But, this is a feature I've been wanting to add to the package (along with other export options as calls). I'll find out more and update soon.
This'd be super handy - trying to produce a pdf_document including the diagram won't work at the mo
@jjallaire is there any way currently (or planned) to export Viewer graphics via function calls? This is an often-requested feature (probably the most requested).
The Viewer graphics export relies on doing a screen capture within RStudio's QtWebKit wrapper so there isn't really a way to do that via a function call.
What I'd recommend is experimenting with creating screenshots via phantom.js (http://phantomjs.org) and then add a function to the package which will produce a bitmap version if if the user has phantom.js installed and on their PATH.
On Thu, Apr 2, 2015 at 10:24 AM, Richard Iannone notifications@github.com wrote:
@jjallaire https://github.com/jjallaire is there any way currently (or planned) to export Viewer graphics via function calls? This is an often-requested feature (probably the most requested).
— Reply to this email directly or view it on GitHub https://github.com/rich-iannone/DiagrammeR/issues/70#issuecomment-88927199 .
I'll try to demo something. I think a lot of these requests can be satisfied by offering an export (like ggvis
) on the client side after the page is rendered. Also, RStudio has the nice feature of allowing (manual, not with a function) export of rendered html/htmlwidgets.
Guess I can try to tackle some of this in next week's widget of the week.
We could extend saveWidget to optionally call phantom.js -- that way all widgets could take advantage of this.
On Thu, Apr 2, 2015 at 11:37 AM, timelyportfolio notifications@github.com wrote:
I'll try to demo something. I think a lot of these requests can be satisfied by offering an export (like ggvis) on the client side after the page is rendered. Also, RStudio has the nice feature of allowing (manual, not with a function) export of rendered html/htmlwidgets.
Guess I can try to tackle some of this in next week's widget of the week.
— Reply to this email directly or view it on GitHub https://github.com/rich-iannone/DiagrammeR/issues/70#issuecomment-88950056 .
I think the webshot package by @wch can come in handy here, since it already wraps phantom.js
and provides an R friendly interface. As @jjallaire said, we could wrap some of this functionality in saveWidget
.
Webshot doesn't actually wrap phantom.js
; it just uses it if it's available. Though it would be nice if phantomjs were easy to install from R. It might also be worth looking into whether the V8 package could be used, though @timelyportfolio, I'm sure you know more about that.
Had a look at phantom.js
and it looks great for this. @timelyportfolio : would including the library locally and calling via V8
mean that V8
would again be a required package rather than suggested?
phantom.js did not work for me in V8
last time I tried, but I can't remember why. I'll try again. @jeroenooms, have you tried phantom.js?
I definitely do not think we should include phantom.js
in DiagrammeR
.
I think that in the first pass we just require that folks have or can install phantom.js
. Then webshot
would be a great way to add the functionality.
@timelyportfolio that sounds sensible. Thanks for helping with this!
Practically speaking you can't include phantom.js in a CRAN package (it includes all of WebKit which is a huge dependency that probably can't be built on CRAN's servers). I think the best we can do is tell users to install it and then everything will work swimmingly.
On Thu, Apr 2, 2015 at 5:12 PM, Richard Iannone notifications@github.com wrote:
@timelyportfolio https://github.com/timelyportfolio that sounds sensible. Thanks for helping with this!
— Reply to this email directly or view it on GitHub https://github.com/rich-iannone/DiagrammeR/issues/70#issuecomment-89045734 .
Should reference https://github.com/ramnathv/htmlwidgets/issues/95 since I think this issue is htmlwidget-wide.
@vbakella, this is experimental and early, but I would love feedback on exportwidget if you have a chance to try it out.
Now that DiagrammeR supports render_graph(output="SVG")
seems like that might be a promising way to work around this?
For instance, I'm just writing out that svg
file and then using a system call to have inkscape convert the svg to a png of the desired resolution. Perhaps if there is a more portable library than inkscape to convert from svg it would be possible for DiagrammeR objects to act like other graph objects in rstudio where the output format can be controlled from knitr options, etc.
@cboettig thanks for bringing this to the fore, because I really would like to get this functionality in. Inkscape is a good option, as is ImageMagick, but that assumes that either of these is installed.
I suppose this option is better than nothing. I haven't really found a lightweight SVG to PNG or PDF conversion tool as a JS library. Somebody posted a pretty interesting workaround but it involved some hoops as well.
If there is not much in the way of options, I might just provide a choice of Inkscape or ImageMagick conversion. Comments here definitely welcome!
Haven't tried wrapping python libraries in an R package, but perhaps http://cairosvg.org/ is a possible option that might be more lightweight than inkscape or imagemagick?
My googling finds an npm module which could be helpful? https://www.npmjs.com/package/svg-to-png
Thanks for the links @cboettig and @stephlocke. The npm package requires phantom.js, which we tried to avoid because of its own build dependencies. The Python library looks promising.
Also, from this Stack Overflow question (http://stackoverflow.com/questions/3975499/convert-svg-to-image-jpeg-png-etc-in-the-browser) there might be two viable solutions. One involves a simple JS library called Pablo. It has a toImage() function which is supposed to convert SVG to PNG.
Will need to do some testing...
@vbakella @stephlocke @jjallaire @cboettig @ramnathv @timelyportfolio @wch
This is an old issue, but finally solved thanks to the availability of the rsvg package. A new DiagrammeR function export_graph
allows for exporting a graph to a few different image file formats. An example:
devtools::install_github("rich-iannone/DiagrammeR")
devtools::install_github("rich-iannone/DiagrammeRsvg")
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
# Create a random graph, change the layout and
# some node attributes, then save as a PDF file
# in the working directory
create_random_graph(
30, 50,
directed = TRUE,
fully_connected = TRUE,
set_seed = 25) %>%
set_global_graph_attr("graph", "layout", "twopi") %>%
set_global_graph_attr("node", "shape", "circle") %>%
set_global_graph_attr("node", "color", "lightblue") %>%
export_graph("random_graph.pdf")
Other file types include png
, svg
, and ps
. Try it out!
very nice, thanks for the note!
This is great, although I can't see a way to use it for graph created from a dot file - is that possible?
@puterleat
It's a bit manual because not wrapped in an explicit function, but:
devtools::install_github("rich-iannone/DiagrammeR")
devtools::install_github("rich-iannone/DiagrammeRsvg")
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
library(rsvg)
grViz(diagram = "~/Desktop/dot_file_from_the_internet.dot") %>%
export_svg %>% charToRaw %>% rsvg_pdf("graph.pdf")
The dot file I used (most valid ones will work):
graph {
rankdir=LR; // Left to Right, instead of Top to Bottom
a -- { b c d };
b -- { c e };
c -- { e f };
d -- { f g };
e -- h;
f -- { h i j g };
g -- k;
h -- { o l };
i -- { l m j };
j -- { m n k };
k -- { n r };
l -- { o m };
m -- { o p n };
n -- { q r };
o -- { s p };
p -- { s t q };
q -- { t r };
r -- t;
s -- z;
t -- z;
}
I'll try to make this work better. One obvious improvement is adding DOT import in the import_graph
function.
That's nice... at present, if it's useful to anyone else, this is what I'm doing:
```{r test-bash, engine='bash'}
dot -Tpdf model.dot > model.pdf
![Model caption \label{fig:model}](model.pdf)
Although obviously you can't knit to both pdf and html this way because you'll need a png for the html.
@rich-iannone
It's a bit manual because not wrapped in an explicit function, but:
devtools::install_github("rich-iannone/DiagrammeR") devtools::install_github("rich-iannone/DiagrammeRsvg") library(DiagrammeR) library(DiagrammeRsvg) library(magrittr) library(rsvg) grViz(diagram = "~/Desktop/dot_file_from_the_internet.dot") %>% export_svg %>% charToRaw %>% rsvg_pdf("graph.pdf")
The dot file I used (most valid ones will work):
graph { rankdir=LR; // Left to Right, instead of Top to Bottom a -- { b c d }; b -- { c e }; c -- { e f }; d -- { f g }; e -- h; f -- { h i j g }; g -- k; h -- { o l }; i -- { l m j }; j -- { m n k }; k -- { n r }; l -- { o m }; m -- { o p n }; n -- { q r }; o -- { s p }; p -- { s t q }; q -- { t r }; r -- t; s -- z; t -- z; }
I'll try to make this work better. One obvious improvement is adding DOT import in the
import_graph
function.
I use your solution to save a grViz diagram (containing greek characters) to pdf. The greek characters are displayed correctly as long as I use the 2.2 version of V8 package. If I go to v.3.0.1 the greek characters are no longer displayed correctly.
What fix would you suggest, in order to avoid a version rollaback? Thank you.
@gd047 that would be a Q better asked as a separate issue rather than on this closed issue
Guessing a lot of people wanting to save graphs will find this issue. Might be worth a link to a working/current example as export_svg()
is now not found, likewise set_global_graph_attr
and
Error: 'create_random_graph' is not an exported object from 'namespace:DiagrammeR'
Hi,
I have used this package and find it extremely useful. Thank you for all the great work.
I have built a Shiny based program that makes calls to GraphViz and Mermaid using this package. Any thoughts on how I could save the output to a png or pdf file?
I recall rCharts had a call to generate a standalone html file. Do we have an equivalent call in this package to save the plot as a file?
Thanks