Plumber

Plumber是一個R包,它使用少量特殊的單行註釋將您現有的R代碼轉換爲Web API。

什麼是Web API?對於某些人來說,API(應用程序編程接口)是聽說過但很少見的東西。但是,無論可見與否,API都是日常數字生活的一部分。實際上,即使您當時不認識它,您也可能已經在R中使用了Web API!幾個R包只是流行的Web API(例如tidycensus和)的包裝gh。Web API是用於在網絡上共享信息的框架,最常見的是通過HTTP。

我們來看一個例子。

# plumber.R

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg=""){
  list(msg = paste0("The message is: '", msg, "'"))
}

#* Plot a histogram
#* @png
#* @get /plot
function(){
  rand <- rnorm(100)
  hist(rand)
}

#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b){
  as.numeric(a) + as.numeric(b)
}

創建好這一份代碼之後,在R語言的終端輸入:

library(plumber)
r <- plumb("plumber.R")  # Where 'plumber.R' is the location of the file shown above
r$run(port=8000)

可以使用瀏覽器或終端訪問此URL,以運行R函數並獲取結果。例如,http:// localhost:8000 / plot將顯示一個直方圖,而http:// localhost:8000 / echo?msg = hello將回顯提供的“ hello”消息。

可能會出現的bug

Error: 'is_present' is not an exported object from 'namespace:lifecycle'

解決方案未知

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章