I have been trying to figure out how to switch the output format of a plumber API endpoint based on a user argument. Eventually I hit on the solution below, making a low-level custom serializer that switches the serialization function and type header based on an object attribute. Since I didn't find any examples elsewhere, posting it here in case it is useful or in case anyone has suggestions for improvements:
library(plumber)
#* @apiTitle Example Plumber API with Serializer to switch formats using object
#* attributes
serializer_switch <- function() {
function(val, req, res, errorHandler) {
tryCatch({
format <- attr(val, "serialize_format")
if (is.null(format) || format == "json") {
type <- "application/json"
sfn <- jsonlite::toJSON
} else if (format == "csv") {
type <- "text/csv; charset=UTF-8"
sfn <- readr::format_csv
} else if (format == "rds") {
type <- "application/rds"
sfn <- function(x) base::serialize(x, NULL)
}
val <- sfn(val)
res$setHeader("Content-Type", type)
res$body <- val
res$toResponse()
}, error = function(err) {
errorHandler(req, res, err)
})
}
}
register_serializer("switch", serializer_switch)
#* Return a data frame of random values
#* @param n size of data frame
#* @param format one of "json", "csv", or "rds"
#* @serializer switch
#* @get /random_df
function(n = 10, format = "json") {
out <- data.frame(value = rnorm(n))
attr(out, "serialize_format") <- format
out
}