V8 version 0.4: console.log and exception handling

[This article was first published on OpenCPU, 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.

opencpu logo

V8 version 0.4 has appeared on CRAN. This version introduces several new console functions (console.log, console.warn, console.error) and two vignettes:

I will talk more about using NPM in another blog post this week.

JavaScript Exceptions

Starting V8 version 0.4 each context has a console object in the global namespace:

Object.keys(console)
log,warn,error

The console.log, console.warn and console.error functions can be used to generate stdout, warnings or errors in R from JavaScript. This allows for writing embedded JavaScript functions that propagate exceptions back to R, similar as we would do for other foreign language interfaces such as C or C++:

library(V8)
ct <- new_context()
ct$eval('console.log("Bla bla")')
# Bla bla
ct$eval('console.warn("Heads up!")')
# Warning: Heads up!
ct$eval('console.error("Oh noes!")')
# Error: Oh noes!

For example you can use this to verify that external resources were loaded:

ct$source("https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.min.js")
ct$eval('var cf = crossfilter || console.error("failed to load crossfilter!")')

Of course, in R you could use tryCatch or whatever you like to catch exceptions that were raised this way in your JavaScript code.

Interactive Console

The interactive console has been enhanced a bit as well. It no longer prints redundant “undefined” returns:

library(V8)
ct <- new_context()
ct$console()
# This is V8 version 3.14.5.10. Press ESC or CTRL+C to exit.

From here we can try our new functions:

console.log("Bla bla")
console.warn("Heads up!")
console.error("Oh noes!")

Bindings to JavaScript Libraries

V8 provides a JavaScript call interface, data interchange, exception handling and interactive debugging console. This is everything we need to embed JavaScript code and libraries in R.

If you are curious how this would work, I have started working on a new R package implementing bindings to some of the very best libraries available for working with JavaScript and HTML. I hope this package will make it’s way to CRAN soon, but until then it is available from github

library(devtools)
install_github("jeroenooms/js")

Some silly example illustrating jshint:

library(js)
code = "var foo = 123nvar bar = 456nfoo + bar"
cat(code)
# var foo = 123
# var bar = 456
# foo + bar

jshint(code)[c("line", "reason")]
#  line                                                                 reason
#     1                                                     Missing semicolon.
#     2                                                     Missing semicolon.
#     3 Expected an assignment or function call and instead saw an expression.
#     3                                                     Missing semicolon.

Or the brilliant uglify-js:

uglify_reformat(code)
# [1] "var foo=123;var bar=456;foo+bar;"
uglify_optimize(code)
# Warning: Dropping side-effect-free statement [null:3,0]
# [1] "var foo=123,bar=456;"

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)