2011-04-26 277 views
13

作爲探索如何在R for Denver RUG中創建一個包的一種方法,我決定在datasciencetoolkit API上編寫一個R包裝器將是一個有趣的小項目。基本的R工具來自RCurl包,正如你可能想象的那樣。我被困在一個看似簡單的問題上,我希望這個論壇上的某個人能夠指引我朝着正確的方向發展。基本的問題是,我似乎無法使用postForm()傳遞一個未鍵控的字符串作爲curl中數據選項的一部分,即curl -d「string」「address_to_api」。使用RCurl的POST請求

例如,在命令行中我可能會做

$ curl -d "Tim O'Reilly, Archbishop Huxley" "http://www.datasciencetoolkit.org/text2people" 

成功。但是,postForm()在將其他參數傳遞到POST請求時需要顯式鍵。我已經通過datasciencetoolkit代碼和開發人員文檔查找了可能的密鑰,但似乎無法找到任何內容。

順便說一句,通過GET請求將輸入傳遞給DSTK API的其他部分非常簡單。例如,

ip2coordinates <- function(ip) { 
    api <- "http://www.datasciencetoolkit.org/ip2coordinates/" 
    result <- getURL(paste(api, URLencode(ip), sep="")) 
    names(result) <- "ip" 
    return(result) 
} 
ip2coordinates('67.169.73.113') 

會產生期望的結果。

爲了清楚起見,我已閱讀了DTL的omegahat網站上的RCurl文檔,包中的RCurl文檔以及捲曲手冊頁。然而,我錯過了關於curl(或postForm()函數中的.opts())的基本概念,我似乎無法得到它。

在python中,我基本上可以使用httplib.HTTPConnection創建'原始'POST請求 - 類似於R中可用的那樣?我也查看了httpRequest包中的simplePostToHost函數,它似乎鎖定了我的R會話(它似乎也需要一個密鑰)。

FWIW,我在Mac 10.6.7上使用R 2.13.0。

任何幫助,非常感謝。如果您有興趣使用數據科學工具包,所有代碼即將在github上提供。

乾杯。

回答

15

隨着HTTR,這僅僅是:

library(httr) 
r <- POST("http://www.datasciencetoolkit.org/text2people", 
    body = "Tim O'Reilly, Archbishop Huxley") 
stop_for_status(r) 
content(r, "parsed", "application/json") 
6

通常,在您嘗試發佈未鍵控的某些內容的情況下,您可以爲該值分配一個虛擬鍵。例如:

> postForm("http://www.datasciencetoolkit.org/text2people", a="Archbishop Huxley") 
[1] "[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":44,\"end_index\":61,\"matched_string\":\"Archbishop Huxley\"},{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":88,\"end_index\":105,\"matched_string\":\"Archbishop Huxley\"}]" 
attr(,"Content-Type") 
       charset 
"text/html"  "utf-8" 

將工作一樣,如果我想用B =「大主教赫胥黎」等

享受RCurl - 它可能是我最喜歡的[R包。如果你喜歡冒險,升級到〜libcurl 7.21會通過curl(包括SMTP等)公開一些新的方法。

+0

感謝您的幫助!有沒有任何理由,關鍵是'一個'?我嘗試了'名稱','文本'和一堆其他垃圾。 – rtelmore 2011-04-27 02:24:22

+0

更正:我嘗試在不同的調用中使用「名稱」等。我嘗試過使用,例如postForm(api,string),你需要postForm(api,a = string)。 – rtelmore 2011-04-27 04:43:56

+0

對,你需要提供一個key = value對。 'a'完全是任意的(這只是想到的第一個字母)。其他任何工作都一樣(例如a =「string」,name =「string」等。「a」=「string」將不起作用。) – Noah 2011-04-27 09:19:19

1

我只想指出,通過postForm函數傳遞一個原始字符串時一定會有問題。例如,如果我用捲曲在命令行中,我得到如下:

$ curl -d "Archbishop Huxley" "http://www.datasciencetoolkit.org/text2people 
[{"gender":"u","first_name":"","title":"archbishop","surnames":"Huxley","start_index":0,"end_index":17,"matched_string":"Archbishop Huxley"}] 

和RI獲得

> api <- "http://www.datasciencetoolkit.org/text2people" 
> postForm(api, a="Archbishop Huxley") 
[1] "[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":44,\"end_index\":61,\"matched_string\":\"Archbishop Huxley\"},{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":88,\"end_index\":105,\"matched_string\":\"Archbishop Huxley\"}]" 
attr(,"Content-Type") 
       charset 
"text/html"  "utf-8" 

注意,它返回的JSON字符串兩個元素,並沒有一個匹配上start_index或end_index。這是編碼或其他問題嗎?

+0

我猜這實際上是API端的東西 - 這是你期望看到的類型,如果他們奇怪地處理URL編碼的東西。你可以嘗試在你的論點上使用URLencode(),但這可能實際上沒有幫助。 – Noah 2011-04-29 21:10:42

1

的simplePostToHost功能放在HttpRequest包可能你在找什麼在這裏。

2

從鄧肯寺郎的R-幫助列表上:

postForm()是使用比提交卷曲-d命令形式的不同的式(或特異性的Content-Type)。 切換style ='POST'使用相同的類型,但很快猜測,參數名稱'a'導致混淆 ,結果是空的JSON數組 - 「[]」。

一個快速的解決方法是使用curlPerform(),而不是直接postForm()

r = dynCurlReader() 
curlPerform(postfields = 'Archbishop Huxley', url = 'http://www.datasciencetoolkit.org/text2people', verbose = TRUE, 
      post = 1L, writefunction = r$update) 
r$value() 

這就產生

[1] 
"[{\"gender\":\"u\",\"first_name\":\"\",\"title\":\"archbishop\",\"surnames\":\"Huxley\",\"start_index\":0,\"end_index\":17,\"matched_string\":\"Archbishop 
Huxley\"}]" 

,您可以使用fromJSON()將其改造成R.數據