我可以从R jupyter笔记本中调用自定义javascript吗 [英] Can I call custom javascript from an R jupyter notebook
问题描述
我有一个使用R编程语言的Jupyter Notebook
.我想在此R笔记本中调用javascript functions
.
I have a Jupyter Notebook
which is using the R programming language. I would like to call javascript functions
within this R notebook.
我知道有一种方法可以执行此操作,因为可以从R调用基于javascript的库,但是我找不到包装javascript函数的任何示例,因此R可以使用它.
I know there is a way to do this, because there are javascript based libraries that you can call from R, but I cannot find any examples of wrapping a javascript function, so it can be used by R.
即使是将javascript分配给R variable
然后调用该R变量的示例,也会很有帮助.
Even an example of assigning javascript to a R variable
and then calling that R variable would be helpful.
推荐答案
js::js_eval()
可以在R中评估JavaScript字符串.来自?js::js_eval
:
js::js_eval()
can evaluate a string of JavaScript within R. From ?js::js_eval
:
# Stateless evaluation
js_eval("(function() {return 'foo'})()")
要执行更复杂的JavaScript操作,请检出V8
,它可以使一个JavaScript实例保留多行.来自?V8::v8
:
For more complicated JavaScript operations, check out V8
, which lets you keep a JavaScript instance for more than one line. From ?V8::v8
:
# Create a new context
ctx <- v8();
# Evaluate some code
ctx$eval("var foo = 123")
ctx$eval("var bar = 456")
ctx$eval("foo+bar")
最终,除了小小的黑客攻击之外,其他任何事情都将使它感到非常沮丧,但是它确实可以工作.如果您很聪明,您可能会采购整个脚本,但是除非有绝对可以在JavaScript中完成的工作,否则我不确定这样做是否值得.骇客骇客!
Ultimately it's going to get really frustrating for anything beyond little hacks, but it does work. You could likely source a whole script if you're clever, but I'm not sure it's worth it unless there's something that can absolutely only be done in JavaScript. Happy hacking!
这篇关于我可以从R jupyter笔记本中调用自定义javascript吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!