An open API service indexing awesome lists of open source software.

https://github.com/ColinFay/bubble

Launch and interact with a NodeJS session from R
https://github.com/ColinFay/bubble

Last synced: about 1 month ago
JSON representation

Launch and interact with a NodeJS session from R

Awesome Lists containing this project

README

        

---
output:
github_document:
html_preview: false
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

DISCLAIMER: THIS IS A WORK IN PROGRESS, DO NOT USE UNLESS FOR TESTING / TOYING WITH THE PACKAGE.

# bubble

The goal of `{bubble}` is to launch and interact with a NodeJS session, from R.

## Installation

You can install the dev version of `{bubble}` with:

``` r
remotes::install_github("colinfay/bubble")
```

## Example

> Note that you'll need to have NodeJS installed on your machine

### Using bubble to launch and communicate with a NodeJS session

Launch a new NodeJS session with `NodeSession$new()`.

This function uses the path to your NodeJS binary. On MacOS, it can be found with `Sys.which("node")`, on Debian `Sys.which("nodejs")` (if installed through `apt-get install nodejs`).

`NodeSession$new()` tries to guess where NodeJS is by looking at both these `Sys`. If they are both empty, you'll get an error, and need to provide the path manually. The function to find Node is exported and called `find_node()`. It will either return the path to your local installation of Node or an error

```{r}
library(bubble)
find_node()
```

The `NodeSession$new()` function returns an object that can be used to interact with the launched Node session.

```{r example}
n <- NodeSession$new()
n$eval("var x = 12")
n$eval("var y = 17")
n$eval("x + ")
n$eval("y")
# Return the x value
x <- n$get(x)
class(x)
# Create a variable
n$assign(vehicles, cars[1:5,])
n$get(vehicles)
n$state()
n$kill()
n$state()
```

### Using {bubble} to launch an express app

```{r error = TRUE}
n <- NodeSession$new()

n$eval("const express = require('express');")
n$eval("app = express()", print = FALSE)

n$eval("app.get('/', function (req, res) {", print = FALSE)
n$eval(" res.send('Hello R!')")
n$eval("})", print = FALSE)
n$eval("app.listen(3002)", print = FALSE)

x <- httr::GET("http://127.0.0.1:3002")
httr::content(x)
n$kill()
httr::GET("http://127.0.0.1:3002")
```

```{r include = FALSE}
Sys.sleep(5)
```

### Using {bubble} to launch a NodeJS terminal

```{r eval = FALSE}
node_repl()
```

![](readme-fig/node_repl.gif)

> This REPL has been inspired by the one from `{reticulate}` : https://rstudio.github.io/reticulate/

### Using {bubble} to launch a NodeJS script

```{r}
n <- NodeSession$new(
bin = "/usr/local/bin/node",
params = "inst/launch.js"
)
x <- httr::GET("http://127.0.0.1:3000")
httr::content(x)
n$terminate()
```

### Knitr

`{bubble}` comes with a knitr engine that can be set with `bubble::set_node_engine()` at the top of your markdown.

Then, each chunck `{node}` will be evaluated in a Node session.

You'll find an example in [inst/rmdexample.Rmd](inst/rmdexample.Rmd)

### Using {bubble} with npm

```{r, eval=FALSE}
library(bubble)

# install kmeans
npm <- Npm$new()
npm$install("ml-kmeans")

n <- NodeSession$new()
n$eval("const kmeans = require('ml-kmeans');")
data <- dplyr::select_if(iris, is.numeric)
data <- unname(data)
n$assign(iris, data)
n$eval("var ans = kmeans(iris, 4);")
n$get(ans) # get clusters
n$terminate()

npm$uninstall("ml-kmeans")
```

## CoC

Please note that the 'bubble' project is released with a
[Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.