Monday, April 12, 2021
  • Setup menu at Appearance » Menus and assign menu to Top Bar Navigation
Advertisement
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
No Result
View All Result
Home Data Science

How to Safely Remove a Dynamic Shiny Module

May 15, 2020
in Data Science
How to Safely Remove a Dynamic Shiny Module
585
SHARES
3.3k
VIEWS
Share on FacebookShare on Twitter

Despite their advantages, Dynamic Shiny Modules can destabilize the Shiny environment and cause its reactive graph to be rendered multiple times. In this blog post, I present how to remove deleted module leftovers and make sure that your Shiny graph observers are rendered just once.

While working with advanced Shiny applications, you have most likely encountered the need for using Shiny Modules. Shiny Modules allow you to modularize the code, reuse it to create multiple components using single functions and prevent the code’s duplication.

You might also like

Job Scope For MSBI In 2021

Leveraging SAP’s Enterprise Data Management tools to enable ML/AI success

Vue.js vs AngularJS Development in 2021: Side-by-Side Comparison

Perhaps the best feature of Shiny Modules is the ability to create dynamic app elements. A great implementation of this can be found here. This particular example provides convenient logic for adding and removing variables and their values in a reactive manner.

Implementing Shiny Modules does come with certain challenges that can affect the stability of your Shiny environment. In this article, I will show you how to overcome them.

Removing the remnants of an obsolete module

Removing a module can have a destabilizing impact on your Shiny app environment. To illustrate this problem, let’s consider this simple application:

The app allows the user to create (and remove) a new module that counts the number of clicks of the button placed inside of the module. The number of clicks is also displayed outside the module in order to see the internal module value after that module is removed.

The expectation is that removing the module would remove its internal objects including input values. Unfortunately, this is not the case:

In fact, removing the module only affects the UI part while the module’s reactive values are still in the Shiny session environment and are rewritten right after a new module is called. This becomes particularly problematic when the module stores large inputs. Adding a new module aggregates the memory used by the application and can quickly exhaust all the available RAM on the server hosting your application. This issue can be resolved by introducing the remove_shiny_inputs function, as explained here. The function allows you to remove input values from an unused Shiny module.

In our implementation, making use of the function requires a simple modification of the remove_module event:

observeEvent(input$remove_module, {

  removeUI(selector = "#module_content")

  shinyjs::disable("remove_module")

  shinyjs::enable("add_module")

  remove_shiny_inputs("my_module", input)

  local_clicks(input[["my_module-local_counter"]])

})
 

Removing internal observers that have registered multiple times

The second issue has likely contributed to hair being ripping out of many Shiny programmers’ heads.

Observe events, just like reactive values in the example above, are not removed when a module is deleted. In this case, the issue is even more serious –  the obsolete observer is replicated rather than overwritten. As a result, the observer is triggered as many times as the new module (with the same id) was created.

In our example, adding a simple print function inside an observeEvent shows the essence of this issue:

observeEvent(input$local_counter, {

  print(paste("Clicked", input$local_counter))

  local_clicks(input$local_counter)

  }, ignoreNULL = FALSE, ignoreInit = TRUE

)

This behavior may cause your application to slow down significantly within just a few minutes of use.

The fastest solution to this problem is a workaround which requires the developer to create new modules with unique identifiers. This way, each new module creates a unique observer and the previous observers are not triggered anymore.

The proper solution, not as commonly known, is offered directly by the Shiny package and does not require any hacky workarounds. We begin by assigning the observer to the selected variable:

my_observer <- observeEvent(...)

We have two options to apply this solution to our example.The my_observer object now allows us to use multiple, helpful methods related to the created observer. One of them, destroy(), provides for correctly removing a Shiny observer from its environment, with:

The first approach calls for assigning the module’s observer to a variable that is accessible from within the Shiny server directly. For instance, we can use reactiveVal that is passed to the module and designed to store observers. 

The second approach makes use of the session$userData object (see Marcin Dubel’s related blog post).

We decided to use the second approach, so we assigned an observer to the session$userData$my_observer variable:

session$userData$clicks_observer <- observeEvent(...)

Then, we modified the remove_module event by adding a destroy action on the variable we just created:

observeEvent(input$remove_module, {

  removeUI(selector = "#module_content")

  shinyjs::disable("remove_module")

  shinyjs::enable("add_module")

  remove_shiny_inputs("my_module", input)

  local_clicks(input[["my_module-local_counter"]])

  session$userData$clicks_observer$destroy()

})

The result met our expectations:

The final application code is available here.

Conclusion

Shiny offers great functionalities for creating advanced, interactive applications. 

Whilst this powerful package is easy to use, we still need to properly manage the application’s low-level objects to ensure optimal performance. If you have any examples of how you struggled with solving other less common Shiny challenges, please share in the comment section below!


Credit: Data Science Central By: Krystian Igras

Previous Post

Startups: Focus on DeepCube, FortressIQ

Next Post

Hackers target the air-gapped networks of the Taiwanese and Philippine military

Related Posts

Job Scope For MSBI In 2021
Data Science

Job Scope For MSBI In 2021

April 11, 2021
Leveraging SAP’s Enterprise Data Management tools to enable ML/AI success
Data Science

Leveraging SAP’s Enterprise Data Management tools to enable ML/AI success

April 11, 2021
Vue.js vs AngularJS Development in 2021: Side-by-Side Comparison
Data Science

Vue.js vs AngularJS Development in 2021: Side-by-Side Comparison

April 10, 2021
5 Dominating IoT Trends Positively Impacting Telecom Sector in 2021
Data Science

5 Dominating IoT Trends Positively Impacting Telecom Sector in 2021

April 10, 2021
Four Alternative Data Trends to Watch in 2021
Data Science

Four Alternative Data Trends to Watch in 2021

April 10, 2021
Next Post
Hackers target the air-gapped networks of the Taiwanese and Philippine military

Hackers target the air-gapped networks of the Taiwanese and Philippine military

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

January 6, 2019
Microsoft, Google Use Artificial Intelligence to Fight Hackers

Microsoft, Google Use Artificial Intelligence to Fight Hackers

January 6, 2019

Categories

  • Artificial Intelligence
  • Big Data
  • Blockchain
  • Crypto News
  • Data Science
  • Digital Marketing
  • Internet Privacy
  • Internet Security
  • Learn to Code
  • Machine Learning
  • Marketing Technology
  • Neural Networks
  • Technology Companies

Don't miss it

Cambridge Quantum Computing Pioneers Quantum Machine Learning Methods for Reasoning
Machine Learning

Cambridge Quantum Computing Pioneers Quantum Machine Learning Methods for Reasoning

April 11, 2021
Why Machine Learning Over Artificial Intelligence?
Machine Learning

Why Machine Learning Over Artificial Intelligence?

April 11, 2021
27 million galaxy morphologies quantified and cataloged with the help of machine learning
Machine Learning

27 million galaxy morphologies quantified and cataloged with the help of machine learning

April 11, 2021
Machine learning and big data needed to learn the language of cancer and Alzheimer’s
Machine Learning

Machine learning and big data needed to learn the language of cancer and Alzheimer’s

April 11, 2021
Job Scope For MSBI In 2021
Data Science

Job Scope For MSBI In 2021

April 11, 2021
Basic laws of physics spruce up machine learning
Machine Learning

New machine learning method accurately predicts battery state of health

April 11, 2021
NikolaNews

NikolaNews.com is an online News Portal which aims to share news about blockchain, AI, Big Data, and Data Privacy and more!

What’s New Here?

  • Cambridge Quantum Computing Pioneers Quantum Machine Learning Methods for Reasoning April 11, 2021
  • Why Machine Learning Over Artificial Intelligence? April 11, 2021
  • 27 million galaxy morphologies quantified and cataloged with the help of machine learning April 11, 2021
  • Machine learning and big data needed to learn the language of cancer and Alzheimer’s April 11, 2021

Subscribe to get more!

© 2019 NikolaNews.com - Global Tech Updates

No Result
View All Result
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News

© 2019 NikolaNews.com - Global Tech Updates