The value of x is 42
x is now 42
Warning: x is 42 and that might be bad
Error in eval(expr, envir, enclos): x cannot be 42
Level Up with Shiny for R
posit::conf(2024)
2024-08-12
We’re going to start by deploying _exercises/15_deploy/15_01_app.R
.
First, we’ll just get it onto Connect.
Then, we’ll switch to a programmatic deployment using rsconnect::deployApp()
.
I’ll walk through each step in the process. Choose your adventure:
Follow along and work through this with me
Just watch and absorb
I’ll work in the same file, but there are snapshot of the progress we’ll make, e.g. 15_02_app.R
, etc.
The easiest way to deploy your app is with RStudio’s Publish App button, right next to the Run App button.
Follow the instructions to get connected with Posit Connect.
…they’re probably doing something you didn’t expect.
Find the logs of the app we just deployed.
Share your app with a small group first. Someone who will tell you what they think and reach out if the app breaks.
What happens when an app breaks in the middle of a user session?
lgr
INFO [17:16:32.792] Loading data from customers.csv
WARN [17:16:32.824] Missing values detected in column `salary`
ERROR [17:16:32.831] Model validation failed {accuracy: `0.459`}
FATAL [17:16:32.832] object of type 'closure' is not subsettable
INFO [17:16:32.966] Loading data from customers.csv
WARN [17:16:32.997] Missing values detected {columns: (salaries, age)}
ERROR [17:16:32.999] Model validation failed {accuracy: `0.459`, threshold: `0.5`}
FATAL [17:16:33.000] object of type 'closure' is not subsettable
Warning: Error in dispatch: Invalid map parameter
100: expandLimits
98: addMarkers
97: ::
htmlwidgets
shinyRenderWidget [/Users/garrick/teach/level-up-shiny/_examples/12-deploy/02_log_app.R#247]
# ... more stuff
Task: Add logging to the app to diagnose this error.
DEBUG [10:04:39.334] Rendering map with color mode {color_mode: `light`}
DEBUG [10:04:39.357] Choosing OpenStreetMap.Mapnik tiles
DEBUG [10:04:42.767] Rendering map with color mode {color_mode: `dark`}
Warning: Error in dispatch: Invalid map parameter
100: expandLimits
98: addMarkers
97: ::
htmlwidgets
shinyRenderWidget [/Users/garrick/teach/level-up-shiny/_examples/12-deploy/02_log_app.R#247]
Task: Fix and re-deploy the app.
{config}
Except they are…
Sys.getenv(name, default)
to get the value of an envvaredit_r_environ()
to edit the .Renviron
file.env
files
{dotenv}
What’s the value of the R_CONFIG_ACTIVE
envvar?
Service | R_CONFIG_ACTIVE |
---|---|
Posit Connect | rsconnect |
Post Cloud | rstudio_cloud |
shinyapps.io | shinyapps |
Shinylive | shinylive |
You: Hi, how do I get connected to the database?
IT: Sure thing, here’s the connection string.
postgres://dbuser:$3cur3C0d3!@ec2-54-123-45-67.compute-1.amazonaws.com:5432/mydatabase
host:
port:
dbname:
user:
password:
{dotenv}
db.env
secrets/db-staging.env
or secrets/db-prod.env
arrange()
–> window_order()
!!
in the between()
callscollect()
the data in the schools
reactivelibrary(dplyr)
staging_env <- here::here("secrets/db-staging.env")
prod_env <- here::here("secrets/db-prod.env")
dotenv::load_dot_env(staging_env)
con <- DBI::dbConnect(
RPostgres::Postgres(),
host = Sys.getenv("DB_HOST"),
port = Sys.getenv("DB_PORT"),
dbname = Sys.getenv("DB_DATABASE"),
user = Sys.getenv("DB_USER"),
password = Sys.getenv("DB_PASSWORD")
)
tbl(con, "school")
tbl(con, "scorecard")
Is it working now with these packages? Good, lock it down. This is a good time for renv.
Now that it’s working, let’s test it.
Does that sound backwards?
Testing shiny apps isn’t like testing package code.
Your tests are most helpful when they keep you from breaking something that was working without knowing it.
First, record the test.