3

I want to make a local crime API from a data frame of about 50,000 crimes with the following variables; crime type as a factor, the date of the crime, and latitude and longitude.

I want to use my build in my functions for choosing a time range, crime type, and most importantly returning only crimes that occurred within a given radius from a given lat and long.

I have learned on my own how to use GET and POST for an api and converting between R objects and json but I don't know much about building an api. If someone please provide me a little direction on an easy implementation to be hosted by a web service that would be great. Free hosting for experimenting would be preferred but if there is a simple way to make this happen for a trivial price that is cool to.

Thanks

cylondude
  • 1,816
  • 1
  • 22
  • 55

1 Answers1

4

For a simple API call, I believe the best is to use RApache. Install RApache as indicated in http://rapache.net/manual.html

Set the Apache directive in httpd.conf which will make sure all files under /var/www/brew are parsed as R scripts

<Directory /var/www/brew>
    SetHandler r-script
    RHandler brew::brew
</Directory>

Make your R script with your API e.g. mycrimeapi.R and put it under the /var/www/brew folder. This R script file can e.g. look as follows:

<%
require(jsonlite)
load("yourdataset.RData") # this contains your crimes data frame
mycrimes <- subset(crimes, crimetype %in% GET$crime & crimedate %in% as.Date(GET$crimedate))
cat(toJSON(mycrimes))
%>

Now someone can call your API by calling http://localhost/brew/mycrimeapi.R?crime=crimewhichisreallynasty&crimedate=2014-01-01. Replace localhost with the IP of the server where you are hosting the API.

When using RApache, each time you get GET, POST, COOKIES, FILES, SERVER variables which were passed on to the API call. So if you want to use POST in your call instead of the GET example, go ahead. See the documentation in http://rapache.net/manual.html for these variables.

  • This looks like exactly what I'm looking for. I'll have to take a couple days to get my linux box so I will have to come back to this in a few days. – cylondude Dec 31 '13 at 17:18
  • I haven't been able to deploy the api but I am approving this answer because its popularity. I'm having troubles with rapache and when I figure it all out I'll be back. Thanks again. – cylondude Jan 26 '14 at 20:56