rstudio/plumber

Docs (or helper functions?) on consuming Plumber APIs in R, Shiny and RMarkdown/Quarto

Open

#901 opened on Jan 12, 2023

View on GitHub
 (2 comments) (0 reactions) (0 assignees)R (1,437 stars) (257 forks)batch import
difficulty: intermediatedocseffort: lowhelp wanted

Description

I've been trying to explain to people inside my team how we will use Plumber APIs inside our other R code.

So far we've used a fork copy of [https://github.com/bergant/rapiclient] with some auth additions which I think will make R calling plumber APIs pretty straight-forward.

I am, however, struggling a bit with "good practice" when it comes to consuming anything except text and JSON inside e.g. Shiny and RMarkdown.

For example, I've been trying to use a PNG plot output today in a Shiny app. I've coded that into an app which an imageOutput like:

  iv_api <- ApiClient::api_get_plumber_api("demo_charts")

  output$iv_plot <- renderImage({
    req(input$which_security)
    Log("renderImage", input$which_security)
    width  <- session$clientData$output_iv_plot_width
    height <- session$clientData$output_iv_plot_height
    Log("renderImage", input$which_analyst, input$which_security, width, height)
    req(width)
    req(height)
    
    Log("Fetching")
    please_wait <- quickUdsDialog(paste0("Please wait... loading chart for ", input$which_security), 
                                            label="Loading",
                                            overlay_dismiss = FALSE,
                                            escape_dismiss = FALSE,
                                            closable = FALSE, 
                                            open = TRUE)
    on.exit({ hideUdsDialog(please_wait) })
    
    magic_resolution <- 96
    pngChart <- iv_api$Generate_Chart_for_analyst(input$which_security, width = width/magic_resolution, height = height/magic_resolution)
    
    # Use a temp file to save the output (It would have been more efficient for the PNG to never have been read in the first place!)
    outfile <- tempfile(fileext='.png')
    png::writePNG(pngChart, outfile)
    
    # Return a list containing the temporary file
    list(src = outfile,
         width = width,
         height = height,
         alt = paste0("Chart for ", input$which_security, " for ", input$which_analyst)
    )
  }, deleteFile = TRUE)

Note: I know this isn't optimal... still plenty to tweak and experiment with - would like to get the file saving direct into the httr::GET ideally... just fighting the rapiclient wrapper

The feature request here is: I think it'd be useful for the plumber docs to have a section on how to make Shiny apps and markdowns that interally use plumber requests for json data, for png's, for svg's and for htmlwidget's...

I also think it might be useful to add some helper functions to some library somewhere too - e.g. most of the above renderImage could be hidden away I think (especially if #897 provides a standard way of specifying image sizes)

Contributor guide