zoom, pan, and axis rescale

860 views
Skip to first unread message

Jonathan

unread,
May 6, 2014, 11:08:56 AM5/6/14
to shiny-...@googlegroups.com
I'd like to be able to zoom, pan, and rescale axes in XY line and or scatter plots in R Shiny. 

I found the perfect example in D3:
Does anyone know of an R Shiny package with similar capability? Or is there an R Shiny package that makes it easy to interface with D3?

I found this example of interfacing R Shiny with D3, but before I do a deep dive to understand it, thought I'd ask if there are tools out there already?

Thomas Lin Pedersen

unread,
May 9, 2014, 2:13:42 AM5/9/14
to shiny-...@googlegroups.com
This might not be the answer you are looking for but here goes:

You can make whatever plot using d3.js and shiny that you wish, but you would need a minimal of javascript to achieve this. The way I have interfaced shiny and d3 (or the simplest way I have doe it) is to create a javascript function that takes some input data and constructs the plot I want using the d3 framework. I have then defined a custom output for shiny of the type: 

var plot = new Shiny.OutputBinding();
$.extend(plot, {
find: function(scope) {
return $(scope).find(".plot");
},
renderValue: function(el, data) {
if (data) {
myD3plotFunction(data);
};
}
});
Shiny.outputBindings.register(plot, 'thomasp85.plot');

Now I know that this requires you to learn a bit of d3 and javascript, but to the best of my believe this is a fantastic investment compared to learning some of the higher level libraries build on top of d3. The freedom to make any plot, animation and interaction you can dream up is intoxicating. Furthermore there is a wealth of examples on d3 functions that make it easy to pick up the basic and advanced part of the framework (for example the one you linked to).

Hope this helps...

best

Thomas

Familie Gieshoff

unread,
May 12, 2014, 11:55:26 PM5/12/14
to shiny-...@googlegroups.com
Dear Thomas,

As I am not familiar with javascript and interfacing javascript libraries with Shiny, might you please create a complete and simple example to learn the basics?
It would be great.......

Thank you and best regards,
Jürgen

Thomas Lin Pedersen

unread,
May 14, 2014, 6:20:23 AM5/14/14
to shiny-...@googlegroups.com
Have a look at https://github.com/thomasp85/d3Disco

The app does not do anything meaningful be design - the question is about the interface. I would say though that in order to use d3 you should learn a bit of javascript. d3 does not hold your hand in the way that plot() in R does. You are in charge of creating axes and placing them were you want as well as putting data points in the right place and styling them. In that regards it is more akin to the grid graphics system in R (though their syntax differs)...

best

Thomas

Familie Gieshoff

unread,
May 14, 2014, 12:49:33 PM5/14/14
to shiny-...@googlegroups.com
Thank you very much Thomas.....
Your comment is really very valid. After having seen your example i will need more than a weekend ....
Best regards,
Jürgen

Paul Lemmens

unread,
Feb 26, 2015, 2:07:17 AM2/26/15
to shiny-...@googlegroups.com
Hi all,

I am getting quite curious why it seems to take such an effort to enable panning and zooming in Shiny. There are also R packages like rbokeh and dygraphs that support zooming in on a plot out of the box (i.e., without specifying anything in a function call). Is this some kind of limitation in Shiny or is it more that Shiny is so generic that you really need to go to the lower level (like mentioned above) to get this worked out?

Thanks for your input!

Joe Cheng [RStudio]

unread,
Feb 26, 2015, 2:32:36 PM2/26/15
to shiny-...@googlegroups.com
Think of Shiny as a way to wire up user input with R calculations, and route the results into web-based output widgets. Shiny is so agnostic about how you choose to present results that it isn't really meaningful to talk about a generic notion of panning and zooming in Shiny. However, you can use panning/zooming JavaScript libraries with Shiny (there's a packaging issue with bokeh specifically that currently makes that integration difficult, but dygraphs, leaflet, highcharts, and others can be used in Shiny apps using their respective R packages).

Paul Lemmens

unread,
Feb 27, 2015, 8:59:18 AM2/27/15
to shiny-...@googlegroups.com
Hi Joe,

Thanks for your elaboration. As mentioned, I already found out about rbokeh, so a pity that it mixes bad with Shiny. However, I will have a look at the other options. I presume that you listed the most viable/interesting ones by mentioning leaflet and highcharts (dygraphs is not an option because of my data type)?

Thanks!

Joe Cheng

unread,
Feb 27, 2015, 12:57:00 PM2/27/15
to Paul Lemmens, shiny-...@googlegroups.com
It really depends entirely on your data type. You can see some other libraries here, though this is not comprehensive either:


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/50969eb9-4793-44f2-8ffd-3f08ada226c4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jonathan

unread,
Mar 2, 2015, 1:24:30 PM3/2/15
to shiny-...@googlegroups.com, paul.l...@gmail.com
Hi,

So R Shiny definitely has the low level framework, but higher level implementations remain a key barrier.
It seems like just a matter of time, but not quite there yet...

Jonathan



Guy Hotson

unread,
Mar 2, 2015, 6:04:16 PM3/2/15
to shiny-...@googlegroups.com
The svgPanZoom works great for me. Here's an example (I don't think you actually need SVGAnnotation to run it). Though one bug that came up for me is setting the linetype to blank in stat_density2d causes an error.

library(shiny)
library(SVGAnnotation)
library(svgPanZoom)
library(ggplot2)

ui <- shinyUI(bootstrapPage(

  svgPanZoomOutput(outputId = "main_plot")

))

server = shinyServer(function(input, output) {
  output$main_plot <- renderSvgPanZoom({
    p <- ggplot() + geom_point(data=data.frame(faithful),aes(x=eruptions,y=waiting)) + stat_density2d(data=data.frame(faithful),aes(x=eruptions,y=waiting, alpha =..level..),geom="poly
 gon") + scale_alpha_continuous(range=c(0.05,0.2))
    svgPanZoom(p, controlIconsEnabled = T)
  })
})

runApp(list(ui=ui,server=server))

Paul Lemmens

unread,
Mar 4, 2015, 9:39:02 AM3/4/15
to Guy Hotson, shiny-...@googlegroups.com
Hi Guy,

Great idea and suggestion and thanks for the code! What I dislike about the solution is that it zooms and pans the entire SVG. Packages like rbokeh and dygraphs only zoom in on the data leaving the axes visible (while adapting their ranges/tick marks). Could you comment on that?

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/8MIoNvFBOCo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/13eeb48f-37d8-497c-bcf6-66ba743afe12%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
 Use the Force                pa...@paul-lemmens.nl
Read the Source               www.paul-lemmens.nl

Guy Hotson

unread,
Mar 4, 2015, 9:50:15 AM3/4/15
to Paul Lemmens, shiny-...@googlegroups.com
Ah, good point. I hadn't really noticed since I'm using it for density plots on an image without any axes. You could try asking on the github, I got a very quick response when I opened an issue there. 
Reply all
Reply to author
Forward
0 new messages