Site icon R-bloggers

Use an R Session as a Websocket Server

[This article was first published on RLang.io | R Language Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is VERY simple R function that when ran turns your R session into a websocket server. For this example it returns the standard deviation from a JSON encoded array sent via websockets.

This method can be used to do all sorts of things, include return information for the R workspace environment and even dynamically run code through the use of eval() on your global environment (not recommended for security reasons). If you want to respond with the workspace environment information simply swap out ws$send() with the following…

ws$send(toJSON(ls(envir = .GlobalEnv)))

Hope this is useful for someone. I will be putting together a much more involved tutorial sometime down the road that will cover dashboards outside the use of Shiny.

To leave a comment for the author, please follow the link and comment on their blog: RLang.io | R Language Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.