-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include-file.qmd
169 lines (128 loc) · 3.06 KB
/
include-file.qmd
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: "Include Data By Embedding File Contents Inside Code Cell"
format:
html:
resources:
- shinylive-sw.js
filters:
- shinylive
---
This R Shinylive app example demonstrates the process of embedding data directly into the application using the `## file:` directive.
## Example App
The following R Shinylive app is a demo of a Shiny app reading data from an embedded file (`fruit.csv`) in a code cell. To modify the data, navigate to the `fruit.csv` editor tab located to the right of `app.R` in the editor view.
:::callout-note
We've enabled the editor view to emphasize that the data is embedded within the app. Typically, R Shinylive applications only include the app running under the viewer view.
:::
```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 300
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
verbatimTextOutput(outputId = "dataVariables")
)
server <- function(input, output) {
output$dataVariables <- renderPrint({
ds <- read.csv("fruit.csv")
ds
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
## file: fruit.csv
id,name,count
1,"apple",20
2,"orange",12
3,"grape",100
```
## The `file` Directive
Inside a `{shinylive-r}` code cell, you can embed a text file using:
```{{shinylive-r}}
## file: my-file.txt
contents here
```
To include data read by the app through a CSV, use:
```{{shinylive-r}}
## file: fruit.csv
id,name,count
1,"apple",20
2,"orange",12
3,"grape",100
```
:::callout-important
R Shinylive apps currently do not support loading binary data. All file directives are considered to have the `type` declaration of `text`. For example:
```{{shinylive-r}}
## file: fruit.csv
```
will be treated as:
```{{shinylive-r}}
## file: fruit.csv
## type: text
```
:::
## Including Shiny App
In the code cell, place all Shiny app code under the `## file: app.R` directive, like this:
```{{shinylive-r}}
#| standalone: true
## file: app.R
library(shiny)
ui <- fluidPage(
# Shiny app UI source
)
server <- function(input, output) {
# Shiny app server source
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
```
Once done, include the data embedding in the same cell:
```{{shinylive-r}}
#| standalone: true
## file: app.R
# Shiny app code from above
## file: fruit.csv
id,name,count
1,"apple",20
```
## Source of App
View the full source of the app here:
````md
---
title: "Embedded Data"
format:
html:
resources:
- shinylive-sw.js
filters:
- shinylive
---
This is an R Shinylive code cell with embedded data!
```{shinylive-r}
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 300
## file: app.R
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
verbatimTextOutput(outputId = "dataVariables")
)
server <- function(input, output) {
output$dataVariables <- renderPrint({
ds <- read.csv("fruit.csv")
ds
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
## file: fruit.csv
id,name,count
1,"apple",20
2,"orange",12
3,"grape",100
```
````