Home  >  Article  >  Web Front-end  >  Detailed explanation of using JavaScript in R language

Detailed explanation of using JavaScript in R language

php是最好的语言
php是最好的语言Original
2018-08-04 09:16:145475browse

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

1. Verify that the JavaScript code is syntactically correct in R

#js_typeof(),如果代码无效将引发SyntaxError,用于验证单个函数或对象
callback<-&#39;function test(x, y){var z = x*y ;return z;}&#39;
js_typeof(callback)  #&#39;function&#39;
js_typeof(&#39;function(x,y){return x + y}&#39;) #&#39;function&#39;
conf<-&#39;{foo : function(){},bar : 123}&#39;
js_typeof(conf) #&#39;object&#39;

#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(&#39;function(x, y){return x + y}&#39;, error = FALSE) #FALSE不允许在全局范围内定义匿名函数

2. ESprima: parsing, supports ECMAScript2017 and Returns a reasonable syntax tree format standardized by the ESTree project

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) #返回&#39;JS_EVAL&#39;类型,解析成树形结构

3.compiling coffeescript, compiling coffee script into JavaScript, the code is compiled into equivalent JS one-to-one, and the coffeescript function is bound to coffee script cpmpiler

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,将代码重写为更紧凑但等效的程序

4.uglify_reformat reformatting, very suitable for repairing spaces, semicolons, etc.

code <- "function test(x, y){x = x || 1; y = y || 1; return x*y;}"
cat(uglify_reformat(code, beautify = TRUE, indent_level = 2))

5.JSHint code analysis, automatically detects errors and potential problems in JavaScript code, and returns data.frame

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn