如何解决在管道工端点中创建/加载模型
我关注这个 great post 和这个 repo。这里的其余端点如下所示:
# build the model
source("make_model.R")
#* @get /predict_petal_length
get_predict_length <- function(petal_width){
# convert the input to a number
petal_width <- as.numeric(petal_width)
#create the prediction data frame
prediction_data <- data.frame(Petal.Width=petal_width)
# create the prediction
predict(model,prediction_data)
}
和 make_model.R 像这样:
dataset <- iris
# create the model
model <- lm(Petal.Length ~ Petal.Width,data = dataset)
一切都很简单,而且工作正常。只是好奇每个 GET 请求会启动 make_model.R 的执行还是有点像单例设计模式?换句话说,make_model.R 会只执行一次吗?谢谢。
解决方法
要继续我的评论,source"(make_model.R")
将只运行一次。
结果将可用于所有发出的请求。这意味着您可以一次加载数据集或模型,这些数据将与您的请求的环境共享。
默认情况下,当您创建新的 Plumber 路由器时(发生 当您对文件调用 plumb() 时,会隐式地创建一个新环境 专为此路由器创建。正是在这样的环境中 将评估表达式并调用所有端点。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。