Shiny Express: Blending the Best of Shiny and Streamlit for Dashboard Development

[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Shiny has long stood as a pillar of functionality and flexibility for R-based web applications. Let’s explore Shiny Express — an emerging Python-centric framework in development that combines the ease of Streamlit with the robustness of Shiny.

This article explores the nuances of Shiny Express and what differentiates it from Classic Shiny and Streamlit.

Table of Contents

Ready to Enhance Your Shiny App Prototyping Skills? Explore {ShinyUiEditor}.

Brief Overview of Shiny Express

Shiny Express is a new, simplified way to write Shiny app prototypes by Posit, offering an easier and quicker development process compared to traditional Shiny. It’s akin to Streamlit in user-friendliness but with greater potential.

Shiny Express includes integrated UI and server code, simplified layout components, and automatic setting of top-level page containers, making it more accessible, especially for beginners. Just like Streamlit, it won’t suit all use cases, but on the contrary will be much easier to refactor into Classic Shiny, with clear UI and server separation, for complex applications.

Key Features of Shiny Express

Here are some key features of Shiny Express:

  • Simplified Code Structure: It has a simpler code structure with fewer concepts, enhancing accessibility for beginners.
  • Integrated UI and Server Code: Unlike traditional Shiny, Shiny Express does not separate UI and server code, streamlining the development process.
  • Layout Components Usage: It introduces layout.foo() for easy nesting of UI components.
  • Automatic Page-Level Container Setting: Shiny Express automatically sets page-level containers like page_fluid() or page_sidebar() based on the used layout components, simplifying layout management.

Installation

Install Shiny and htmltools:

Shiny Express requires the installation of development shiny and htmltools versions from GitHub. In your terminal, execute the following commands:

pip install git+https://github.com/posit-dev/py-htmltools
pip install git+https://github.com/posit-dev/py-shiny

Example

In this Shiny Express example, let’s create an interactive app featuring a dynamic histogram plot with an adjustable slider:

from shiny import ui, render, reactive
from shiny.express import input
import numpy as np
import matplotlib.pyplot as plt

# Setting the title of the panel
ui.panel_title("PyShiny App")

# Creating a slider input for number of samples
ui.input_slider("n_samples", "N Samples", 10, 100, 50)

# Creating a dropdown select input for choosing a color
ui.input_select('selected_color', "Color", ['red', 'green', 'blue'])

# Reactive function to generate random numbers
# It updates when the slider value changes
@reactive.Calc
def x():
    return np.random.randn(input.n_samples())
    
# Function to render the histogram plot
# It updates when either the slider value or the selected color changes
@render.plot
def plot():
    fig, ax = plt.subplots()
    # Histogram of the random numbers with the selected color
    ax.hist(x(), color=input.selected_color())
    return fig

PyShiny App

This example showcases a Shiny Express app that integrates a slider for adjusting sample size and a dropdown for colour selection, dynamically updating a histogram plot in response to user inputs. This illustrates the power and flexibility of Shiny Express in creating interactive and responsive web application prototypes with minimal and intuitive coding.

Comparing Shiny Express with Streamlit

Streamlit App

This Streamlit app mirrors the above Shiny Express example.

import streamlit as st
import numpy as np
import matplotlib.pyplot as plt

# Displaying the title of the Streamlit app
st.title("Streamlit App")

# Creating a slider for number of samples
n_samples = st.slider("N samples", 10, 100, 50)

# Creating a dropdown select box for choosing a color
selected_color = st.selectbox("Color", ["red", "green", "blue"])

# Generating random numbers based on the selected number of samples
x = np.random.randn(n_samples)

# Creating a matplotlib figure and axis
fig, ax = plt.subplots()
# Plotting a histogram of the random numbers with the selected color
ax.hist(x, color=selected_color)
# Displaying the plot in the Streamlit app
st.pyplot(fig)

PyShiny vs Streamlit

  • Flexibility: While Streamlit is user-friendly and suitable for quickly creating simple apps, it has limitations when it comes to more complex or customized functionalities. On the other hand, Shiny Express provides a balance between ease of use and the advanced capabilities of Shiny such as the seamless integration of custom CSS and JavaScript, allowing for more complex app development.
  • Capabilities: While Streamlit excels in simplicity and ease of use, it might not be the ideal choice for applications demanding complex user interactions or advanced data manipulations. This observation becomes apparent when contrasting Streamlit’s script execution model with Shiny’s reactive graph approach, including Shiny Express. The latter’s design not only offers superior scalability but also supports more intricate data processing and user interface dynamics. This makes Shiny, and by extension Shiny Express, more adaptable and suitable for a wider variety of applications, accommodating more complex requirements.
  • Learning Curve: For users familiar with Python and seeking quick app development with minimal learning, Streamlit is often the go-to. Shiny Express, while striving for simplicity, might have a slightly steeper learning curve but leads to a more powerful toolkit, especially beneficial for those wishing to transition to more complex Shiny applications later on.

In summary, Shiny Express offers a more flexible and capable alternative to Streamlit, especially for users looking to create more complex web applications while still benefiting from a simplified development process.

Discover whether Streamlit or Shiny best suits your data science needs in our comprehensive comparison, Choosing the Right Data Dashboard Tool: The Unique Strengths of Streamlit and Shiny.

Potential Challenges

  • Risk of Confusion: Having two distinct ways of writing Shiny apps (Classic and Express) could create confusion, especially for new users.
  • Suitability for Use Cases: Shiny Express is not one-size-fits-all. It’s more suitable for simpler applications or for beginners. The traditional Shiny model is more appropriate for complex, large-scale applications, where intricate control and customization are required.

Conclusion

Shiny Express offers a more accessible entry point for newcomers to Shiny, simplifying the learning curve with its streamlined syntax and integrated UI and server code. Experienced users can benefit from its quick prototyping capabilities while retaining the option to smoothly switch to traditional Shiny for more complex tasks.

The choice between Shiny Express, Classic Shiny, and Streamlit depends on the application’s complexity and specific requirements. Streamlit suits rapid, simple app development, Shiny Express is ideal for intermediate complexity with greater flexibility, and Classic Shiny is best for intricate, highly customised applications. Users can evaluate their needs and expertise level to select the most appropriate framework.

Enjoying our content? Then you’ll love our upcoming event – Shiny Gatherings #9: R Shiny Trivia Night! Whether you’re a solo player or teaming up with friends and colleagues, this is your chance to showcase your R Shiny knowledge. Get ready for an evening of competition, prizes, and fun. Secure your spot today and be part of the excitement.

 

The post appeared first on appsilon.com/blog/.

To leave a comment for the author, please follow the link and comment on their blog: Tag: r - Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)