class: inverse, center, middle #
Shiny in production ![:custom_hr]() ## An overview --- # Deploying a Shiny app ## Making your application available <br> -- * Posit (formerly RStudio) solutions: *
https://posit.co/products/open-source/shinyserver/ *
https://www.shinyapps.io/ *
https://docs.posit.co/shinyapps.io/index.html -- * Self-hosting: -
https://emeraldreverie.org/2019/11/17/self-hosting-shiny-notes-from-edinbr/ -
https://www.shinyproxy.io/#open-source ??? Account via github --- # Best practices ###
https://mastering-shiny.org/scaling-intro.html -- ## Follow general best practices ### (software engineering principles) * Good design of your code (easy to read, non-redundant, etc.) * version control such (e.g.
) * use a good code editor * test your application * etc. --- # Best practices ## Pay attention to your
code * Use functions to avoid redundancy * Use mapping functions [`purrr`](https://CRAN.R-project.org/package=purrr) -- ## Profile and optimize your
code * profile your code * `Rprof()` *
[`profvis`](https://CRAN.R-project.org/package=profvis) ([flame graph](https://queue.acm.org/detail.cfm?id=2927301)) * benchmark *
[`microbenchmark`](https://CRAN.R-project.org/package=microbenchmark) * read about this *
[Improving performance](https://adv-r.hadley.nz/perf-improve.html) (
Advanced R) *
[Efficient R programming](https://csgillespie.github.io/efficientR/) --- # Best practices ### **More complex shiny longer codebase** -- ### Try to create **independent components** * functions in separate `.R` files in `R/` * external package(s) * modules --- # Best practices ### Use [modules](https://mastering-shiny.org/scaling-modules.html) ```R yourModuleUI <- function(id) { ns <- NS(id) tagList(...) } yourModuleServer <- function(id, geoms, preview, u_details) { moduleServer( id, function(input, output, session) { ... } ) } ``` --- # Best practices ## Turn your shiny app into an
package ###
Access to robust tools to develop your app (namely
[`devtools`](https://CRAN.R-project.org/package=devtools)) -- ### **Testing** * Critical part of the development of your app * Unit tests for your functions (see
https://r-pkgs.org/testing-basics.html#introducing-testthat) * Test reactivity with `testServer()`(see
https://mastering-shiny.org/scaling-testing.html) * Test your application visual (using snapshot, see https://testthat.r-lib.org/articles/snapshotting.html) --- # Best practices ### Avoid heavy computations when possible -- ### Load tests * Test how your application with a given load (i.e. a given number of users using your application simultaneously) *
[`shinyloadtest`](https://CRAN.R-project.org/package=shinyloadtest) -- ### Caching * Save the output result of a given action and reuse it instead of redoing the computation * Use `bindCache()` (see also
[`cachem`](https://CRAN.R-project.org/package=cachem)) *
https://shiny.rstudio.com/articles/caching.html -- *
https://www.rstudio.com/resources/rstudioconf-2019/shiny-in-production-principles-practices-and-tools/