How to Pull Shiny Usage Data from RStudio (Posit) Connect: API Setup Guide

Estimated time:

Let’s imagine you’ve built a Shiny app and published it. Now, you start getting spontaneous feedback from your colleagues who use it. But do you actually know how often the application is used? When is it mostly used or for how long? If you’ve made it this far, it’s the best time to learn how to take advantage of Shiny usage data. Having deployed your app with RStudio Connect, you can use their API to gain a deeper insight into user data. This quick guide will help you prepare the right setup, so you can monitor your Shiny app usage like a pro. <ul><li><a href="#anchor-1" target="_blank" rel="noopener noreferrer">Why you should monitor usage data</a></li><li><a href="#anchor-2" target="_blank" rel="noopener noreferrer">Where to look for usage data</a></li><li><a href="#anchor-3" target="_blank" rel="noopener noreferrer">How to set up usage stats monitoring with the RStudio Connect API</a></li><li><a href="#anchor-4" target="_blank" rel="noopener noreferrer">What else you can do with usage data</a></li></ul> Note: At the time of writing this article, Posit PBC was RStudio PBC. We use RStudio and Posit interchangeably in this text (e.g. RStudio Connect == Posit Connect). <h2 id="anchor-1">Why you should monitor Shiny usage data</h2> This information is crucial for the further development of your Shiny app. You should be able to monitor the usage of your apps to check if they get user buy-in and to know how successful you are at meeting user needs. If you see that your app is a ghost town or the usage is far below your expectations, you should check what’s stopping people from using the app. Maybe it isn’t intuitive enough? Maybe they don’t need it? Or maybe they just don’t know about it? On the other hand, when you showcase the success of your app with hard numbers, you are more likely to gain stakeholders’ interest. A data-driven approach gives you better chances of securing a budget for further development. <blockquote><strong>For rapid app development try <a href="" target="_blank" rel="noopener noreferrer">Appsilon's open source Shiny Templates</a>.</strong></blockquote> <h2 id="anchor-2">Where to look for app usage data</h2> There are some tools for gathering information about usage stats, like Google Analytics or Matomo. They are both great tools with many functionalities that will allow you to analyze user flows in detail. However, they require some additional setup and they only display the data from the moment you set them up. So if your application has been up and running for half a year, you would still have no clue how it’s been used so far. <blockquote>Want to see how users interact with your app? <a href="" target="_blank" rel="noopener">Monitor user sessions for free with shinyHeatmap</a>.</blockquote> When you use RStudio Connect to deploy your Shiny application, your life is already easier and you’ll break no sweat monitoring the usage stats. For a quick check, you can view the summarised usage in the past 30 days by going to the Info tab in the Settings of a deployed application. Just open the app from the main page of RStudio Connect, go to Settings in the upper-right corner and switch to the Info tab where you’ll find a ‘Past 30 days usage’ graph. However, this information will give you a very limited overview of how your user base grows. <img class="wp-image-8119 size-full" src="" alt="" width="650" height="700" /> Past 30 days of application usage in RStudio Connect interface. The good news is that RStudio (Posit) offers an API to help you pull out much more granular information on app usage. Don’t worry if you aren't an experienced developer or you’ve never used an API before. It's really easy and I will show you how to do it in 4 simple steps. <blockquote><strong>Looking to build REST API with R? Read Appsilon's <a href="" target="_blank" rel="noopener noreferrer">Beginner's Guide to REST APIs with R and the plumber package</a></strong></blockquote> <h2 id="anchor-3">How to set up usage stats monitoring with the RStudio (Posit) Connect API</h2> <h3>1) Getting the credentials</h3> Credentials in the form of an API key are used to identify and authenticate API users. Remember to keep your credentials safe and never publish the API key in an open online repository. In my example, I will place the credentials in the .Renviron file. First, you need to get the server URL. To do so, link to your RStudio Connect without the last connect/ part. This way, if you access the dashboard at <a href="" target="_blank" rel="noopener noreferrer"></a>, the server URL is <a href="" target="_blank" rel="noopener noreferrer"></a>. Second, you need an API key. In order to generate the API key, open RStudio Connect and click your username in the upper-right corner. <img class="size-full wp-image-8120" src="" alt="" width="239" height="228" /> User menu view in RStudio Connect. Next, go to the API Keys section. <img class="size-full wp-image-8121" src="" alt="" width="1293" height="229" /> API Keys view in RStudio Connect. Click New API Key and provide a descriptive name. <img class="size-full wp-image-8122" src="" alt="" width="509" height="235" /> Creating a new API Key in RStudio Connect. Click OK and the API Key will be generated. Remember to copy it and save it in a secure place, as you won’t be able to come back to this step later! The last thing you need is an application GUID, which is a unique id of your Shiny app. You will find it at the bottom of the Info tab in the Settings of your deployed app. Once you have all three - the server URL, the API Key, and the app GUID - put them in the .Renviron file, like in the example below: <script src=""></script> <h3>2) Connecting to the API</h3> Now it’s time to write the script to connect with the API and get the first batch of data. <script src=""></script> First, load the httr package and variables from .Renviron. Then, set the date range for the data you will be pulling out (start_date and end_date).  Next, create the API call URL. The data is returned in pages and you can set the number of records per page to 30. In the API call URL, you should provide the app GUID (otherwise, you will pull out the data for all apps deployed on that RStudio Connect account) and you may specify additional parameters. In my example, that will be the date range of the usage data I want to pull out. You can check other parameters in the <a href="" target="_blank" rel="noopener noreferrer">RStudio API documentation</a>. Finally, send the GET method to the API and save the results. <h3>3) Processing the usage data</h3> The first page of the usage data is in payload$results. You can see that it’s a nested list. In order to process the data, I will use the following function:  <script src=""></script> The function will return the data frame with 5 columns: <ul><li style="font-weight: 400;" aria-level="1">id </li><li style="font-weight: 400;" aria-level="1">user (RStudio Connect of a user, if logged in) </li><li style="font-weight: 400;" aria-level="1">start of the session </li><li style="font-weight: 400;" aria-level="1">end of the session </li><li style="font-weight: 400;" aria-level="1">calculated session duration in minutes</li></ul> You can of course add any other calculations or data processing that you need here. <h3>4) Collecting the usage data</h3> Now, you can collect and process the data from the entire date range specified. <script src=""></script> First, initialize the data frame with the first 30 records that you’ve already pulled out. Then, use a loop to iterate through all the pages, process the data, and bind it together. <blockquote><strong>Not sure which RStudio services best fit your project needs? As an <a href="" target="_blank" rel="noopener noreferrer">RStudio Full Service Certified Partner</a> Appsilon can help you make the right decision.</strong></blockquote> <h2 id="anchor-4">What else you can do with Shiny usage data</h2> Once you have the data there is a range of possibilities. You can analyze data to learn: <ul><li style="font-weight: 400;" aria-level="1">when your app is used </li><li style="font-weight: 400;" aria-level="1">how frequently (e.g. how many sessions you have weekly or monthly) </li><li style="font-weight: 400;" aria-level="1">what is the average duration of the session </li><li style="font-weight: 400;" aria-level="1">how the app usage changes over time</li></ul> You can go even further and prepare automated reports that will pull out new data regularly, or build a Shiny dashboard to monitor the usage stats on a daily basis.  However, most importantly use the data to get insights, change insights to actions and polish your app's performance and usability. You can check out our blog posts on improving Shiny apps: <ul><li style="font-weight: 400;" aria-level="1"><a href="" target="_blank" rel="noopener noreferrer">Creating production-ready Shiny applications</a></li><li style="font-weight: 400;" aria-level="1"><a href="" target="_blank" rel="noopener noreferrer">Building Shiny dashboards with great UX</a></li><li style="font-weight: 400;" aria-level="1"><a href="" target="_blank" rel="noopener noreferrer">How to improve an app with User Tests</a></li></ul> Good luck! <h3 id="anchor-4">We're here to help</h3> Building a successful Shiny app is no easy feat. The beauty of the R/Shiny combo is having complete control over customizing applications with R's powerful data handling capabilities. But every step along the development path from PoCs to a product launch poses its own challenges. The team at Appsilon has spent years developing frameworks for building enterprise Shiny apps. Along the way, we've developed <a href="" target="_blank" rel="noopener noreferrer">open source packages</a> to simplify the process of creating beautiful, <a href="" target="_blank" rel="noopener noreferrer">advanced Shiny apps</a>. We've even launched a few open source <a href="" target="_blank" rel="noopener noreferrer">Shiny Dashboard Templates</a> for users to download and implement in their data science projects. We want to build better <a href="" target="_blank" rel="noopener noreferrer">Shiny tools</a> and support the R community along the way. If you need help with your project, let's connect. We'd love to solve your development challenges. &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.
Have questions or insights?
Engage with experts, share ideas and take your data journey to the next level!
Join Slack
Explore Possibilities

Take Your Business Further with Custom Data Solutions

Unlock the full potential of your enterprise with our data services, tailored to the unique needs of Fortune 500 companies. Elevate your strategy—connect with us today!

Talk to our Experts
rstudio connect
data analytics