-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadding-logging-to-a-shiny-app.Rmd
More file actions
94 lines (63 loc) · 3.25 KB
/
adding-logging-to-a-shiny-app.Rmd
File metadata and controls
94 lines (63 loc) · 3.25 KB
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
---
title: "Adding logging to a shiny app"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Adding logging to a shiny app}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## Information needed
### ID of Google sheet to use
To keep all logging sheets together, it is advised to create an initial Google sheet and then add new sheets as you add logging to apps. You will need the ID of the initial sheet. This can be found from the URL.
#### Example
A sheet with URL
```
https://docs.google.com/spreadsheets/d/1PZJ_xCbZPSlMzfmjtrM0ePB6115Qqp5fYmLxTNTa5ms/edit?gid=0#gid=0
```
has ID `1PZJ_xCbZPSlMzfmjtrM0ePB6115Qqp5fYmLxTNTa5ms`
### Google account username
You will need the username of the Google account. Typically this will be an email.
## Add credentials
Since the shiny app will be deployed, you need to provide it with a 'hard copy' of the credentials. To add these, use `add_credentials()`. Ensure you are in the root folder of the R project holding the app in Rstudio. Two pop-ups will appear, one for sheet ID and one for username.
These pop-ups will allow the credentials to be cached in your OS secrets store securely. This makes them permanently available, ready for the next time you need them. If necessary, the option to install and update the `keyring` package will be shown.
The file in which credentials are saved will be added to the `.gitignore` file automatically.
## Set up logging sheet
Once the credentials are added you can add a new sheet for the app. To do this, again ensuring you are in the root directory of the R project containing the app, run `setup_sheet()`. This will append a new sheet to the sheet with ID provided earlier. By default, `login`, `logout`, `duration` and `tag` will be logged. Although `sessionid` and `username` are also available, these have potential to be treated as PII.
Before the sheet can be added, authorisation must take place. You have two options.
Option 1. Place an existing authorisation file in a folder `.secret`, within the root folder of the project.
Option 2. Let a browser window open, and confirm authorisation. This will save an authorisation file in `.secret`.
The `.secret` folder, whether pasted in or created anew, will be added to the `.gitignore` file automatically.
## Add code to server function
Add the logging code to the shiny server function. This should be placed at the top, before any other code.
``` r
server <- function(input, output, session) {
shinyusertracking::use_logging()
... # Existing code below
...
...
}
```
By default, the `tag` used will be the deployed app's URL. You can set a custom `tag` like
```r
shinyusertracking::use_logging(tag = "Version for 2023/24")
```
## Shiny apps as packages
If your shiny app is a package, such as when using `golem`, you will need to add this package to your `DESCRIPTION` file in `Imports` and `Remotes`.
```
Imports:
...,
shinyusertracking,
...
Remotes:
...,
nhsbsa-data-analytics/shinyusertracking,
...
```
## App deployment
When deploying the shiny app, ensure that both the credentials file (`.google-sheets-credentials` by default) and `.secret` folder are bundled with the usual files.