R语言中的plumber介绍
plumber 是个强大的 R 包,用于将 R 代码转换为 Web API,通过使用 plumber,可轻松地创建 RESTfulI,以便将 R 的数据处理和分析功能暴露给其他应用程序或用户,plumber是一个非常方便的工具,无论是简单的函数调用还是复杂的数据处理,plumber 都能轻松应对
基本用法
示例
library(plumber)
#* @apiTitle 简单 API 示例
#* @get /echo
#* @param msg Query parameter
#* @response 200 返回传递的消息
function(msg = "") {
list(message = paste("你发送的消息是:", msg))
}
其中#* @标记请求参数格式
可将上面的代码保存在一个名为 api.R 的文件中,然后使用以下代码启动 API:
# 启动 API
library(plumber)
r <- plumb("api.R") # 载入 API 定义
r$run(port = 8000) # 启动 API 服务器
然后可通过访问 http://localhost:8000/echo?msg=Hello 来测试 API
常用 API 方法
1. GET 方法
#* @get /hello
function() {
list(message = "Hello, world!")
}