简体   繁体   English

R - 水管工,如何将值传递给全局环境?

[英]R - plumber, how to pass a value to the global environment?

Can anyone tell me if there is a way to pass a value, say the session pid, to the global environment with a plumber API?谁能告诉我是否有办法通过管道工 API 将值(例如会话 pid)传递到全局环境?

I have tried the <<- operator, but doesn't work.我试过<<-运算符,但不起作用。

here's a really simple example :这是一个非常简单的例子:

(my_file.R) (my_file.R)

#* @param x My argument
#* @get /lag_lead 
function(x){
 return(x*2)
 api_pid <<- Sys.getpid()
}

and the script to run the api :以及运行 api 的脚本:

library(plumber)
mon_api <- plumb('my_file.R')
mon_api$run(port = 8000)

You are return -ing before the assignment, ie it does not take effect.您在分配之前return -ing,即它不会生效。 Try尝试

#* @param x My argument
#* @get /lag_lead 
function(x){
  api_pid <<- Sys.getpid()
  return(x*2)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM