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.
- oldFaithful (original code in one file
app.R) - newFaithful (original code in “fake” functions)
- callModule (demo
callModulevsmoduleServer) - moduleServer (full module approach using
moduleServerandNSnamespace)
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.]
- slidesPythonModules
- Simple shiny app: compare R to Python
- Shiny module app
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.pyhas 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
reticulatefrom withinRstudio. - You can use other names than
app.py, such asapp_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
iomodule, r_object() which finds a free port. - Some files have commented first lines to use reticulate package to run.
- I created a Python function in the
Python and Quarto and Shiny
- It is possible to combine R and Python with Quarto:
- Often best to have
Quartoand Python shiny apps in different folders. - Be aware that the file
app.pyis 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*.pycode in apythonchunk 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 ininst/connect_moduleshave been moved to inst/connect_modules/quarto.] See for example
More Python examples
- inst/build_module/5_python/app.py functioning python app without
histmodule (open and run app) - inst/build_module/5_python/appHist.py functioning python
histapp with module - inst/build_module/5_python/appGghist.py module with grammar of graphics using
gghistmodule - inst/build_module/5_python/app_gghist.py functioning python app with grammar of graphics code
- python/penguins_restart.qmd working Quarto example with python
- python/hist.py
histogrammodule - python/gghist.py
geom_histmodule - python/io.py io kludges to retrieve R data
retrieveR()and find open port and display on web browerapp_run()
References
- Shiny R
- Shiny Python
- Python Shiny Modules
- Comparison of Shiny for Python and R
- Shiny for Python 0.6.0
- Shiny for Python Cheatsheet
- Intro to Shiny for Python by Ryan Johnson, Posit (Video)
- R Interface to Python: reticulate
- Python Interface to R: rpy2
- Deploying a Shiny for Python application with Posit Connect
- Carpentries: Interactive Data Visualizations in Python (Streamlit)
- Carpentries: Interactive Applications in Python (Shiny)