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.
The goal of {bubble}
is to launch and interact with a NodeJS session, from R.
You can install the dev version of {bubble}
with:
remotes::install_github("colinfay/bubble")
Note that you'll need to have NodeJS installed on your machine
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
library(bubble) find_node()
The NodeSession$new()
function returns an object that can be used to interact with the launched Node session.
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()
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")
Sys.sleep(5)
node_repl()
This REPL has been inspired by the one from
{reticulate}
: https://rstudio.github.io/reticulate/
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()
{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
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")
Please note that the 'bubble' project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.