js package is a tool for using JavaScript in R, which implements bindings to several popular JavaScript libraries and is used to compile, verify, reformat, optimize and analyze JavaScript code. Built on the basis of the V8 package, these libraries can be called
#js_typeof(),如果代码无效将引发SyntaxError,用于验证单个函数或对象 callback<-'function test(x, y){var z = x*y ;return z;}' js_typeof(callback) #'function' js_typeof('function(x,y){return x + y}') #'function' conf<-'{foo : function(){},bar : 123}' js_typeof(conf) #'object' #JavaScript程序验证:由JavaScript语句集合组成,用js_validate_script()函数 jscode<-readLines(system.file("js/uglify.min.js",package="js"),warn=FALSE) js_validate_script(jscode) #TRUE js_validate_script('function(x, y){return x + y}', error = FALSE) #FALSE不允许在全局范围内定义匿名函数
esprima_tokenize(text,range=FALSE,loc=FALSE,comment=FALSE) #返回data.frame text:JavaScript代码的字符向量,range:以0为基准注释每个标签的起始位置+结束位1:300,loc:注释每个标签起始行+起始列+终止行+终止列的位置,numeric类型 esprima_parse(text,jsx = FALSE,range = FALSE,loc = FALSE,tolerant = FALSE,tokens = FALSE,comment = FALSE) #返回'JS_EVAL'类型,解析成树形结构
cat(coffee_compile("square = (x) -> x * x")) cat(coffee_compile("square = (x) -> x * x",bare=T)) demo<-readLines(system.file("example/demo.coffee", package = "js")) cat(demo, sep = "\n") js<-coffee_compile(demo) #输出js脚本 cat(js) uglify_optimize(js) #压缩js,将代码重写为更紧凑但等效的程序
code <- "function test(x, y){x = x || 1; y = y || 1; return x*y;}" cat(uglify_reformat(code, beautify = TRUE, indent_level = 2))
code <- "var foo = 123" jshint(code)
Related articles:
r language-Where to find teaching courses on R language, MySQL and Hadoop
How to call R language in PHP Function, like calling a function in C language
The above is the detailed content of Detailed explanation of using JavaScript in R language. For more information, please follow other related articles on the PHP Chinese website!