Search code examples
rplumber

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?

I have tried the <<- operator, but doesn't work.

here's a really simple example :

(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 :

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

Solution

  • You are return-ing before the assignment, i.e. it does not take effect. Try

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