Skip to content

Commit cbdf131

Browse files
committed
Touch up the vignette for CRAN
1 parent e04811b commit cbdf131

File tree

1 file changed

+125
-40
lines changed

1 file changed

+125
-40
lines changed

vignettes/behind-the-scenes.qmd

+125-40
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,77 @@ Deployment.
5858

5959
The first step in converting to Shinylive involves preparing your Shiny
6060
application code. The core application logic remains largely unchanged, but
61-
there are some key considerations:
61+
there are some adjustments that may be required:
6262

6363
- All dependencies must be available in WebAssembly format
6464
- Check if the R package is in the [webR package repository][webrpkg] (mobile intensive)
6565
- See if the Python package is apart of the [built-in Pyodide packages][pyodidepkg] or
6666
has a **pure Python** implementation that can be used directly from PyPI.
6767
- File paths need to be adjusted for browser context
68-
- External resources must be properly bundled
68+
- Use relative paths to refer to scripts or data.
69+
- Keep data files in your app directory or one of its subdirectories.
70+
- External resources need to be retrieved using specific functions that
71+
have been specially designed to work in the WebAssembly environment through
72+
shims or are pre-packaged.
73+
74+
For example, in R, we could pre-package data with the application using:
75+
76+
```r
77+
# Before: External data source
78+
api_data <- fetch_from_api("https://api.example.com/data")
79+
80+
# After: Bundled data
81+
## Setup data
82+
api_data <- fetch_from_api("https://api.example.com/data")
83+
jsonlite::write_json(api_data, "data.json") # Pre-bundled data
84+
85+
# Retrieve data from bundle
86+
data <- jsonlite::fromJSON("data.json")
87+
```
88+
89+
Alternatively, if the API permits [Cross-origin resource sharing (CORS)](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) and
90+
can be switched to using `download.files()`,
91+
then the data can be downloaded into the virtual system.
92+
93+
```r
94+
# Before: External data source
95+
api_data <- fetch_from_api("https://api.example.com/data")
96+
97+
# After: Using download.file to fetch data
98+
download.files("https://api.example.com/data", "data.json")
99+
data <- jsonlite::fromJSON("data.json") # Pre-bundled data
100+
```
69101

70102
### Step 2: Conversion Process
71103

72-
The conversion to Shinylive is automated by the
104+
The conversion to Shinylive is automated by either the [{py-shinylive}][pyshinylive] or
105+
[{r-shinylive}][rshinylive] packages.
106+
107+
Specifically, we can convert a shiny application found in `myapp/` directory to
108+
a shinylive application via either:
109+
110+
For R Shiny application, please type in the **R console**:
111+
112+
```r
113+
# install.packages("shinylive")
114+
shinylive::export("myapp", "_site")
115+
```
116+
117+
For Python Shiny application, please type in the **Terminal**:
118+
119+
```sh
120+
# pip install shinylive
121+
shinylive export myapp _site
122+
```
123+
124+
Behind the scenes, the `export` command is performing the following steps:
73125

74126
1. **File Bundling**: All application files are collected and bundled together:
75127
- R/Python source code
76128
- Data files
77129
- Static assets (images, CSS, etc.)
78130

79-
2. **Metadata Generation**: A `app.json` file is created containing:
131+
2. **Metadata Generation**: A `app.json` file is created containing the source:
80132
```json
81133
[
82134
{
@@ -85,63 +137,86 @@ The conversion to Shinylive is automated by the
85137
"type": "text"
86138
},
87139
{
88-
"name": "data.csv",
140+
"name": "data/dataset.csv",
89141
"content": "col1,col2\n...",
90142
"type": "text"
143+
},
144+
{
145+
"name": "www/image.png",
146+
"content": "iVBORw0KGgo...",
147+
"type": "binary"
148+
},
149+
{
150+
"name": "www/custom.css",
151+
"content": ".custom-class { ... }",
152+
"type": "text"
91153
}
92154
]
93155
```
94156

95-
3. **Runtime Preparation**:
157+
3. **Runtime Preparation**: The converted application is integrated into a
158+
viewer and/or editor alongside of the underlying language distribution.
96159
- For R: WebR environment is configured
97160
- For Python: Pyodide environment is set up
98161

99162
### Step 3: Browser Deployment
100163

101-
The converted application now runs entirely in the browser:
164+
From here, the Shinylive application can be run locally through a Live Server
165+
or it can be placed on any static web server host that supports HTTPS URLs.
166+
Some common web server hosts for this kind of application are GitHub Pages or Netlify.
167+
168+
Feel free to consult the following GitHub Pages Deployment tutorials:
169+
170+
1. [Creating an R Shinylive App inside a Quarto Document](https://github.com/coatless-quarto/r-shinylive-demo)
171+
2. [Deploying an **R** Shinylive App via GitHub Pages through GitHub Actions](https://github.com/coatless-tutorials/convert-shiny-app-r-shinylive)
172+
3. [Deploying a **Python** Shinylive App via GitHub Pages through GitHub Actions](https://github.com/coatless-tutorials/convert-py-shiny-app-to-py-shinylive-app)
173+
174+
With this being said, the converted application is now running entirely in the browser!
175+
Specifically, we have:
102176

103177
1. WebR/Pyodide runtime initializes
104178
2. Application files are loaded from `app.json`
105179
3. UI renders and connects to a local runtime
106-
4. All computation happens client-side
180+
4. All computation in app happens client-side in their web browser
107181

108182
## Source Code Transparency
109183

110-
Unlike traditional Shiny applications, Shinylive apps are inherently transparent. This transparency is a fundamental characteristic due to several factors:
184+
Unlike traditional Shiny applications, Shinylive apps are inherently transparent.
185+
This transparency is a fundamental characteristic due to several factors:
111186

112187
1. **Client-Side Execution**: All code must be available in the browser
113188
2. **Bundled Resources**: All files are packaged in accessible formats
114189
3. **No Server Privacy**: No server-side code protection exists
115190

191+
116192
## Extracting Source Code with `peeky`
117193

118194
The `peeky` package leverages this transparency to extract source code from
119-
Shinylive applications. Here's how it works:
120-
121-
### Detection
122-
123-
TODO: Clarify
124-
125-
```r
126-
# URL of the Shinylive application
127-
url <- ""
128-
129-
# Auto-detect and handle both standalone and Quarto-embedded apps
130-
peeky::peek_shinylive_app(url)
131-
```
132-
133-
### Content Retrieval
134-
135-
TODO: Clarify
195+
Shinylive applications through the following functions:
136196

137-
1. Downloads the webpage or app.json
138-
2. Identifies Shinylive components
139-
3. Extracts embedded code and resources
197+
- `peeky::peek_shinylive_app()`: Automatically retrieve either a standalone or
198+
Quarto embedded Shinylive application.
199+
- `peeky::peek_standalone_shinylive_app()`: Retrieve a standalone Shinylive
200+
application.
201+
- `peeky::peek_quarto_shinylive_app()`: Retrieve embedded Shinylive applications
202+
in Quarto documents.
140203

141204
### File Reconstruction
142205

143206
By default, extracted content is reconstructed into runnable applications that
144207
have the directory structure of a standalone app with all of its components.
208+
209+
```
210+
extracted_app/
211+
├── app.R # Main application code
212+
├── data/
213+
│ └── dataset.csv # Data files
214+
├── www/
215+
│ ├── image.png # Static assets
216+
│ └── custom.css
217+
└── shinylive_metadata.json # Configuration and metadata (Quarto-apps only)
218+
```
219+
145220
If the application is embedded in a Quarto document, the extracted content can
146221
consist of one or more Shiny applications and, thus, is reconstructed into a
147222
into subdirectories, e.g. `app_1`, `app_2`, etc. Or, the content can be
@@ -158,33 +233,41 @@ peeky::peek_quarto_shinylive_app(url, output_format = "app-dir")
158233
peeky::peek_quarto_shinylive_app(url, output_format = "quarto")
159234
```
160235

161-
## Security Implications
162236

163-
This transparency has important implications for developers:
237+
### Security Implications
238+
239+
As a result of the ability to fetch and reconstruct the application locally,
240+
there are several important implications for developers:
164241

165-
1. **No Code Privacy**: All source code is accessible to users
242+
1. **No Code Privacy**: All source code is accessible to users.
166243
2. **Data Visibility**: Bundled datasets are accessible
167244
3. **API Keys**: Never include sensitive credentials
168245
4. **Business Logic**: Proprietary algorithms are visible
169246

170-
## Best Practices
247+
## Best Practices for Deploying
171248

172-
When developing Shinylive applications:
249+
As Shinylive is a new technology, the best practices are a work in progress.
250+
We make the following suggestions
251+
When moving to deploy a Shinylive applications,
173252

174253
1. **Assume Transparency**: Design with the understanding that code is visible
175-
2. **Handle Sensitive Data**: Process sensitive data server-side if needed
176-
3. **License Clearly**: Include clear licensing terms
177-
4. **Consider Hybrid Approaches**: Use traditional Shiny for sensitive components
178-
and offload non-sensitive parts to Shinylive.
254+
2. **Avoid Handling Sensitive Data**: Do not use Shinylive with any
255+
Personal Identifiable Information (PII) data.
256+
3. **License Clearly**: Include explicit licensing terms as comments at the top
257+
of the application.
258+
259+
If you must use sensitive data with Shinylive, consider using a **Hybrid Approach**
260+
to deployment. In particular, use traditional Shiny server for retrieving and
261+
releasing sensitive components and offload non-sensitive parts to Shinylive.
179262

180263
## Conclusion
181264

182265
The transformation from traditional Shiny to Shinylive represents a fundamental
183-
shift in how web applications are delivered. While this brings advantages in
266+
shift in how data web applications are delivered. While this brings advantages in
184267
deployment and accessibility, it also requires developers to embrace
185268
transparency and adjust their development practices accordingly. Tools like
186269
`peeky` help demonstrate this transparency and can assist in understanding
187-
how Shinylive applications work.
270+
how deployed Shinylive applications work.
188271

189272
## References
190273

@@ -207,3 +290,5 @@ how Shinylive applications work.
207290
[wasm]: https://webassembly.org/
208291
[peekygh]: https://github.com/coatless-rpkg/peeky
209292
[protoslr]: https://github.com/georgestagg/shiny-standalone-webr-demo
293+
[pyshinylive]: https://pypi.org/project/shinylive/
294+
[rshinylive]: https://cran.r-project.org/package=shinylive

0 commit comments

Comments
 (0)