Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving a Shiny-Graphviz plot to a png or pdf file #70

Closed
vbakella opened this issue Feb 27, 2015 · 26 comments
Closed

Saving a Shiny-Graphviz plot to a png or pdf file #70

vbakella opened this issue Feb 27, 2015 · 26 comments
Assignees
Milestone

Comments

@vbakella
Copy link

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

@rich-iannone
Copy link
Owner

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.

@stephlocke
Copy link
Contributor

This'd be super handy - trying to produce a pdf_document including the diagram won't work at the mo

@rich-iannone
Copy link
Owner

@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).

@jjallaire
Copy link
Contributor

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
#70 (comment)
.

@timelyportfolio
Copy link
Contributor

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.

@jjallaire
Copy link
Contributor

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
#70 (comment)
.

@ramnathv
Copy link

ramnathv commented Apr 2, 2015

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.

@wch
Copy link

wch commented Apr 2, 2015

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.

@rich-iannone
Copy link
Owner

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?

@timelyportfolio
Copy link
Contributor

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.

@rich-iannone
Copy link
Owner

@timelyportfolio that sounds sensible. Thanks for helping with this!

@jjallaire
Copy link
Contributor

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
#70 (comment)
.

@timelyportfolio
Copy link
Contributor

Should reference ramnathv/htmlwidgets#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.

@cboettig
Copy link

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.

@rich-iannone
Copy link
Owner

@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!

@cboettig
Copy link

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?

@stephlocke
Copy link
Contributor

My googling finds an npm module which could be helpful? https://www.npmjs.com/package/svg-to-png

@rich-iannone
Copy link
Owner

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...

@rich-iannone
Copy link
Owner

@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!

@cboettig
Copy link

cboettig commented Feb 3, 2016

very nice, thanks for the note!

@puterleat
Copy link

This is great, although I can't see a way to use it for graph created from a dot file - is that possible?

@rich-iannone
Copy link
Owner

@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.

@puterleat
Copy link

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.

@ghost
Copy link

ghost commented Feb 27, 2020

@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.

@stephlocke
Copy link
Contributor

@gd047 that would be a Q better asked as a separate issue rather than on this closed issue

@tbates
Copy link

tbates commented Jan 23, 2021

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants