Programmatically set plumber apiVersion to package version

Hello!

I have my plumber API in the main directory of my R package. Is there a way to fill in the apiVersion with the version in the DESCRIPTION file?

I have not dived into programmatically creating roxygen-like documentation in files before. Theoretically, I want to do something like this:

#* @apiTitle My Plumber API
#* @apiVersion `r desc::desc_get_version()`

Any thoughts on achieving this or if it's possible at all are greatly appreciated! :smile:

I have the same need. That would be a good PR.

In the mean time, here's an ugly hack programmatically.

library(plumber)
pr <- pr()
pr$.__enclos_env__$private$globalSettings$info$version <- "4.0.0"
pr$run()

Less ugly hack by overwriting the spec using annotations

#* @plumber
function(pr) {
  oas <- function(spec) {
    spec$info$version <- packageVersion("plumber") |> as.character()
    spec
  }
  pr$setApiSpec(oas)
}

#* @get /sample
function() {
  "OK"
}
1 Like

Perfect! This gets me what I want :slight_smile: I ended up with the following

#* @plumber
function(pr) {
  oas <- function(spec) {
    spec$info$version <- pkgload::pkg_version() %>% as.character()
    spec$info$title <- "My API"
    spec
  }
  pr$setApiSpec(oas)
}

I use to have the @apiTitle tag. But when I used this method it ended up appending the "@plumber" to the end of it. Easy modification with the code you shared! Thank you so much! :smiley:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.