Deploying Plumber APIs with RStudio Connect
Overview
As a developer, you’ve likely worked with various web frameworks to build RESTful APIs. In recent years, Plumber has emerged as a popular choice for building APIs in R, thanks to its simplicity and ease of use. However, when it comes to deploying these APIs on platforms like ShinyApps.io, things can get more complicated. In this article, we’ll delve into the world of Plumber and RStudio Connect API deployment, exploring the reasons behind the compatibility issues and providing solutions for a seamless experience.
Background
Plumber is an R package designed to make it easy to build web APIs using the R programming language. It utilizes the plumber library under the hood, which provides a simple way to create RESTful APIs in R. The plumber package offers several benefits, including support for HTTP requests, routing, and error handling.
RStudio Connect is an integrated development environment (IDE) that allows developers to build, deploy, and manage web applications built with R. It provides a seamless experience for deploying R code on platforms like ShinyApps.io, making it an attractive choice for data scientists and analysts.
The Problem
When we deployed our Plumber API using the rsconnect::deployAPI() function, we encountered an unexpected error message:
An error has occurred unable to launch worker: API content cannot be
run on this installation of RStudio Connect. Contact your
administrator.
This error message suggests that there’s a compatibility issue between our Plumber API and ShinyApps.io.
Understanding the Issue
The problem lies in the fact that Plumber APIs are not natively supported by ShinyApps.io. This is due to security concerns, as some of the features provided by Plumber can pose a risk to the platform.
According to the official documentation for RStudio Connect and ShinyApps.io, “Plumber apps require additional configuration and setup beyond what’s available in our standard app templates.”
Solution 1: Configure your Plumber API
To resolve this issue, you’ll need to configure your Plumber API to comply with the security requirements set by ShinyApps.io. This involves setting up authentication and authorization mechanisms for your API.
Here’s an example of how you can do this:
# Create a new file named "plumber.R"
library(plumber)
# Define a simple function
ll.toClass <- function(x) {
# Process the input data
x <- as.character(x)
# Return the result
list(result = x)
}
# Define the API endpoints
get("/ll.toClass") <- function(req, res) {
# Get the request body
req_body <- req Body
# Call the function and return the result
output <- ll.toClass(req_body$ll.toClass)
# Return the response
res$status <- 200
res(body = jsonlite::toJSON(output, simplifyDataFrame = FALSE))
}
Solution 2: Use a third-party package
Alternatively, you can use a third-party package like shinyhttp to create a Shiny app that wraps your Plumber API. This approach allows you to maintain the functionality of your API while ensuring compliance with the security requirements set by ShinyApps.io.
Here’s an example of how you can do this:
# Create a new file named "plumber.R"
library(plumber)
# Define a simple function
ll.toClass <- function(x) {
# Process the input data
x <- as.character(x)
# Return the result
list(result = x)
}
# Define the API endpoints
get("/ll.toClass") <- function(req, res) {
# Get the request body
req_body <- req Body
# Call the function and return the result
output <- ll.toClass(req_body$ll.toClass)
# Return the response
res$status <- 200
res(body = jsonlite::toJSON(output, simplifyDataFrame = FALSE))
}
# Create a new file named "shinyApp.R"
library(shiny)
# Define the UI components
ui <- fluidPage(
# Add your Plumber API endpoints here
actionButton("apiCall", "Make API Call")
)
# Define the server function
server <- function(input, output) {
# Create a connection to the Plumber API
conn <- plumb::plumb("./plumber.R")
# Define an observer for the button click event
observeEvent(input$apiCall, {
# Make an HTTP request to the Plumber API endpoint
response <- conn$call("get", "/ll.toClass", body = "testString")
# Update the UI with the response data
output$data <- renderPrint({
jsonlite::fromJSON(response, simplifyDataFrame = FALSE)
})
})
# Return the server function
list(ui, server)
}
# Define the shiny app
shinyApp(ui, server)
Conclusion
Deploying Plumber APIs with RStudio Connect requires careful configuration and setup. By understanding the compatibility issues and using one of the provided solutions, you can ensure a seamless experience for your users.
As we continue to explore the world of R and web development, it’s essential to stay up-to-date with the latest developments and best practices in our field. With this article, we hope to have provided you with the knowledge and tools necessary to overcome common challenges when working with Plumber APIs and RStudio Connect.
Last modified on 2023-09-19