我正在努力将一些 JavaScript 代码(包括异步和图形功能)移植到 R。请帮助!
这是我尝试移植的内容:
import jsonpFetch from "./jsonpFetch";
import bus from '../bus';
/**
* This function builds a graph from google's auto-suggestions.
*/
export default function buildGraph(entryWord, pattern, MAX_DEPTH, progress) {
entryWord = entryWord && entryWord.trim();
if (!entryWord) return;
entryWord = entryWord.toLocaleLowerCase();
const insertPosition = pattern.indexOf('...');
if (insertPosition < 0) {
throw new Error('Query pattern is missing "..."');
}
const queryPosition = pattern.indexOf('[query]');
if (queryPosition < 0) {
throw new Error('Query pattern is missing "[query]" keyword');
}
if (insertPosition < queryPosition) {
throw new Error('[query] should come before ...');
}
let cancelled = false;
let pendingResponse;
let graph = require('ngraph.graph')();
graph.maxDepth = MAX_DEPTH;
let queue = [];
let requestDelay = 300 + Math.random() * 100;
progress.startDownload();
startQueryConstruction();
return {
dispose,
graph
}
function dispose() {
cancelled = true;
if (pendingResponse) {
pendingResponse.cancel();
pendingResponse = null;
}
}
function startQueryConstruction() {
graph.addNode(entryWord, {depth: 0});
fetchNext(entryWord);
}
function loadSiblings(parent, results) {
let q = fullQuery(parent).toLocaleLowerCase();
var parentNode = graph.getNode(parent);
if (!parentNode) {
throw new Error('Parent is missing for ' + parent);
}
请注意,我已经包含了一些注释掉的 JavaScript 代码行,我不确定 R 的等价物是什么。对我来说,大部分晦涩的代码都集中在如何igraph
在 R 中执行操作以及如何在 R 中异步执行操作(使用promises
和/或futures
)。
归属:https : //github.com/anvaka/vs/blob/master/src/lib/buildGraph.js
提前致谢!
30秒到达战场
相关分类