Showing posts with label RTips. Show all posts
Showing posts with label RTips. Show all posts

March 31, 2014

Why don't you use %>% ?

One of features of dplyr package, which is well known as very useful one for manipulation, is the operator %.%. It is called chain operator and chains any operation in R as follows.

library(dplyr)
iris %.% group_by(Species) %.% summarise(avg = mean(Sepal.Width))
## Source: local data frame [3 x 2]
## 
##      Species   avg
## 1     setosa 3.428
## 2 versicolor 2.770
## 3  virginica 2.974

%.% works like pipe operator in UNIX. %.% is simple but powerful. In case you have to make temporary objects, you don't need.

If you like F# style, you can use %>% operator with magrittr package. In the next version of dplyr, it is announced that %.% will be deprecated and replaced with %>%. Please check out below in detail.

https://groups.google.com/forum/#!msg/manipulatr/4EtIPVR3qEw/Xx4Vec7O0CQJ

library(magrittr)
iris %>% group_by(Species) %>% summarise(avg = mean(Sepal.Width))
## Source: local data frame [3 x 2]
## 
##      Species   avg
## 1     setosa 3.428
## 2 versicolor 2.770
## 3  virginica 2.974

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.