Thursday, December 19, 2013

Make All Polygons the Same Shade in Leaflet

The Quick Start tutorial shows us how to change the color of a polygon:

var circle = L.circle([51.508, -0.11], 500, {
    color: 'red',
    fillColor: '#f03',
    fillOpacity: 0.5
}).addTo(map);

But what if we want to change the color and style of all (or a set of the) polygons?

First we can define the style:

var defaultStyle = {
  color: 'green',
  fillOpacity: 0.2
};

And then we can just add that style to our polygons:

var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]).setStyle(defaultStyle).addTo(map);

var circle = L.circle([51.508, -0.11], 500).setStyle(defaultStyle).addTo(map);


The full code (based on the Quick Start tutorial) is available in a gist.

Thursday, December 12, 2013

Add Spaces Between Citations in LaTeX

I had an issue where LaTeX wasn't adding spaces between references when I had multiple references in the same place.

So this:

of their own \cite{moretti:2012,saxenian:1996,casper:2007}. 

was compiling as this:

of their own [Moretti, 2012,Saxenian, 1996,Casper, 2007].


To fix the problem, I simply added the space option for the cite package.

\usepackage[space]{cite}

And now it looks as it should:

of their own [Moretti, 2012, Saxenian, 1996, Casper, 2007].

If you have spaces and don't want them, you can instead use the nospace option to remove the space between each citation.

References

Thursday, December 5, 2013

Check if a Variable Exists in R

If you use attach, it is easy to tell if a variable exists. You can simply use exists to check:

>attach(df)
>exists("varName")
[1] TRUE

However, if you don't use attach (and I find you generally don't want to), this simple solution doesn't work.

> detach(df)
> exists("df$varName")
[1] FALSE

Instead of using exists, you can use in or any from the base package to determine if a variable is defined in a data frame:

> "varName" %in% names(df)
[1] TRUE
> any(names(df) == "varName")
[1] TRUE

Or to determine if a variable is defined in a matrix:


> "varName" %in% colnames(df)
[1] TRUE
> any(colnames(df) == "varName")
[1] TRUE


References