How do I export and host a ggvis chart on my own w

2020-06-06 04:56发布

问题:

As I understand ggvis runs on top of shiny. But I can't figure out how to export all the files needed for running a single ggvis chart on a webserver without R/shiny.

回答1:

That won't be possible for interactive plots since "every interactive ggvis plot must be connected to a running R session" (via ggivs basics). However, it is possible with ones that don't require reactive values.

CAVEAT ggvis is still rly new, so YMMV with 0.2.x for the following+.

I gave the various ggvis/knitr print functions a quick try but didn't manage to generate the whole document (i.e with the necessary libraries). If you do invoke a standard, non-interactive ggvis with something like:

library(ggvis)
p <- ggvis(mtcars, x = ~wt, y = ~mpg)
p

and do a "view in browser", you'll see that the directory it creates has a main HTML file and a libs directory. You'll need all those javascript libraries in your site and then an outer template like:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="lib/jquery-1.11.0/jquery.min.js"></script>
<link href="lib/jquery-ui-1.10.4/css/smoothness/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
<script src="lib/jquery-ui-1.10.4/js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="lib/d3-3.4.1/d3.min.js"></script>
<script src="lib/vega-1.3.3/vega.min.js"></script>
<script src="lib/lodash-2.2.1/lodash.min.js"></script>
<script>var lodash = _.noConflict();</script>
<link href="lib/ggvis-0.3.0.99/css/ggvis.css" rel="stylesheet" />
<script src="lib/ggvis-0.3.0.99/js/ggvis.js"></script>
<script src="lib/shiny-ggvis-0.3.0.99/js/shiny-ggvis.js"></script>

</head>
<body>

</body>
</html>

You can get the ggvis plot HTML via:

library(knitr)
q <- knit_print.ggvis(p)
writeLines(as.character(q), "~/SOMEDIR/ggvis.html")

and then just insert it in the body tag.

If you poke around a bit more at the ggvis source at github, you might be able to figure out how to use view_static (from ggvis) to get it to this automatically. I'm also pretty sure that there's a way to knit from an rmarkdown file directly to html, but I'm kinda staying clear of ggvis until at least 0.4/0.5 for pseudo-production vis.



标签: r shiny ggvis