Python-bloggers

Unveiling Bottlenecks (Part 2): A Deep Dive into Profiling Tools

This article was first published on Appsilon | Enterprise R Shiny Dashboards , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In our previous blog post, we introduced the concept of profiling for optimizing Shiny app performance. Today, we’ll take a deep dive into three powerful tools in this arsenal: reactlog, profvis and shiny.tictoc.

Imagine your Shiny app – users are interacting seamlessly, data is processing swiftly, and visualizations update effortlessly. This blog post has everything you need to know. 

We’ll demonstrate their functionalities on a more intricate Shiny app, providing insights into their strengths and potential limitations.

reactlog

reactlog acts as a debugger for your Shiny app’s reactive behavior. It visualizes a dependency graph, revealing how user inputs and reactive expressions interact. This helps you identify overly sensitive reactive elements that re-run too often and slow down your app. By analyzing the graph, you can optimize your reactive logic and ensure a smooth user experience.

Setup reactlog

  1. Installation: Run install.packages(“reactlog”)
  2. Enable reactlog: Run reactlog::reactlog_enable().
  3. Run your app: shiny::runApp().
  4. Interact with the app: Use the various input controls and observe the app’s behavior. The interactions you perform here will be recorded by reactlog.
  5. Launch reactlog: Run shiny::reactlogShow() to view dependency graph

Deep dive into reactlog

Status bar

Progress Bar: Visualizing Reactive Flow

The current step section provides in-depth information about the specific reactive step you’re currently viewing:

Search Bar:

The search bar helps you navigate potentially complex reactive graphs with ease:

Navigation Buttons: Exploring the Reactive Timeline

The reactlog status bar provides a set of navigation buttons that allow you to move forward and backward through the timeline of your app’s reactive computations. These buttons offer different levels of granularity:

Dependency Graph

Dependency graph can be complex for simple apps and simple for complex apps. It provides a useful high-level view of your app reactivity and can provide good insight on fixing the reactivity of your app. This helps in avoiding unnecessary reactive and observe calls.

Legend:

Analyzing Output

profvis

profvis is an R package specially designed for performance profiling in Shiny apps. It takes a different approach than reactlog where it acts like a code execution profiler. Imagine a flame graph – profvis captures how long different parts of your code take to run. This helps you identify bottlenecks, sections of code that consume a significant amount of execution time. By analyzing the profvis flame graph, you can optimize your code and make your Shiny app run faster.

Setup profvis

  1. Installation: Run install.packages(“profvis”)
  2. Launch in profiling mode: Setting up profvis is very easy, simply use profvis::profvis( runApp(…) ) ) and later analyze the flame graph. Most complex step ever: Setting up profvis is really difficult and requires a lot of RShiny experience. If you are working on Rshiny since its inception and know how to write profvis::profvis( runApp(…) ) ) then only you’ll be able to use profvis to profile your app
  3. Interact with the app: Play around with like a normal user exploring different scenarios and use cases.
  4. Final step: Close the app and on the console, click on “Stop Profiling”. A profiler output window will appear automatically.

Deep dive into profvis

Flame graph

Analyzing Output

Analyzing the Flame Graph:

Interpreting the Information:

Prototyping apps in Shiny is fast and easy, but once an app grows, performance issues may arise. Check out the definitive guide to speeding up your R/Shiny app.

Which one is better?

Both reactlog and profvis are powerful tools for optimizing Shiny app performance, but they offer distinct perspectives. Choosing the “better” option depends on the specific issue you’re trying to diagnose. Let’s delve into their unique strengths and see when each one shines:

Quick Comparison

shiny.tictoc

Sometimes difficult problems have simple solutions. It’s okay if you feel overwhelmed by the above-mentioned packages. If you want to avoid diving into the rabbit hole of profiling and analyzing flame or dependency graphs, we’ve an alternate solution for you to start with. Presenting shiny.tictoc made with in Appsilon’s laboratory.

shiny.tictoc helps you measure the performance of your app without installing additional package or writing scenarios. It provides a simple visualization that helps you measure how much time it takes to recalculate each output of your app and how long do server side computations take.

Setup shiny.tictoc

  1. Installation: You DON’T need to install anything
  2. Integration: Include a js script in your app’s ui
    tags$script(src = “https://cdn.jsdelivr.net/gh/Appsilon/shiny.tictoc@v0.2.0/shiny-tic-toc.min.js”)
  3. Run your app: shiny::runApp().
  4. Interact with your app: Use the various input controls and observe the app’s behavior.
  5. How to view the output? some text
    1. Open browser tools: Windows: F12, macOS: ⌘ + ⌥ + I
    2. Run below commandssome text
      1. Print out all measurements: showAllMeasurements()
      2. Download all measurements as a CSV file: exportMeasurements()
      3. Print out summarised measurements (slowest rendering output, slowest server computation): showSummarisedMeasurements()
      4. Export an html file that visualizes measurements on a timeline: await exportHtmlReport()

Analyzing output

shiny.tictoc outputs looks like this:

You need to remember a few things before analyzing it:

  1. X – axis shows timeline.
  2. Red is for outputs (it looks at the shiny:recalculating and shiny:value events)
  3. Green is for custom message handlers (looks at the shiny:message event)
  4. Blue is for general server side calculations (shiny:busy and shiny:idle events)

To learn more about js events in shiny, check out the official documentation.

Why shiny.tictoc?

  1. Quick profiling.
  2. Provides a good starting point.
  3. Can be integrated in production applications. Developers can test the performance of deployed applications.

For more information on shiny.tictoc, check out the official repo.

The Power of Combining Forces

All 3 packages mentioned are not mutually exclusive. In fact, combining them offers a comprehensive view of your app’s performance:

By working together, these tools empower you to not only identify potential problems but also pinpoint their root causes within your code. This holistic approach allows you to make informed decisions for optimizing your Shiny app’s performance and user experience.

Stay up to date on the latest in R/Shiny. Register for our weekly newsletter today.

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

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

Want to share your content on python-bloggers? click here.
Exit mobile version