Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devOpifex/communicate
💬 Communicate between client and server in Shiny via HTTP
https://github.com/devOpifex/communicate
client-server http javascript rstats
Last synced: 3 months ago
JSON representation
💬 Communicate between client and server in Shiny via HTTP
- Host: GitHub
- URL: https://github.com/devOpifex/communicate
- Owner: devOpifex
- License: gpl-3.0
- Created: 2023-10-28T00:29:22.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-05-29T12:11:05.000Z (5 months ago)
- Last Synced: 2024-05-30T01:24:05.329Z (5 months ago)
- Topics: client-server, http, javascript, rstats
- Language: R
- Homepage: https://communicate.opifex.org
- Size: 347 KB
- Stars: 19
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-shiny-extensions - communicate - Small framework to communicate between Shiny client and server via HTTP requests. (Backend / API Frameworks)
- jimsghstars - devOpifex/communicate - 💬 Communicate between client and server in Shiny via HTTP (R)
README
Small framework to communicate between Shiny client and server via HTTP requests.
Run `communicate::example()` for a short demo.[Site](https://communicate.opifex.org/)
## Installation
```r
# install.packages("remotes")
remotes::install_github("devOpifex/communicate")
```## How it works
Create a shiny application and "commincation channels."
Add callback functions with `com`.```r
library(shiny)
library(communicate)add <- \(x){
x + 2
}ui <- fluidPage(
h1("Hello")
)server <- \(input, output, session){
com("add", add)
}shinyApp(ui, server)
```Then use the JavaScript library to communicate.
```r
library(shiny)
library(communicate)add <- \(x){
x + 2
}script <- "
$('#btn').on('click', () => {
communicate.com('add', {x: 1})
.then(res => alert(`equals: ${res}`));
})
"ui <- fluidPage(
# import dependencies
useCommunicate(),
h1("Hello"),
tags$a("Communicate", id = "btn"),
tags$script(HTML(script))
)server <- \(input, output, session){
com("add", add)
}shinyApp(ui, server)
```### Types
Though optional it is recommended to specify the types of the arguments
of your callback function. This enables type conversion and type check when communicating from the
client.Existing types:
- `Character`
- `Numeric`
- `Integer`
- `Date`
- `Dataframe`
- `Posixct`
- `Posixlt`
- `Character`
- `List`
- `Function````r
library(shiny)
library(communicate)add <- \(x){
x + 2
}script <- "
$('#btn').on('click', () => {
communicate.com('add', {x: 1})
.then(res => alert(`equals: ${res}`));
})
"ui <- fluidPage(
# import dependencies
useCommunicate(),
h1("Hello"),
tags$a("Communicate", id = "btn"),
tags$script(HTML(script))
)server <- \(input, output, session){
com("add", add)(x = Integer)
}shinyApp(ui, server)
```### Defaults
You can also specifiy callback functions' argument defaults as done below.
```r
library(shiny)
library(communicate)add <- \(x, y){
x + y
}script <- "
$('#btn').on('click', () => {
communicate.com('add', {x: 1})
.then(res => alert(`equals: ${res}`));
})
"ui <- fluidPage(
# import dependencies
useCommunicate(),
h1("Hello"),
tags$a("Communicate", id = "btn"),
tags$script(HTML(script))
)server <- \(input, output, session){
com("add", add)(x = Integer, y = Numeric)(y = 1.1)
}shinyApp(ui, server)
```## JavaScript
Accessible from `communicate`, functions:
- `com` - communicate.
- `hasCom` - check if communication channel registered.
- `getCom` - get communication channel and its arguments.
- `getComs` - get all communication channels registered.## Dependency
You import the dependency with `useCommunicate()`.
Alternatively you can install the npm package,
e.g.: if you use [packer](https://packer.john-coene.com/).```bash
npm install @devopifex/communicate
```Or with packer:
```r
packer::npm_install("@devopifex/communicate")
```### Usage
```js
communicate.hasCom("add")communicate.getCom("add")
communicate.com("add", {x: 1, y: 2})
.then(data => console.log(data))
.catch(error => console.error(error))
```