Comments (26)
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.
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.
This'd be super handy - trying to produce a pdf_document including the diagram won't work at the mo
from diagrammer.
@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.
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.
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.
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.
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.
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.
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.
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.
@timelyportfolio that sounds sensible. Thanks for helping with this!
from diagrammer.
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.
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.
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.
@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.
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.
My googling finds an npm module which could be helpful? https://www.npmjs.com/package/svg-to-png
from diagrammer.
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.
@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.
very nice, thanks for the note!
from diagrammer.
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.
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.
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.
@gd047 that would be a Q better asked as a separate issue rather than on this closed issue
from diagrammer.
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)
- Avoid nodes overlapping
- Integrate r variables in Mermaid Graph HOT 1
- github website link leads to spam HOT 1
- influenceR orphaned dependency HOT 4
- Release DiagrammeR 1.0.10
- export_graph layout
- render_graph layout
- `generate_dot()` uses single quotes instead of double quotes HOT 1
- as_svg produces `object 'display' not found' error
- Are the new features of gantt charts mermaid.js updated? HOT 2
- Error in render_graph HOT 1
- Test relies on specific output from sample_islands() for given seed HOT 4
- mermaid sequenceDiagram not working? HOT 2
- Documentation checks
- Quarto 1.4 - Cannot render `grVIz()` chart into HTML
- Error in `s$close()`: ! attempt to apply non-function
- Release DiagrammeR 1.0.11 HOT 6
- Maybe need `.name_repair`, according to warning message HOT 1
- DiagrammeR Graph not displayed when rendering in Quarto HOT 3
- Feature Request: Support for lavaan EFA
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from diagrammer.