forked from sol-eng/connect-usage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
118 lines (96 loc) · 3.71 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
# RStudio Connect Usage Data
This repository illustrates several examples for getting started with the
RStudio Connect usage data. The examples:
- [./examples/last_30_days](./examples/last_30_days)
- [./examples/interactive_app](./examples/interactive_app)
- [./examples/realtime](./examples/realtime)
- [./examples/connectAnalytics](./examples/connectAnalytics)
- [./examples/connectViz](./examples/connectViz)
The examples are generated using the [RStudio Connect Server
API](https://docs.rstudio.com/connect/api). The API and data collection are both
available as of RStudio Connect 1.7.0. The API contains data to help answer
questions like:
- What content is most visited?
- Who is visiting my content?
- What reports are most common?
- Has viewership increased over time?
- Did my CEO actually visit this app?
**A data science team's time is precious, this data will help you focus and justify your efforts.**
## Basic example
The following code should work as-is if copied into your R session. NOTE that it
uses the [`connectapi`](https://github.com/rstudio/connectapi) package, which
can be installed from GitHub with
`remotes::install_github("rstudio/connectapi")`. You just need to replace the
server name, and have your API key ready.
```{r connect_creds, eval=FALSE}
## ACTION REQUIRED: Change the server URL below to your server's URL
Sys.setenv("CONNECT_SERVER" = "https://connect.example.com/rsc")
## ACTION REQUIRED: Make sure to have your API key ready
Sys.setenv("CONNECT_API_KEY" = rstudioapi::askForPassword("Enter Connect Token:"))
```
This will use the `get_usage_shiny()` function to pull the latest activity of
the Shiny apps you are allowed to see within your server.
```{r shiny_usage}
library(ggplot2)
library(dplyr)
library(connectapi)
client <- connect()
# Get and clean the Shiny usage data
shiny_rsc <- get_usage_shiny(
client,
from = lubridate::today() - lubridate::ddays(7),
limit = Inf
) %>%
filter(!is.na(ended)) %>%
mutate(session_duration = ended - started)
glimpse(shiny_rsc)
```
The identifiers used for the content in RStudio Connect are GUIDs. We can
retrieve content names using the API. The API handles only one GUID at a time,
so `purrr`'s `map_dfr()` is used to iterate through all of the unique GUIDs in
order to get every Shiny app's title.
```{r shiny_content_names}
# Get the title of each Shiny app
shiny_rsc_titles <- shiny_rsc %>%
count(content_guid) %>%
pull(content_guid) %>%
purrr::map_dfr(
~tibble(content_guid = .x, content_name = content_title(client, .x))
)
glimpse(shiny_rsc_titles)
```
The new `shiny_rsc_titles` table, and the `shiny_rsc` can be joined to return
the "user readable" title. Using standard `dplyr` and `ggplot2` functions, we
can now determine things such as the top 10 apps based on how long their average
sessions are.
```{r analyze_data}
# Calculate the average session duration and sort
app_sessions <- shiny_rsc %>%
inner_join(shiny_rsc_titles, by = "content_guid") %>%
group_by(content_name) %>%
summarise(avg_session = mean(session_duration)) %>%
ungroup() %>%
arrange(desc(avg_session)) %>%
head(10)
# Plot the top 10 used content
app_sessions %>%
ggplot(aes(content_name, avg_session)) +
geom_col() +
scale_y_continuous() +
geom_text(aes(y = (avg_session + 200), label = round(avg_session)), size = 3) +
coord_flip() +
theme_bw() +
labs(
title = "RStudio Connect - Top 10",
subtitle = "Shiny Apps",
x = "",
y = "Average session time (seconds)"
)
```
Learn more about programmatic deployments, calling the server API, and custom emails [here](https://docs.rstudio.com/user).