Speeding Up R Shiny - The Definitive Guide

Estimated time:
time
min

<h2>Better App Performance - It Can Be Done!</h2> Prototyping apps in Shiny is fast and easy, but once an app grows, performance issues may arise. Speeding up Shiny is possible and the methods described below can prevent or resolve these issues. There are a few good practices to have in mind in order to keep a growing app performing quickly as well as few things you could do to improve the performance of one already built. In this article, I’ll cover techniques that you can employ to speed up Shiny.  <ol><li><a href="#anchor-1" target="_blank" rel="noopener noreferrer">Proper Data Handling</a></li><li><a href="#anchor-2" target="_blank" rel="noopener noreferrer">Faster Functions</a></li><li><a href="#anchor-3" target="_blank" rel="noopener noreferrer">Multiple Processes</a></li><li><a href="#anchor-4" target="_blank" rel="noopener noreferrer">Scoping Rules</a></li><li><a href="#anchor-5" target="_blank" rel="noopener noreferrer">Unload UI</a></li><li><a href="#anchor-6" target="_blank" rel="noopener noreferrer">Cache Outputs</a></li><li><a href="#anchor-7" target="_blank" rel="noopener noreferrer">Proper Architecture</a></li><li><a href="#anchor-8" target="_blank" rel="noopener noreferrer">Profiling</a></li><li><a href="#anchor-9" target="_blank" rel="noopener noreferrer">Conclusion</a></li></ol> <blockquote>Read more: <a href="https://appsilon.com/why-you-should-use-r-shiny-for-enterprise-application-development/" target="_blank" rel="noopener noreferrer">Why You Should Use Shiny for Enterprise Application Development</a></blockquote> <h2 id="anchor-1">Proper Data Handling</h2> Shiny apps are often built to interact with a dataset. As the dataset grows in size, how you handle it gains importance and affects the performance of the app.    <h3>Preprocessing</h3> Data comes in a lot of forms, but it’s crucial to make the data ready for use beforehand. Avoid including data processing scripts anywhere in the app as they may cause a significant slowdown. If the data is static, preprocess the data once and use it every time the app runs. If the data changes periodically, schedule a script to do it for you.  <h3>Storage</h3> Depending on the size of data and how you interact with it (i.e. how often you load data or what statistics do you calculate), you should consider the way it’s stored. There is no clear and easy way to tell when you should use which solution, but there are some guidelines that can be beneficial to follow. <h3>Small data</h3> Let’s define small data as data that fits into your machine’s memory and allows other processes to run smoothly. In this case, the easiest way is to load the data into memory and interact with it any way you want.  If the data is partitioned into separate datasets that a user can work on, it may be worth considering loading the data dynamically based on the user’s input.  Be aware of the time it takes for the data to load. Base R functions readRDS and read.csv, although popular, are not the fastest out there. Learn about faster alternatives <a href="https://appsilon.com/fast-data-loading-from-files-to-r/" target="_blank" rel="noopener noreferrer">here</a>. <h3>Big data</h3> If small data is the one that fits into your memory, then big data is the one that doesn’t. In this case, there are 3 options differing where the computations are carried on: <ul><li style="font-weight: 400;">Partially on disk: if data won’t fit in RAM, SWAP partition will be used. Doing calculations this way will lead to a significant slowdown (up to an order of magnitude, depending on the machine disk)</li><li style="font-weight: 400;">Directly on disk: <a href="https://github.com/xiaodaigh/disk.frame" target="_blank" rel="noopener noreferrer">disk.frame</a> allows you to manipulate data that doesn’t fit into RAM using data.table or dplyr interface. It splits the data into smaller, “RAM fittable” chunks</li><li style="font-weight: 400;">In external service: database, Spark, Hadoop. When working with databases, dplyr allows you to carry calculations directly in the database without changes in syntax.  </li></ul> <h2 id="anchor-2">Faster Functions</h2> Be aware of faster alternatives to functions you use, especially in most frequent routines as the differences add up and you can gain significant improvement in no time. <h3>Vectorized expressions</h3> Make sure to use vectorized expressions. They are most often not slower than using explicit loops but may bring significant speed improvements. They also simplify the code which in return allows you to spot more potential issues. Just by rewriting a loop to vectorized if/else statement, we obtained a huge speedup. <h3>Fast data reading/writing functions</h3> A common misconception is that using R specific format of serialized data (rds, rda, etc) is the fastest out there. There are far faster alternatives. Using default CSV reader read.csv also proves not to be the fastest.  <table> <tbody> <tr> <td><b>Method            </b></td> <td><b>Format  </b></td> <td><b>Time (ms)  </b></td> <td><b>Size (MB)  </b></td> <td><b>Speed (MB/s)  </b></td> <td><b>N</b></td> </tr> <tr> <td>readRDS</td> <td>bin</td> <td>1577</td> <td>1000</td> <td>633</td> <td>112</td> </tr> <tr> <td>saveRDS</td> <td>bin</td> <td>2042</td> <td>1000</td> <td>489</td> <td>112</td> </tr> <tr> <td>fread</td> <td>csv</td> <td>2925</td> <td>1038</td> <td>410</td> <td>232</td> </tr> <tr> <td>fwrite</td> <td>csv</td> <td>2790</td> <td>1038</td> <td>358</td> <td>241</td> </tr> <tr> <td>read_feather</td> <td>bin</td> <td>3950</td> <td>813</td> <td>253</td> <td>112</td> </tr> <tr> <td>write_feather</td> <td>bin</td> <td>1820</td> <td>813</td> <td>549</td> <td>112</td> </tr> <tr> <td><b>read_fst</b></td> <td><b>bin</b></td> <td><b>457</b></td> <td><b>303</b></td> <td><b>2184</b></td> <td><b>282</b></td> </tr> <tr> <td><b>write_fst</b></td> <td><b>bin</b></td> <td><b>314</b></td> <td><b>303</b></td> <td><b>3180</b></td> <td><b>291</b></td> </tr> </tbody> </table> Table 1. Comparison of reading/writing a data frame with 10 million rows using different methods. Source Using non-base implementation is beneficial, especially when the data is big enough to spot the difference. <h2 id="anchor-3">Multiple Processes</h2> R is single-threaded. This proves to be a major drawback when it comes to creating web applications in such a language. Each function call is executed sequentially, which poses problems when multiple users interact with an R process, when one task takes some time, it will block the others. The following techniques don’t speed up the processes themselves, but by delegating tasks, they unlock the UI, which translates to a smoother experience when multiple users use the app concurrently. <h3>Promises</h3> This package introduces a few functions and operators which allow you to easily convert Shiny applications into asynchronous ones. To convert the app into an asynchronous one, do the following steps: <ol><li style="font-weight: 400;">Load dependencies: promises, future </li><li style="font-weight: 400;">Specify how promises are resolved. In Shiny apps, you want to use a plan(multisession) as it will resolve them in separate sessions on the same machine.</li><li style="font-weight: 400;">Wrap slow operations in future_promise()</li><li style="font-weight: 400;">Any code that relies on the result of that operation now must be converted to promise handlers that operate on the future object. Code below won’t work as the operation is executed in a different process than renderPlot.</li></ol> <h3>shiny.worker</h3> Appsilon has developed a package that allows you to delegate long-running jobs to separate processes - shiny.worker Arguments for the job are provided as a reactive (args_reactive). Its value will be passed to the job function as args. This means that every time the value of this reactive changes, shiny.worker will take action, depending on the strategy you choose. It can be triggering a new job and canceling a running job or ignoring the change (no new job is scheduled until the current is resolved). It is the developer’s responsibility to implement app logic to avoid potential race conditions. To access the worker’s result, you call it as you do with a reactive (plotValuesPromise()). As a result you are able to read its state (plotValuesPromise()$resolved) and returned value (plotValuesPromise()$result). You decide what should be returned when the job is still running with the argument value_until_not_resolved. <h2 id="anchor-4">Scoping Rules</h2> When working with Shiny we could differentiate between 4 scopes rules: <ul><li style="font-weight: 400;"><b>Global</b>: Objects in global.R are loaded into R’s global environment. They persist even after an app stops. This matters in a normal R session, but not when the app is deployed to Shiny Server or Connect. To learn more about how to scale Shiny applications to thousands of users on RStudio Connect, <a href="https://support.rstudio.com/hc/en-us/articles/231874748-Scaling-and-Performance-Tuning-in-RStudio-Connect" target="_blank" rel="noopener noreferrer">this article</a> has some excellent tips. Also, see alternatives to scaling here.</li><li style="font-weight: 400;"><b>Application-leve</b>l: Objects defined in app.R outside of the server function are similar to global objects, except that their lifetime is the same as the app; when the app stops, they go away. These objects can be shared across all Shiny sessions served by a single R process and may serve multiple users.</li><li style="font-weight: 400;"><b>Session-level</b>: Objects defined within the server function are accessible only to one user session.</li><li style="font-weight: 400;"><b>Module/function-level</b>: Objects created inside will be created every time a module/function is called.</li></ul> In general, the best practice is to: <ul><li style="font-weight: 400;">Create objects that you wish to be shared among all users of the Shiny application in the global or app-level scopes (e.g., data, constants).</li><li style="font-weight: 400;">Create objects that you wish to be private to each user as session-level objects (e.g., generating a user avatar or displaying session settings).</li><li style="font-weight: 400;">Avoid creating objects multiple times that could be passed as a parameter when calling modules or functions.</li></ul> <h2 id="anchor-5">Unload UI</h2> If you want to apply a change in the UI, each time Shiny needs to send a message to the browser. You can achieve a visible speedup by applying a few tricks. <h3>Avoid using renderUI</h3> When there’s a component that depends on some value from the server, the easiest way to create it is using renderUI function. This pattern allows you to create components dynamically in the server and send it to the browser to be rendered. In small applications using this pattern may not result in any slowdown, but as apps get bigger and there are more elements that are dynamic it can create significant overhead. To avoid it use update functions such as updateNumericInput. When a Shiny app is created, the UI part is created first, so that a user opening the application already has all inputs ready for usage. This is not the case when using renderUI, that’s why reloading the page results in flickering on inputs. In the most extreme case, components will be appearing one by one as the page loads. <blockquote>Read more: <a href="https://appsilon.com/r-shiny-faster-updateinput-css-javascript/" target="_blank" rel="noopener noreferrer">Make R Shiny Faster with updateInput, CSS, and JavaScript</a></blockquote> <h6>Image 1. Difference between using updateInput and renderUI functions. Updating components seems to work identically, but when the app is reloaded the component using renderUI begins blinking. Observe how values change when clicking the button and what happens when the page is being refreshed.</h6> <img class="aligncenter size-full wp-image-7221" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02058e196d46b4e1eb70b_1_updateinputs.gif" alt="Update Input Gif" width="512" height="313" /> In the case of such a small app, there’s no difference between using updateInput and renderUI when it comes to updating input values. But as the number of components to be updated and the complexity of calculating updated values rises the slowdown will become more visible when using renderUI. As the number of used reactive expressions inside renderUI increases, it may result in it being called multiple times, potentially affecting the performance to a higher degree. <h3>Use JavaScript</h3> To completely remove overhead from communication between the browser and the server you can leverage JavaScript. In the given example we compare using renderUI to update button icon with simple JavaScript code. Even in this minimal example, you can see a slight difference in the speed of an update. In the case of a bigger app, where such updates take place more often, those differences add up and result in a smoother interaction with the app. <img class="aligncenter size-full wp-image-7222" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02058369f5360b2ae73b2_2_iconclick.gif" alt="Variable Icon Update Response" width="512" height="187" /> <h2 id="anchor-6">Cache outputs</h2> Caching is an operation of saving a result of a function to a file. It is a go-to solution wherever there exists a heavy, repeated operation that yields results from a limited set of results (e.g. plotting from a subset of data based on some input). <h3>Generic Usage</h3> Caching may be quite easily implemented on your own, but oftentimes it's better to use ready-made solutions. For generic usage, <a href="https://github.com/r-lib/memoise">memoise</a> can be used.  <h3>Shiny Built-In Mechanism</h3> One of the common bottlenecks in Shiny apps is output rendering. When the output depends on a combination of inputs and those combinations will occur more than once in an app's lifetime, you can cache them.  From Shiny 1.6.0 you can now use convenient built-in caching mechanisms for all types of outputs. You can simply chain a bindCache call to reactive or render functions. In the given example, the plot will be calculated once for each value of n. Other times it will use saved images and put them in the app, reducing the time it takes for the plot to appear. You can combine multiple reactive values within the bindCache call. If you want the plot to be invalidated only when a button is clicked, you can use bindEvent. The above expression will cache the plot as n changes, but it will wait for the button to be clicked. You can cache reactive expressions as well using the same syntax. <h3>Cache scoping</h3> When using Shiny built-in caching, it’s important to be aware of different cache scoping. There are 3 scopes, which you can set either by setting a global option or in each caching function call: <ul><li style="font-weight: 400;"><b>App</b>: <ul><li style="font-weight: 400;">Share cache between sessions run on each R process, useful when multiple users can share the same results.</li><li style="font-weight: 400;">shinyOptions(cache = "app"), bindCache(..., cache = "app") </li></ul> </li> <li style="font-weight: 400;"><b>Session</b>: <ul><li style="font-weight: 400;">Keep cache for each session. Useful when the value should remain private for the user.</li><li style="font-weight: 400;">shinyOptions(cache = "session"), bindCache(..., cache = "session")</li></ul> </li> <li style="font-weight: 400;"><b>Persistent</b>: <ul><li style="font-weight: 400;">Share cache between sessions and R processes. Cache persists after the app closes. Useful when there are a lot of users and the app runs on multiple processes. If the cache directory is created in a temporary folder, it will be deleted automatically after the machine restarts. </li><li style="font-weight: 400;">shinyOptions(cache = cachem::cache_disk("./<cache dir>"))</li></ul> </li> </ul> <h2 id="anchor-7">Use proper architecture</h2> Even after applying all the best practices when developing the application, you need to ask yourself whether you serve it to users properly. Depending on your needs and budget there are a few options available (e.g. RStudio Connect). <h3>RStudio Connect</h3> Using <a href="https://rstudio.com/products/connect/" target="_blank" rel="noopener noreferrer">RStudio Connect</a>, you can have multiple R processes per app. This means that many concurrent users can be distributed between separate processes and are served more efficiently. As there is no limitation on the number of processes, you can make use of all your machine resources. You can configure a strategy on how resources should be handled with the utilization_scheduler parameter. For example, you can set: <ul><li style="font-weight: 400;">The maximum R process capacity (i.e. the number of concurrent users per single R process).</li><li style="font-weight: 400;">The maximum number of R processes per single app.</li><li style="font-weight: 400;">When the server should spawn a new R process (e.g. when existing processes reach 90% of their capacity).</li></ul> <img class="aligncenter size-full wp-image-7223" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b020591acc439bc25aab05_3_rstudiodistribution.webp" alt="Rstudio distribution of features" width="600" height="336" /> RStudio Connect is a go-to solution as it offers multiple features with a click of a button, managing apps, authorization, scheduling, distribution, and security options that are unavailable anywhere else. For a list of approaches to scaling Shiny, see this article.  <h2 id="anchor-8">Profiling</h2> When using the app you can spot that in some places it may be working slower than expected. Seeing for example, that a plot takes some time to render gives you a good sense of which part of the code is the culprit. But don't rely on your gut feeling about which part of the code is responsible for the slowdown. That's where profvis comes to the rescue! It allows you to spot which exact functions consume the most time. Suppose you have an app that creates a sample of data and puts it on the plot: Call to pause in the prepareData function represents a computation-heavy function. In the case of such a small app it's easy to spot which lines are responsible for the slowdown without using any tools, but for sake of presentation let's continue with the example. Suppose we have saved the script above in the app.R file and we're in the directory where it's located. The basic usage of profvis is to wrap an expression in profvis call: The profile will capture every function call while the app is used, as well as any calls that happen before launching the app until the profiling is ended from within RStudio or the app is stopped. <img class="aligncenter size-full wp-image-7226" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0205af75d40a9af720a3d_6_preparedata.webp" alt="Data preparation table" width="512" height="352" /> We can clearly see that most of the time is consumed by the prepareData function, which should be a focus of optimization. <h4>profvis Module</h4> Such usage of profvis proves especially useful when the app is small. It's also a great tool when you want to focus on the startup of the app. As the app gets more complicated, there are more function calls to the point where the profile report may become obscured or even grow so big that it would take a vast amount of time to render it! If that is the case, it's better to use a module provided by profvis. Just add profvis_ui and profvis_server to the app.  It adds a widget that allows you to start and stop profiling at any moment of using the app. And it allows you to conveniently download reports on the go. <img class="aligncenter size-full wp-image-7227" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0205b48b83fcd2e56a2c5_7_widget.gif" alt="shinyapp widget" width="600" height="299" /> <h3>What to pay attention to</h3> <h4><strong>Reactivity</strong></h4> Reactive framework that Shiny implements, although easy to use, may prove tricky as it’s easy to get entangled in reactive dependencies. Pay special attention to ensure that reactive expressions get invalidated exactly when you want them to. Including too many dependencies may result in a reactive being invalidated multiple times, effectively slowing down the application. <a href="https://rstudio.github.io/reactlog/" target="_blank" rel="noopener noreferrer">Reactlog</a> may prove effective when it comes to spotting undesired behavior. <h4><strong>Unnecessary services</strong></h4> When evaluating why the app is slow, see whether all its components are actually necessary. Is it possible some routine that the app does could be done once before and not on every startup? Or maybe you use some external service that takes some time to fire-up, up but could be substituted with a lighter alternative? <h4><strong>Observe the console</strong></h4> Functions usually don’t print anything to the console, but some of them do. For instance, calls to library or services that are started from within R. By observing the console’s output you can access whether parts of code are being executed in inappropriate moments (e.g. attaching a library when the app is already running) and either remove unnecessary parts or move them to a more suitable place.  <h2 id="anchor-9">Conclusion</h2> Speeding up R Shiny is possible and achieving it is relatively easy with a few best practices and an understanding of your app's needs. Not all of the techniques may apply to your unique app, but most of the steps here can save time and effort, and avoid causing headaches. It's important to take time and think about data handling, processing, etc. before you begin your project to create a smooth, coherent app. But no matter the stage of your project, you can follow the guideline above and find ways to improve the performance of your Shiny app. Feel free to explore more of Appsilon's open-source <a href="https://shiny.tools/" target="_blank" rel="noopener noreferrer">R Shiny packages</a> and discover other ways you might improve your app. If you have any comments head to our <a href="https://github.com/Appsilon" target="_blank" rel="noopener noreferrer">Github</a> and join our discussion threads. And of course, if you enjoy our packages please consider dropping a star on your favorite ones. <h2>We're Hiring!</h2> Interested in working with the leading experts in Shiny? Appsilon is looking for creative thinkers around the globe. We're a remote-first company, with team members in 7+ countries. Our team members are leaders in the R dev community and we take our core purpose seriously. <blockquote>Advance technology to preserve and improve human life #purpose</blockquote> We promote an inclusive work environment and strive to create a friendly team with a diverse set of skills and a commitment to excellence.<a href="https://appsilon.com/company/"> Contact us</a> and see what it's like to work on groundbreaking projects with Fortune 500 companies, NGOs, and non-profit organizations. <img class="aligncenter size-full wp-image-7024" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b020493e47f6a35730bb22_Be-a-part-of-our-team.webp" alt="Be a part of our team" width="1200" height="628" /> <p style="text-align: center;"><b>Appsilon is hiring for remote roles! See our </b><a href="https://appsilon.com/careers/" target="_blank" rel="noopener noreferrer"><b>Careers</b></a><b> page for all open positions, including a</b><a href="https://appsilon.com/careers/job-offer/?job=senior-react-developer-freelancer" target="_blank" rel="noopener noreferrer"> <b>React Developer</b></a><b> and</b><a href="https://appsilon.com/careers/job-offer/?job=r-shiny-developer" target="_blank" rel="noopener noreferrer"> <b>R Shiny Developers</b></a>.<b> Join Appsilon and work on groundbreaking projects with the world's most influential Fortune 500 companies.</b></p> &nbsp;

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
speed up shiny
shiny dashboards
r
rstudio
tutorials