30 plumber
30.1 新建plumber api项目
从Rstudio server的File菜单进入新建
30.2 api.R文件
#
# This is a Plumber API. In RStudio 1.2 or newer you can run the API by
# clicking the 'Run API' button above.
#
# In RStudio 1.1 or older, see the Plumber documentation for details
# on running the API.
#
# Find out more about building APIs with Plumber here:
#
# https://www.rplumber.io/
#
library(plumber)
#* @apiTitle Plumber Example API
#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg=""){
list(msg = paste0("The message is: '", msg, "'"))
}
#* Plot a histogram
#* @serializer png
#* @get /plot
function(){
<- rnorm(100)
rand 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)
}
30.3 run_api.R
::plumb(file='api.R')$run(host = "0.0.0.0", port = 8052) plumber
30.4 从Terminal启动
也可以用
screen -S rapi
的形式保持在后台运行
Rscript run_api.R