Search code examples
rplumber

How to enter character string vector in R plumber API


I'm building some API endpoints using R's Plumber. One thing I'm trying to learn is how to enter a strings as a vector.

Below is a simplified example of the problem. In this case, what I'd like is something like the following if this were pure R.

cols <- c('V1', 'V3')
tmp <- dat[, cols]
colMeans(tmp)

So, I built the following API, but when testing using Swagger when I enter something like V1, V2 into the cols argument it breaks and returns an error

library(plumber)
library(dplyr)
### Contents of plumber.r file
#* Do means
#* @param cols The columns I want
#* @get /mean
function(cols) {
    dat <- data.frame(V1 = rnorm(10), V2 = rnorm(10), V3 = rnorm(10))
    tmp <- dat[, cols]
    colMeans(tmp)
}
pr("c:/path2file/plumber.R") %>%  pr_run(port=8000)

Can anyone suggest how a get request might pass something like "V1, V3" or something like that to the cols argument in this example that would index the columns of the dataframe at the /mean endpoint like the pure R example?


Solution

  • SHORTER ANSWER

    In your local machine reprex, http://127.0.0.1:8000/mean?cols=V1&cols=V2 would work.

    SHORT ANSWER

    After http://host:port/, you can enter your mean function. To pass an array, enter ?param=value1&param=value2&param=value3...