user2165379
user2165379

Reputation: 479

how to assign a fixed port to local plumber server

I have set up a local plumber server in R which I like to call locally from Excel VBA. After a restart of the R plumber-code a new portnumber is assigned. This means I have to adapt my Excel VBA code manually every time with a new port number. For that reason I would like to assign a fixed portnumber. I did find this solution:

@options(plumber.port = XYZ)

Although when I integrate this line in my code below I receive the error:

Error in stopOnLine(lineNum, line, "No path specified.") : 
  Error on line #10: '#* @options(plumber.port = 5555)' - No path specified.

code:

#plumber.R
library(plumber)
#* @get /random_numbers
#* @param maxn
function(maxn) {
  maxn<-as.numeric(maxn)
  runif(1,min=0,max=maxn)
}

#* @options(plumber.port = 5555)
#* @post /operation
#* @param numbers vector of numbers
#* @param metric
function(numbers, metric) {
  if(metric == 'mean')
    mean(numbers)
  else if(metric == 'sd')
    sd(numbers)
  else if(metric == 'min')
    min(numbers)
  else if(metric == 'max')
    max(numbers)
  else
    "Wrong metric! use mean, sd, min or max"
}

I have tried plumber.port = 127.0.0.1:5555 although this makes no difference. Is there a way to assign a fixed adres to the plumber server?

Thanks a lot!

Upvotes: 1

Views: 649

Answers (1)

pgcudahy
pgcudahy

Reputation: 1601

The documentation on this is confusing and I had to look for example usage on github to figure it out. Rather than #* @options(plumber.port = 5555) you need to use options("plumber.port" = 5555)

#plumber.R
library(plumber)
options("plumber.port" = 5555)
#* @get /random_numbers
#* @param maxn
function(maxn) {
  maxn<-as.numeric(maxn)
  runif(1,min=0,max=maxn)
}

#* @post /operation
#* @param numbers vector of numbers
#* @param metric
function(numbers, metric) {
  if(metric == 'mean')
    mean(numbers)
  else if(metric == 'sd')
    sd(numbers)
  else if(metric == 'min')
    min(numbers)
  else if(metric == 'max')
    max(numbers)
  else
    "Wrong metric! use mean, sd, min or max"
}

But as @user2554330 pointed out, I usually set the port in the pr_run function instead.

Upvotes: 2

Related Questions