2 |
3 |

4 |
5 | Small framework to communicate between Shiny client and server via HTTP requests.
6 | Run `communicate::example()` for a short demo.
7 |
8 | [Site](https://communicate.opifex.org/)
9 |
10 |
11 |
12 | ## Installation
13 |
14 | ```r
15 | # install.packages("remotes")
16 | remotes::install_github("devOpifex/communicate")
17 | ```
18 |
19 | ## How it works
20 |
21 | Create a shiny application and "commincation channels."
22 | Add callback functions with `com`.
23 |
24 | ```r
25 | library(shiny)
26 | library(communicate)
27 |
28 | add <- \(x){
29 | x + 2
30 | }
31 |
32 | ui <- fluidPage(
33 | h1("Hello")
34 | )
35 |
36 | server <- \(input, output, session){
37 | com("add", add)
38 | }
39 |
40 | shinyApp(ui, server)
41 | ```
42 |
43 | Then use the JavaScript library to communicate.
44 |
45 | ```r
46 | library(shiny)
47 | library(communicate)
48 |
49 | add <- \(x){
50 | x + 2
51 | }
52 |
53 | script <- "
54 | $('#btn').on('click', () => {
55 | communicate.com('add', {x: 1})
56 | .then(res => alert(`equals: ${res}`));
57 | })
58 | "
59 |
60 | ui <- fluidPage(
61 | # import dependencies
62 | useCommunicate(),
63 | h1("Hello"),
64 | tags$a("Communicate", id = "btn"),
65 | tags$script(HTML(script))
66 | )
67 |
68 | server <- \(input, output, session){
69 | com("add", add)
70 | }
71 |
72 | shinyApp(ui, server)
73 | ```
74 |
75 | ### Types
76 |
77 | Though optional it is recommended to specify the types of the arguments
78 | of your callback function. This enables type conversion and type check when communicating from the
79 | client.
80 |
81 | Existing types:
82 |
83 | - `Character`
84 | - `Numeric`
85 | - `Integer`
86 | - `Date`
87 | - `Dataframe`
88 | - `Posixct`
89 | - `Posixlt`
90 | - `Character`
91 | - `List`
92 | - `Function`
93 |
94 | ```r
95 | library(shiny)
96 | library(communicate)
97 |
98 | add <- \(x){
99 | x + 2
100 | }
101 |
102 | script <- "
103 | $('#btn').on('click', () => {
104 | communicate.com('add', {x: 1})
105 | .then(res => alert(`equals: ${res}`));
106 | })
107 | "
108 |
109 | ui <- fluidPage(
110 | # import dependencies
111 | useCommunicate(),
112 | h1("Hello"),
113 | tags$a("Communicate", id = "btn"),
114 | tags$script(HTML(script))
115 | )
116 |
117 | server <- \(input, output, session){
118 | com("add", add)(x = Integer)
119 | }
120 |
121 | shinyApp(ui, server)
122 | ```
123 |
124 | ### Defaults
125 |
126 | You can also specifiy callback functions' argument defaults as done below.
127 |
128 | ```r
129 | library(shiny)
130 | library(communicate)
131 |
132 | add <- \(x, y){
133 | x + y
134 | }
135 |
136 | script <- "
137 | $('#btn').on('click', () => {
138 | communicate.com('add', {x: 1})
139 | .then(res => alert(`equals: ${res}`));
140 | })
141 | "
142 |
143 | ui <- fluidPage(
144 | # import dependencies
145 | useCommunicate(),
146 | h1("Hello"),
147 | tags$a("Communicate", id = "btn"),
148 | tags$script(HTML(script))
149 | )
150 |
151 | server <- \(input, output, session){
152 | com("add", add)(x = Integer, y = Numeric)(y = 1.1)
153 | }
154 |
155 | shinyApp(ui, server)
156 | ```
157 |
158 | ## JavaScript
159 |
160 | Accessible from `communicate`, functions:
161 |
162 | - `com` - communicate.
163 | - `hasCom` - check if communication channel registered.
164 | - `getCom` - get communication channel and its arguments.
165 | - `getComs` - get all communication channels registered.
166 |
167 | ## Dependency
168 |
169 | You import the dependency with `useCommunicate()`.
170 | Alternatively you can install the npm package,
171 | e.g.: if you use [packer](https://packer.john-coene.com/).
172 |
173 | ```bash
174 | npm install @devopifex/communicate
175 | ```
176 |
177 | Or with packer:
178 |
179 | ```r
180 | packer::npm_install("@devopifex/communicate")
181 | ```
182 |
183 | ### Usage
184 |
185 | ```js
186 | communicate.hasCom("add")
187 |
188 | communicate.getCom("add")
189 |
190 | communicate.com("add", {x: 1, y: 2})
191 | .then(data => console.log(data))
192 | .catch(error => console.error(error))
193 | ```
194 |
195 |
--------------------------------------------------------------------------------
/docs/.nojekyll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devOpifex/communicate/de7f1a21ae99d2608385614f4af40c37426ec03a/docs/.nojekyll
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # Communicate
2 |
3 | Small framework to communicate between shiny client and server via HTTP requests.
4 |
5 | If you want an introduction to using such communication in shiny see the video
6 | below.
7 | This framework attempts to make this more robust and easier to setup.
8 |
9 |