Effective Delivery of Insights

Review

Section Agenda


  • Using Shiny to deliver interactive content
  • Principles of Shiny in production
  • Managing and monitoring production content

Let’s talk Shiny



Whether you’re here
in your Shiny journey:


Photo by Andriyko Podilnyk on Unsplash


Or here:

Photo by Francesco on Unsplash

🧰 Shiny resources

Shiny in production


“But my app runs great on my laptop…”

Two sides to this coin:

  1. Optimize your app
  2. Tune your runtime settings

Shiny in production — Optimize your app


📣 Design principle: Make your app a lightweight presentation layer

Performance workflow:

  • Add logging to app, either basic message statements or use log4r package
  • use shinyloadtest to simulate app performance under load
  • use profvis to profile your app to identify bottlenecks
  • Optimize app (in order of value-add):
    • Move work upstream out of Shiny
    • Make code faster
    • Use caching
    • Use async

🧰 References:

👋 Let’s meet our Shiny App

How does our Shiny App Work?

  • Code for App: materials/project/04_inspector_shiny_app/app.R
# Make prediction (add .pred_FAIL to table)
  prediction_info <- reactive({
    
    prediction_df <- selected_info() |> 
      select(-aka_name) 
    
    fail_prediction <- predict(endpoint, prediction_df)$.pred_FAIL
    
    selected_info() |> 
      mutate(fail_prediction = round(fail_prediction * 100, 3))
    
  })

Activity

Activity

👉 Open the file materials/activities/activity-07_shiny.qmd

Shiny on Connect

Runtime settings

Min processes — Keep a process running on the server at all times?

Max processes — Put a bound on the number of processes spawned?

Connections per processA connection = a browser tab. How many connections will share the same process? Call this n

Load factor — Much like min processes, how early do you want to start a new process for that n + 1 connection?

Posit Connect

The Shiny Restaurant Analogy

Let’s say you are the manager of a restaurant, and you need to determine two things:

  1. How many chefs should I hire?
  2. How many customers can I accommodate at once?
  • Chef == process
  • Customer == connection

Posit Connect

The Shiny Restaurant Analogy

Managing Content

Managing Content

  • How can I organize this content so stakeholders can find it?
  • How will I know if people are consuming my content?

How can I organize this content so stakeholders can find it?

  1. Tags in Connect

  1. 🧰 connectwidgets for content curation

Connectwidgets flow

Incorporate into a stylized R Markdown or Quarto document or Shiny application.

# Establish a connection to the Connect Server
client <- connect(
  server  = Sys.getenv("CONNECT_SERVER"),
  api_key = Sys.getenv("CONNECT_API_KEY")
  )

# Return a dataframe of content information to curate
project_content <- client |> 
  content() |> 
  # filter helpers:
  by_tag() |> 
  by_owner() |> 
  # or use your own dplyr filter
  dplyr::filter()
  
# Use widgets to lay out your content
### card
project_content |> 
  rsc_card()

### grid
project_content |> 
  rsc_grid()

### table
project_content %>%
  rsc_table()

Tip

Content returned by connectwidgets::content will be scoped to the API key provided.

Example

https://colorado.posit.co/rsc/chicago-food-r/

Activity

👉 Open the file materials/activities/activity-08_Connectwidgets.qmd

How will I know if people are consuming my content?


🧰 Posit Connect Server API

Posit Connect Server API endpoints

Most endpoints require an API Key, and results will be scoped to the permissions of the key.

Helpful to publishers:

  • List/Update content
  • Get content details
  • Set environment variables
  • Change permissions
  • Manage content tags
  • Manage content custom URL
  • ✨ Content usage ✨

Helpful to admins:

  • 👈 all that
  • Audit logs
  • User and group management
  • Job management

See full documentation at https://docs.posit.co/connect/api/

Example Content Usage Report

...
con <- connectapi::connect()

app_usage <- get_usage_shiny(
  client,
  from = report_from,
  limit = Inf,
  content_guid = guid
)
...

See more examples: https://solutions.posit.co/operations/connect-apis/

Accessing the Connect Server API

  • Interact remotely with the Connect Server API using HTTP requests directly via:
    • curl - Linux tool for making HTTP calls
    • httr - R HTTP library
    • requests - Python HTTP library
  • Or use connectapi R package, which provides a wrapper around HTTP requests

Connect Server API Resources