Things I Forget: Reading a Shapefile in R with readOGR

One of the more common ways that I read vector data into R is via shapefiles.  I tend to use these partly becuase of my own sordid past with Arc/INFO, ArcView and ArcGIS and partly due to their ubiquity.  In any event I have found the R package, rgdal, indespensible for this.  One of the workhorse functions for pulling in vector data is readOGR().  It has two required parameters dsn and layer. The part I never remember is how these relate to shapefiles. There is nothing especially tricky about it, I just tend to forget what the dsn is and what the layer is. In short the dsn is the directory (without a trailing backslash) and the layer is the shapefile name without the .shp.

So, here’s the actual code so I don’t have to look it up again.

If the shapefile you are reading is in your current working directory the dsn refers simply to that directory. So all you need is simply a “.”. The layer is the name of shapefile without an extension. So it would look something like:


Now, if that file resides elsewhere, the trick is to remember what the heck dsn refers to. Again it is simply the directory where the shapefile resides. So if I had a shapefile in a place like C:/data I would use the command like:


There, it is really quite simple, yet I always mess it up. Not any more.

I hope.


Open Access and Landscape Ecology

I’ve got a dilemma.  I recently received a request to review a manuscript for the journal, Landscape Ecology.  It is the primary journal for the field.  If I am to be a good landscape ecology citizen I really should do the review.  The problem is that Landscape Ecology is not an Open Access journal.  If I am to be a good science citizen then, I believe, I should do all I can to support Open Access.  So, I am left with the decision to support the field and society I have been active in for years or support Open Access, which I support fully yet am only a recent convert. As I see it I have four options:

  • Review and be quiet
  • Don’t review and be quiet
  • Don’t review, but provide an explanation to the editor.    This is much like what Scott Chamberlain, Casey Bergman, Michael Ashburner and others have done.
  • Review, but provide an explanation to the editor and try to start a discussion about migrating Landscape Ecology to Open Access.

I have decided on the last option.  First, I respect the decision others have made to say no to the review.  In some case, I think that would be the best path.  However, I have been active with US-IALE for several years and would feel more hypocritical just saying no.   Thus, for me, the decision I feel best with is to try and support the journal while also pushing for change.  In short,  I am hoping that I will be able to serve both sides.

By doing this,  I can feel as if I am doing my landscape ecological duty and provide a good and constructive review (assuming of course that I am capable of a review that is both good and constructive).  But at the same time I can register my discomfort with the current publishing model to which my field’s flagship journal adheres.  My plan is to agree to the review, but also include some language in my acceptance about my hesitation due to the closed nature of our journal.   Additionally, I will share my thoughts with the papers authors and suggest that, if feasible, they explore publishing the article under the open access license.   Lastly, I plan to start a discussion within our national chapter of the society.  At a minimum I can at least raise the issue.  If I am a bit more successful , then I can hopefully encourage others to act similarly.  At best, I can start a conversation with our journals editorial board to plan how and when Landscape Ecology could go completely Open Access. I do wonder what others think about this plan to encourage change and also what the consensus is on Landscape Ecology switching to an open access model.

Things I Forget: Installing new python packages on Windows

I can never remember how to do simple things, be it in Python, R, or Java (hope I don’t have to do too much more work in Java…).   Given that, I plan to post these kind of things, when I remember of course, under the title of “Things I Forget”.  My primary aim for this is to help myself, but I do hope others with a similar memory affliction will also find these useful.

For this first installment: How do you add new python packages from a tarball (i.e. .tgz or .tar.gz file) on Windows?

The Steps:

  1. Hope there is a windows installer.  Given how unlikely this appears to be, move to step 2
  2. Download the tarball.  This is the thing you always see when you are expecting to find the windows installer.  It has some name followed by a .tar.gz or a .tgz.  For instance, if you want to add the ability to write out Excel files (by the way,  I am not advocating for Excel), then you could use the xlwt package.   I usually download these files directly into my into my site-packages directory (C:/Your Python Path/Lib/site-packages).
  3. Once you download the taball you need to find someway to extract the file.  On linux machines this is easy.  I had a bit more of challenge getting it to work on a windows machine.  There are a few options.  I have used PowerArchiver before, but it costs.  Cygwin can do it, but that may be overkill.  The one I have used most recently is the very simple TarTool.  It is free and doesn’t need to be installed, only extracted.  I also keep the TarTool.exe in my site-packages directory.  So, now that I have my tool to extract the file and the file itself, I simply do the following (click for full size):
  4. Now you will have a directory for the package, so to install it is simply a matter of changing into that directory and running the setup.  Just like this (click for full size):
  5. Your python package is now ready to roll and can be imported (click for full size)!

As an aside, anyone know of a wordpress trick to display a DOS command prompt?  Inserting a screen shot was the best I came up with, but it would be nice to have something akin to the method for highlighting sourcecode.

Whither the landscape ecology blogger?

I started this blog for myself, mostly.  I wanted to get better at writing, to keep track of some R tricks, talk about Open Science, and learn about others doing the same.  It’s this last thing that is turning out to be a bit of a challenge.  Not so much for finding other R bloggers or Open Science Bloggers or GIS bloggers or ecology bloggers.  They are ubiquitous.  It’s the landscape ecology bloggers that seems to be mostly missing.

Just for illustration, I turn, of course, to Google. The first set of terms looks at various sub-disciplines of ecology. The two with the least number of hits are indeed those that would be most closely associated with the field of Landscape Ecology.

Search Terms Number of Hits 
“Spatial Ecology” Blog  79,600
“Landscape Ecology” Blog  118,000
“Marine Ecology” Blog  133,000
“Ecosystem Ecology” Blog  142,000
“Evolutionary Ecology” Blog  415,000

This last set of search terms are just for comparisons sake.  I know it is apples and oranges, but hey it makes my point, kind of.

Search Terms Number of Hits 
“Open Science” Blog  327,000
“Geographic Information System” Blog  7,300,000
Ecology Blog 26,000,000
R Statistics Blog  159,000,000

Looking a bit closer at the actual results for both “”Spatial Ecology” Blog” and “”Landscape Ecology” Blog” reveals that many of these listed blogs are out of date (i.e., no posts in the last 3 months), are primarily lab websites, or are landscape ecology class websites.    Lastly, there is almost no representation, that I could find, from landscape ecologists active in the International Association of Landscape Ecology or the US Chapter (US-IALE, of which I am an active member).  The very few exceptions to this, that I am aware of, I have listed in my blogroll.

Is the field of landscape ecology getting left behind in the Web 2.0 world of science?  Probably not yet.    At this point,  I think it represents an opportunity.  I hope this blog will partly fill the gap.   But given I am merely on post number 2, it is hard to say.  Heck, I don’t know if I can keep up the blogging nor if I have anything interesting enough to say that others might want to read.

Regardless of whether or not I do anything of consequence with Landscape Ecology 2.0, I believe this gap will be filled.  It will be filled by all the fantastic landscape ecology students with whom I have interacted with via US-IALE.  If any of you are actually reading this, take note.  There is a niche to be filled and, in my opinion, engaging in online discussion of science, and specifically landscape ecology, can be a great thing for your career.  Start a blog about your research, about your graduate career, about anything related to landscape ecology.  Then, get it picked up by an aggregator.  The one I found out about recently is EcoBloggers.  For more info on that head over to Jabberwocky Ecology for details.

Also read more about the general topic of research blogging in ecology.  There are growing number of ecology bloggers who make this case much better than I.

In particular, check out these following posts for some of the more cogent arguments:

And lastly, if I have missed the boat completely and my read on the, uh, blogging landscape (sorry) is wrong, let me know.  Who are the landscpe ecology bloggers you follow?  Give me more stuff to read, already!

First post, and its a doozy!

Well, not really a doozy.  Just something nice and slow to get me going.

So, seeing as I intend to post stuff about R along with the other things, I thought it best to understand how all those great R bloggers embed the highlighted R code into their WordPress blogs.  As it turns out, I am not the first to do so.  Head over to the R Statistics Blog for the details.

So, does it work?


helloWorld <- function(x)
helloWorld("Hello, World!")