Skip to content

Commit ef05c0b

Browse files
committed
add to examples.Rmd
1 parent e070ece commit ef05c0b

3 files changed

Lines changed: 349 additions & 5 deletions

File tree

R/background.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
#' edges,
4040
#' background(
4141
#' id = "1",
42-
#' color = "#f1f1f1",
42+
#' color = "black",
4343
#' variant = "lines",
4444
#' gap = 10
4545
#' ),
4646
#' background(
4747
#' id = "2",
48-
#' color = "#cccccc",
48+
#' color = "darkgray",
4949
#' variant = "lines"
5050
#' )
5151
#' )

man/background.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/examples.Rmd

Lines changed: 345 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,357 @@ knitr::opts_chunk$set(
1414
)
1515
```
1616

17-
```{css}
17+
18+
```{r}
19+
library(reactflow)
20+
```
21+
22+
23+
# Basic Usage
24+
25+
To create a basic flow using `reactflow`, you need to define nodes and edges and call the `reactflow` function with these arguments.
26+
27+
Note to label a node, you provide a `label` argument in the `data` list, whereas the `label` is a direct argument to an edge.
28+
29+
```{r}
30+
nodes <- list(
31+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1")),
32+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
33+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"))
34+
)
35+
36+
edges <- list(
37+
list(id = "e1-2", source = "1", target = "2", label = "Edge 1"),
38+
list(id = "e2-3", source = "2", target = "3"),
39+
list(id = "e1-3", source = "1", target = "3")
40+
)
41+
reactflow(
42+
nodes,
43+
edges
44+
)
45+
```
46+
47+
You can change the width of the output widget directly in the call to [`reactflow()`]:
48+
49+
```{r}
50+
reactflow(
51+
nodes,
52+
edges,
53+
width = "100%"
54+
)
55+
```
56+
57+
By default, you can use connect new edges by dragging from one node to another.
58+
To disable this, you can set
59+
60+
```{r}
61+
reactflow(
62+
nodes,
63+
edges,
64+
allow_edge_connection = FALSE,
65+
width = "100%"
66+
)
67+
```
68+
69+
70+
71+
## UI Elements
72+
73+
The `reactflow` package provides some easy to use UI elements to customize the usage and display of a flow.
74+
Namely [`background()`], [`controls()`], [`mini_map()`],
75+
76+
```{r}
77+
reactflow(
78+
nodes,
79+
edges,
80+
background = background(),
81+
controls = controls(),
82+
mini_map = mini_map(),
83+
width = "100%"
84+
)
85+
```
86+
87+
## Background
88+
89+
All arguments passed to `background()` are passed to the underlying `react-flow` component.
90+
The official `background` documentation can be found here: <https://reactflow.dev/api-reference/components/background>.
91+
92+
```{r}
93+
94+
nodes <- list(
95+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1")),
96+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
97+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"))
98+
)
99+
100+
edges <- list(
101+
list(id = "e1-2", source = "1", target = "2", label = "Edge 1"),
102+
list(id = "e2-3", source = "2", target = "3"),
103+
list(id = "e1-3", source = "1", target = "3")
104+
)
105+
106+
reactflow(
107+
nodes,
108+
edges,
109+
background = background()
110+
)
111+
```
112+
113+
An example using the extra arguments can be found below
114+
115+
```{r}
116+
reactflow(
117+
nodes,
118+
edges,
119+
background = background(
120+
color = "beige",
121+
variant = "lines",
122+
gap = 50,
123+
lineWidth = 2
124+
),
125+
width = "100%"
126+
)
127+
```
128+
129+
### Multiple Backgrounds
130+
131+
You can also add multiple backgrounds to the flow by passing multiple `background()` elements.
132+
133+
```{R}
134+
reactflow(
135+
nodes,
136+
edges,
137+
background(
138+
id = 1,
139+
color = "beige",
140+
variant = "lines",
141+
gap = 40,
142+
lineWidth = 3
143+
),
144+
background(
145+
id = 2,
146+
size = 4,
147+
offset = 3,
148+
color = "lightgray",
149+
variant = "dots"
150+
),
151+
width = "100%"
152+
)
153+
```
154+
155+
## Controls
156+
157+
All arguments passed to `controls()` are passed to the underlying `react-flow` component.
158+
The official `controls` documentation can be found here: <https://reactflow.dev/api-reference/components/controls>.
159+
160+
161+
```{r}
162+
nodes <- list(
163+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1")),
164+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
165+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"))
166+
)
167+
168+
edges <- list(
169+
list(id = "e1-2", source = "1", target = "2", label = "Edge 1"),
170+
list(id = "e2-3", source = "2", target = "3"),
171+
list(id = "e1-3", source = "1", target = "3")
172+
)
173+
174+
reactflow(
175+
nodes,
176+
edges,
177+
controls = controls(),
178+
width = "100%"
179+
)
180+
```
181+
182+
An example using the extra arguments can be found below
183+
184+
```{r}
185+
reactflow(
186+
nodes,
187+
edges,
188+
controls = controls(
189+
position = "top-right",
190+
orientation = "horizontal",
191+
showZoom = TRUE,
192+
showFitView = FALSE,
193+
showIntereactive = TRUE
194+
),
195+
width = "100%"
196+
)
197+
```
198+
199+
200+
## Mini Map
201+
202+
All arguments passed to `mini_map()` are passed to the underlying `react-flow` component.
203+
The official `miniMap` documentation can be found here: <https://reactflow.dev/api-reference/components/minimap>.
204+
205+
```{r}
206+
nodes <- list(
207+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1")),
208+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
209+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"))
210+
)
211+
212+
edges <- list(
213+
list(id = "e1-2", source = "1", target = "2", label = "Edge 1"),
214+
list(id = "e2-3", source = "2", target = "3"),
215+
list(id = "e1-3", source = "1", target = "3")
216+
)
217+
218+
reactflow(
219+
nodes,
220+
edges,
221+
mini_map = mini_map(pannable = TRUE, zoomable = TRUE),
222+
width = "100%"
223+
)
224+
```
225+
226+
227+
# Styling
228+
229+
Each component can be styled individually or using CSS.
230+
Eg setting the following CSS class, will make all nodes green.
231+
232+
```css
18233
.react-flow__node {
19234
background-color: green !important;
20235
}
21236
```
22237

23238

239+
See also `app.R` TODO
240+
241+
242+
## Nodes
243+
244+
### Using `className` and custom CSS
245+
246+
Using `className`s, we can assign custom CSS to the nodes in the mini-map.
247+
248+
```{css}
249+
.startNode {
250+
background-color: lightgreen;
251+
}
252+
.endNode {
253+
background-color: coral;
254+
}
255+
```
256+
257+
258+
```{r}
259+
nodes2 <- list(
260+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1"),
261+
className = "startNode"),
262+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
263+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"),
264+
className = "endNode")
265+
)
266+
reactflow(
267+
nodes = nodes2,
268+
edges = edges,
269+
mini_map = mini_map(),
270+
color_minimap = TRUE,
271+
width = "100%"
272+
)
273+
```
274+
275+
276+
277+
278+
## Edges
279+
280+
281+
## Mini Map
282+
283+
The official `miniMap` documentation can be found here: <https://reactflow.dev/api-reference/components/minimap>.
284+
For example, the `nodeColor` argument can be used to change the color of the nodes in the mini map.
285+
286+
```{r}
287+
reactflow(
288+
nodes,
289+
edges,
290+
mini_map = mini_map(
291+
pannable = TRUE,
292+
zoomable = TRUE,
293+
nodeColor = "coral",
294+
position = "top-right",
295+
maskColor = "lightgray"
296+
),
297+
width = "100%"
298+
)
299+
```
300+
301+
Using `className`s, we can assign custom CSS to the nodes in the mini-map.
302+
303+
```{css}
304+
.startNode {
305+
background-color: lightgreen;
306+
}
307+
.endNode {
308+
background-color: coral;
309+
}
310+
```
311+
312+
313+
```{r}
314+
nodes2 <- list(
315+
list(id = "1", position = list(x = 0, y = 0), data = list(label = "Node 1"),
316+
type = "input"),
317+
list(id = "2", position = list(x = 100, y = 100), data = list(label = "Node 2")),
318+
list(id = "3", position = list(x = 0, y = 200), data = list(label = "Node 3"),
319+
type = "output")
320+
)
321+
reactflow(
322+
nodes = nodes2,
323+
edges = edges,
324+
mini_map = mini_map(
325+
nodeColor = JS("
326+
function nodeColor(node) {
327+
switch (node.type) {
328+
case 'input':
329+
return '#6ede87';
330+
case 'output':
331+
return '#6865A5';
332+
default:
333+
return '#ff0072';
334+
}
335+
}")
336+
),
337+
color_minimap = TRUE,
338+
width = "100%"
339+
)
340+
```
341+
TODO make minimap colorful
342+
343+
344+
345+
346+
## Background
347+
348+
349+
350+
## Controls
351+
352+
There is very little styling that can be applied to [`controls()`] directly.
353+
However, we can apply styling using the CSS class `react-flow__controls`.
354+
355+
356+
# Layouting
357+
358+
## Automatic Layout
359+
360+
361+
# Interactivity with Shiny
362+
363+
364+
365+
366+
367+
24368
```{r setup}
25369
library(reactflow)
26370

0 commit comments

Comments
 (0)