Shiny Geyser App with and without Modules

This repo is a study in how to build Shiny apps with and without modules using readily available data on the Old Faithful geyser. See Shiny Apps for other Shiny apps under collaborative development.

Table of Contents

R Shiny App Code Tutorial

Go into one of the first four folders in inst/build_module and run the app.R file using shiny::runApp(). To run the quarto versions, you will need to install Quarto and some packages; perhaps, see my quarto notes. You can learn something about connecting multiple modules from examples in inst/connect_modules.

Study in Building a Module

Once we have code for an app as ui and server, we can turn that into a module. A module is a Server function and at least one Input, Output, and/or UI functions. In addition, an App function enables us to test module code. Attending to namespace conventions is important.

  1. oldFaithful (original code in one file app.R)
  2. newFaithful (original code in “fake” functions)
  3. callModule (demo callModule vs moduleServer)
  4. moduleServer (full module approach using moduleServer and NS namespace)

Study in Connecting Multiple Modules

We can connect multiple modules to develop more complicated dashboards. Again, care with namespaces is important. This both leads to DRY code and increases readability, clarifying logic and pointing to ways to improve (or remedy broken) code.

Python Modules

I am at an early stage with python shiny modules. There are parts of a package here in the geyser folder. Shiny python is a bit different from Shiny R in a number of ways. See references as well as my notes below. [Note: python files in inst/connect_modules have been moved to inst/connect_modules/python.]

A variety of Python apps can be found in build_module/5_python and connect_modules. Compare the app*.R* and app_*.py code.

While you can generally run shiny run my_app.R or shiny run my_app.py from the system command line, it sometimes acts wierdly for app.py or app.R files.

Issues with Python

Be sure you have the latest Python (say 3.12), and that you have installed shiny and shinywidgets. You will have to do install the geyser Python repo. The following is supposed to work, but I have not been able to try it yet.

python -m pip install pip@git+https://github.com/byandell/geyser

Otherwise you should download the repo to your machine to install by, say

python -m pip install ~/Documents/GitHub/geyser

The Python library code is in folder github.com/byandell/geyser/geyser with setup file github.com/byandell/pyproject.toml in the main package folder. This way, the same repo is used for both R and Python code, and Python import lines refer to geyser.

  • The file app.py has a confusing role. Such a file can be run directly to deploy an app locally. However, Quarto Python apps (see below) reserve this name.
  • Issues arise quickly when trying several apps within a session, especially if done using reticulate from within Rstudio.
  • You can use other names than app.py, such as app_hist.py, but you may have to deploy an app in a different way.
  • Python shiny modules appear to work similarly to R shiny modules. See examples above as well as the more complicated (6-module) app_demo:
  • Python apps from Rstudio:
    • I created a Python function in the io module, r_object() which finds a free port.
    • Some files have commented first lines to use reticulate package to run.

Python and Quarto and Shiny

  • It is possible to combine R and Python with Quarto:
  • Often best to have Quarto and Python shiny apps in different folders.
  • Be aware that the file app.py is reserved by Quarto when deploying Python Quarto apps from any folder.
  • I could not get shiny Python modules to function fully with their server function in Quarto. Further, I could not find any working example. Essentially, all I could do was wrap the app*.py code in a python chunk Thus, I see no way at this time to build complicated Python-based apps via Quarto that take advantage of Quarto’s layout features, in a way that was readily done with R-based apps. [Note: quarto files in inst/connect_modules have been moved to inst/connect_modules/quarto.] See for example

More Python examples

References