问题描述
我想使用以下变量从大约 50,000 个犯罪的数据框制作本地犯罪 API;犯罪类型作为一个因素、犯罪日期以及经纬度.
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.
我自己学会了如何为 api 使用 GET 和 POST 以及如何在 R 对象和 json 之间进行转换,但我对构建 api 知之甚少.如果有人请为我提供一个简单的实现方向,由一个很棒的 Web 服务托管.用于实验的免费托管将是首选,但如果有一种简单的方法可以以微不足道的价格实现这一目标,那就很酷了.
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.
谢谢
推荐答案
对于简单的 API 调用,我认为最好使用 RApache.按照 http://rapache.net/manual.html
For a simple API call, I believe the best is to use RApache.Install RApache as indicated in http://rapache.net/manual.html
在 httpd.conf 中设置 Apache 指令,这将确保/var/www/brew 下的所有文件都被解析为 R 脚本
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>
使用您的 API 制作 R 脚本,例如mycrimeapi.R 并将其放在/var/www/brew 文件夹下.这个 R 脚本文件可以例如如下:
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))
%>
现在有人可以通过调用 http://localhost/brew/mycrimeapi.R?crime=crimewhichisreallynasty&crimedate=2014-01-01
来调用您的 API.将 localhost 替换为您托管 API 的服务器的 IP.
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.
使用 RApache 时,每次您获得 GET、POST、COOKIES、FILES、SERVER
变量时,这些变量都会传递给 API 调用.因此,如果您想在调用中使用 POST 而不是 GET 示例,请继续.有关这些变量,请参阅 http://rapache.net/manual.html 中的文档.
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.
这篇关于部署包含 R 函数的 API 的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!