Giter Club home page Giter Club logo

Comments (26)

rich-iannone avatar rich-iannone commented on May 18, 2024 4

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

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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.

from diagrammer.

stephlocke avatar stephlocke commented on May 18, 2024

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

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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

from diagrammer.

jjallaire avatar jjallaire commented on May 18, 2024

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 [email protected]
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)
.

from diagrammer.

timelyportfolio avatar timelyportfolio commented on May 18, 2024

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.

from diagrammer.

jjallaire avatar jjallaire commented on May 18, 2024

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 [email protected]
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)
.

from diagrammer.

ramnathv avatar ramnathv commented on May 18, 2024

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.

from diagrammer.

wch avatar wch commented on May 18, 2024

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.

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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?

from diagrammer.

timelyportfolio avatar timelyportfolio commented on May 18, 2024

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.

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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

from diagrammer.

jjallaire avatar jjallaire commented on May 18, 2024

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 [email protected]
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)
.

from diagrammer.

timelyportfolio avatar timelyportfolio commented on May 18, 2024

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.

from diagrammer.

cboettig avatar cboettig commented on May 18, 2024

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.

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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

from diagrammer.

cboettig avatar cboettig commented on May 18, 2024

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?

from diagrammer.

stephlocke avatar stephlocke commented on May 18, 2024

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

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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

from diagrammer.

rich-iannone avatar rich-iannone commented on May 18, 2024

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

from diagrammer.

cboettig avatar cboettig commented on May 18, 2024

very nice, thanks for the note!

from diagrammer.

puterleat avatar puterleat commented on May 18, 2024

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

from diagrammer.

puterleat avatar puterleat commented on May 18, 2024

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.

from diagrammer.

 avatar commented on May 18, 2024

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

from diagrammer.

stephlocke avatar stephlocke commented on May 18, 2024

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

from diagrammer.

tbates avatar tbates commented on May 18, 2024

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'

from diagrammer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.