How to Pull Shiny Usage Data from RStudio (Posit) Connect: API Setup Guide
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.
- Why you should monitor usage data
- Where to look for usage data
- How to set up usage stats monitoring with the RStudio Connect API
- What else you can do with usage data
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).
Why you should monitor Shiny usage data
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.
For rapid app development try Appsilon’s open source Shiny Templates.
Where to look for app usage data
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.
Want to see how users interact with your app? Monitor user sessions for free with shinyHeatmap.
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.
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.
Looking to build REST API with R? Read Appsilon’s Beginner’s Guide to REST APIs with R and the plumber package
How to set up usage stats monitoring with the RStudio (Posit) Connect API
1) Getting the credentials
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 https://rsc.example.com/connect/, the server URL is https://rsc.example.com/.
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.
Next, go to the API Keys section.
Click New API Key and provide a descriptive name.
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:
2) Connecting to the API
Now it’s time to write the script to connect with the API and get the first batch of data.
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 RStudio API documentation.
Finally, send the GET method to the API and save the results.
3) Processing the usage data
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:
The function will return the data frame with 5 columns:
- user (RStudio Connect of a user, if logged in)
- start of the session
- end of the session
- calculated session duration in minutes
You can of course add any other calculations or data processing that you need here.
4) Collecting the usage data
Now, you can collect and process the data from the entire date range specified.
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.
Not sure which RStudio services best fit your project needs? As an RStudio Full Service Certified Partner Appsilon can help you make the right decision.
What else you can do with Shiny usage data
Once you have the data there is a range of possibilities. You can analyze data to learn:
- when your app is used
- how frequently (e.g. how many sessions you have weekly or monthly)
- what is the average duration of the session
- how the app usage changes over time
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:
- Creating production-ready Shiny applications
- Building Shiny dashboards with great UX
- How to improve an app with User Tests
We’re here to help
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 open source packages to simplify the process of creating beautiful, advanced Shiny apps. We’ve even launched a few open source Shiny Dashboard Templates for users to download and implement in their data science projects. We want to build better Shiny tools 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.