https://github.com/samedwardes/r6-and-shiny
https://github.com/samedwardes/r6-and-shiny
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/samedwardes/r6-and-shiny
- Owner: SamEdwardes
- License: mit
- Created: 2023-01-24T19:36:54.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-24T21:17:48.000Z (over 3 years ago)
- Last Synced: 2025-02-12T17:50:04.126Z (over 1 year ago)
- Language: R
- Size: 1.28 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# r6-and-shiny
This repo demonstrates an approach to use R6Class shiny modules without sharing state.
## Example 1 - No shared state (the desired outcome)
The key to not share any state is to instantiate the module inside of the server function. See [app-no-shared-state.R](./app-no-shared-state.R). The downside of this approach is that you instantiate the object twice. This could slow down the performance of the app.
```r
shiny::runApp('app-no-shared-state.R')
```
In the gif below two different sessions are running. Notice that the changes to the input box in one session have no effect on the other session.

## Example 2 - Shared state (the bad outcome)
In this example, the class is instantiated outside of the server function. See [app-shared-state.R](./app-shared-state.R).
```r
shiny::runApp('app-shared-state.R')
```
In the gif below two different sessions are running. Notice that when changes are made to the session on the right hand side the value also changes on the left hand side.

## Example 3 - Do not use R6
In this example, we do not use R6 at all. Both the UI and the server as defined only as functions. See [app-no-r6.R](./app-no-r6.R).
```r
shiny::runApp('app-no-r6.R')
```