ramnathv / htmlwidgets

HTML Widgets for R
http://htmlwidgets.org
Other
790 stars 207 forks source link

get the result of a "click" back to the server #75

Closed smartinsightsfromdata closed 9 years ago

smartinsightsfromdata commented 9 years ago

I'm working to a wrapper for the great library datamaps (already available with rCharts but with issues with shiny, so I thought of a brand new implementation).

At present everything seems to work. Only one important functionality is missing.

I would like to get back to the server the name of the state (or postcode, county etc.) that the user has clicked. This callback is already available with datamaps:

done : function(datamap) {
  datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
    alert(geography.properties.name);
  });
  },

And my code duly displays (useless!) popups with the name of the states you click.

How to get geography.properties.name back to the R server??

(apologies but I am NO javascript programmer!)

jcheng5 commented 9 years ago

Replace alert(geography.properties.name); with

Shiny.onInputChange(id + "_click", {
  name: geography.properties.name,
  '.nonce': Math.random()
});

Then if the map ID is "foo" you could use input$foo$name to detect the property name (be sure to verify that input$foo isn't NULL first).

smartinsightsfromdata commented 9 years ago

@jcheng5 Thanks for the input (on issue #76 and #75).

76: Sadly I haven't managed (yet!) to find a clear way to update the maps (without destroying them), so for the time being I'll go ahead with the "crude" approach you have kindly suggested.

75 I have used your code to get in shiny the click "content" but it does not seem to work. Please feel free to install the library and try the relative shiny example here. Input$map is always NULL.

I am very keen for this to work because it would allow very cool dashboards, where you click on a specific state (or county, or postcode) on the map and you get a table of data associated to the specific element.

Your help (and anybody's help of course) to resolve #75 would be very much appreciated!

timelyportfolio commented 9 years ago

I know @ramnathv was well down the path on a datamaps widget. Perhaps he has some code that can handle all of this and more.

smartinsightsfromdata commented 9 years ago

Very exiting: it works! This is the function I've used:

Shiny.onInputChange(el.id, {
  name: geography.properties.name,
  '.nonce': Math.random()
});

I've also found this article quite useful.

Thank you so much for the help. I'll update the library on github with the change.

ramnathv commented 9 years ago

Yeah @timelyportfolio, @smartinsightsfromdata. I will be releasing a new version of rMaps shortly. It is built on top of htmlwidgets and will have datamaps among other mapping libraries.