Shiny input selector as plumer parameter

Hi,

I'm writing a plumber API to keep large datasets out of a Shiny app but would like the parameters to accept a vector of values in a filter. I have tried to give a reproducible example below :slightly_smiling_face:

library(plumber)

#* @apiTitle Filter mtcars

#* Return the mtcars filtered
#* @param f1 Filter to select single filter
#* @get /mtcars/filter
function(f1) {
  mtcars[mtcars$cyl %in% f1,]
}

If I want all the rows where cyl == 4 using http://127.0.0.1:8160/mtcars/filter?f1=4 the API correctly returns 11 rows :grinning:

When I want all the data from the API using the filter I have to construct the call as follows http://127.0.0.1:8160/mtcars/filter?f1=4&f1=6&f1=8

I was wondering what is the best way to construct the API call from Shiny using the input$filter_cyl

I would like to generate the following dynamically
For all 3 selected:
api_call = glue::glue("?f1={f1[[1]]}&f1={f1[[2]]}&f1={f1[[3]]}"))
For only 2 values selected:
api_call = glue::glue("?f1={f1[[1]]}&f1={f1[[2]]}"))
For only 1 value selected:
api_call = glue::glue("?f1={f1[[1]]}"))

I can do the if but was wondering if there is a better way.

Thank you

Resolved

On the shiny side I used the paste0() function to create an array and on the plumber side I used strsplit() function to create a vector.

1 Like

@Dawie Thank you for posting your solution!


I would recommend looking into httr. It provides a clean/consistent interface to creating the query string by allowing you to submit a regular named list object. httr will handle any encoding that needs to happen.

If your requests start becoming larger (such as >= 6000 characters), httr can construct a POST request without many changes.

1 Like

Thank you @barret for the guidance.