March 23, 2014

Tips for rMaps and rCharts

Example

Recently Ramnath Vaidyanathan's great packages, rCharts and rMaps, are getting more popular. I'm also a fun and use them heavily. Through the experience of both packages, I have found a few tips to adjust parameters of them. Today I will leave the memo about them.

Example

Here I take the ichoropleth example at the rMaps site.

library(rMaps)
i1 <- ichoropleth(Crime ~ State, data = subset(violent_crime, Year == 2010))
i1$show("iframesrc", cdn = TRUE)

1. States name needs to be in abbreviated form

State names needs to be in abbreviated forms. If you don't use, you will get the result as follows.

violent_crime$State2 <- state.name[match(as.character(violent_crime$State), state.abb)]
i2 <- ichoropleth(Crime ~ State2, data = subset(violent_crime, Year == 2010))
i2$show("iframesrc", cdn = TRUE)

2. Parameters need to be passed in the form of list or “!# #!”

At first, to pass nested parameters like a following case, you have to use list.

## This is a part of Javascript

"geographyConfig": {
          "popupTemplate": function(geo, data) {
              return "<div class=\"hoverinfo\"><strong>"+
                      data.iso3c+
                      '<br>' + data.value+
                      '</strong></div>';
          }
      },

In this case, geographyConfig and popupTemplate are nested next to each other, so you need to use list. Moreover, parameters are usually passed as double-quoted strings. If you don't want to pass the ones as no-quoted, literal, you wrap them by “!# #!”. These points can be applied not only rMaps but also rCharts.

Here's the result. In the following, I modified the tooltip (Compare the first example).

i3 <- ichoropleth(Crime ~ State, data = subset(violent_crime, Year == 2010),
                  geographyConfig=list(popupTemplate="#!function(geo, data) {
                                       return '<div class=\"hoverinfo\"><strong>'+
                                       data.State+
                                       '<br>' + data.Crime+
                                       '</strong></div>';}!#"))
i3$show("iframesrc", cdn = TRUE)                                       

Only the main tips I described here. You can view the other tips at the issues of the package repositories. If I find a new tip, I will add it here. Please let me know your tips.

No comments:

Post a Comment