Deploying Shiny Apps with curl and REST API

Deploy your Shiny app with curl and REST API.

If you don't have a Shiny app, create it. You can type this example code in the R Studio console:

library(shiny)
ui <- fluidPage(
  "Hello, world!"
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

Alternatively, create a sample app directly from the GUI. Refer to Creating an automatically generated app from RStudio GUI.

Transferring your Shiny app to the deployment space

Exporting a project and importing it into a deployment space is the standard way to move assets (for example, Shiny apps) to a deployment space. For more information, see Importing spaces and projects.

If you want to use curl and REST API to transfer your Shiny app to the deployment space, use this code:

# Set up:
export PS1="\W \$ "
function cpdcurl { X=$1; Path=$2; shift 2; curl -k -H "Authorization: Bearer $USER_ACCESS_TOKEN" -X $X $RUNTIME_ENV_APSX_URL$Path "$@" ; }

export SPACE_ID=...pasteYourSpaceGUID...
# See tab "Manage" > "General" in the deployment space

(cd MyAppName; zip -FSr /tmp/MyAppName.zip .)

cpdcurl PUT "/v2/asset_files/shiny_asset/MyAppName.zip?space_id=$SPACE_ID"  \
   -F file=@/tmp/MyAppName.zip

swspec_id=$(cpdcurl GET "/v2/software_specifications?name=rstudio_r4.2" | jq ".resources[0].metadata.asset_id")
cpdcurl POST "/v2/assets?space_id=$SPACE_ID" \
   -H 'Content-Type: application/json' \
   -d '{"metadata": {"name": "MyAppAsset", "asset_type": "shiny_asset", "origin_country": "de"}, "entity": {"shiny_asset":{"software_spec":{"base_id":'$swspec_id'}}}, "attachments": [{"asset_type": "shiny_asset", "mime": "application/zip", "object_key": "shiny_asset/MyAppName.zip"}]}'

To select the default software specification, use "entity": {} in the payload data. The results include the id of the new asset at metadata.asset_id.`

For more information, see Predefined software specifications.

Deploying Shiny Apps with curl and REST API

This example code uses curl and the CP4D RESTful API for assets. You can run the steps as curl commands by copying and pasting them in a Terminal or run all the steps as an R script.

To deploy your Shiny app with curl and REST API use this code:

export SPACE_ID=...pasteYourSpaceGUID...
export ASSET_ID=...pasteYourAssetID... # from step 2.

cat > tmp_deploy_shiny.json <<ENDJSON
{
   "name": "my app deployment name",
   "space_id": "$SPACE_ID",
   "asset": { "id": "$ASSET_ID" },
   "r_shiny": {
      "authentication": "members_of_deployment_space",
      "parameters":{ "serving_name":"myapp_serving_name123"}
   },
   "hardware_spec": { "name": "XXS" }
}
ENDJSON

cpdcurl POST "/ml/v4/deployments?version=2023-04-01" -d @tmp_deploy_shiny.json -H "content-type: application/json"

# the result has
# "state": "initializing"

# wait a minute and check the status of the deployment
cpdcurl GET "/ml/v4/deployments?space_id=$SPACE_ID&version=2023-04-01&asset_id=$ASSET_ID"

# wait until
# "state": "ready"

The result contains the URL of the deployed Shiny app. The URLs refer to the internal nginx route, for example https://internal-nginx-svc.cpd-instance.svc:12443/ml/v4/deployments/myapp_serving_name123/r_shiny. When you start the deployment in the GUI, the external URLs are shown.

For more information, see API documentation.

Parent topic: Deploying Shiny apps in Watson Machine Learning