Is there a way to use a Javascript package in R? - javascript

I am trying to call a Javascript file in R. Inside the Java script file has a require function that calls a a Javascript package. When I try to run this in R, I am getting a ReferenceError. I am not so familiar with frontend, but I want to use Javascript to do something for me.
rFile.R
extendShinyjs(script = 'file/jsFile.js')
jsFile.js
var = require('some-module')
Error in R:
Error in : shinyjs: Error parsing the JavaScript file: ReferenceError: require is not defined.
Do you know how I can make this work?

This R Package helped me to convert the npm package to "browserify" it. Thanks for all your help.
https://cran.r-project.org/web/packages/V8/vignettes/npm.html#what_is_v8_(not)
If you will be encountering errors like:
ReferenceError: window is not defined
You need to remove all the lines in bundle.js that uses window. Though I am still not sure of the implications of doing so. I am still working on how to call functions.

Related

MathJax with WebPack ReferenceError: require is not defined

I would like to put formulas in my webpage, which uses (dotnet Kestrel). I do not want to load javascript libraries from the net, so I am using webpack and npm for my packages.
My problem is, that I find it impossible to load MathJax. I have tried the following:
import 'mathjax-full';
require('mathjax-full'); // << not the error itself
import MathJax from 'mathjax-full';
The most annoying error that I get is this:
ReferenceError: require is not defined
I must do something obviously wrong. The error message comes from the MathJax internals. I have also tried to import requirejs, as some forums mentioned that as some kind of "workaround". The error I get with it is when I run WebPack:
ERROR in ./node_modules/requirejs/bin/r.js 1:0
Module parse failed: Unexpected character '#' (1:0)
Has anybody succeeded with MathJax on WebPack?
I've encountered the same problems as you plus a million more. MathJax is really not WebPack-friendly.
My solution for Angular:
npm install --save mathjax
Extend the "scripts" array of angular.json with:
"node_modules/mathjax/es5/tex-mml-chtml.js"
And now to call MathJax APIs, DO NOT use import of any kind. The mathjax module sets its main to es5/node-main.js, which uses an ugly hack to call require() at runtime, which is the ultimate source of trouble you saw.
Instead, do this:
declare var MathJax;
Now you may complain that this way you have no type information. Well, the fact is that not even mathjax-full has good type information for methods inside this global object: they inject the methods (such as MathJax.typeset()) at runtime and their types are declared using [name: string]: any;.
If your really want type information, you're probably better off declaring it yourself, e.g.:
declare interface MathJaxGlobal {
typeset(elems: HTMLElement[]);
}
declare var MathJax: MathJaxGlobal;

How do I access variables/functions in bundle.js

hello how do you access variables in bundle.js?
I am trying to make a client side javascript and play around with variables and functions but am having trouble calling them in index.html, chrome-console.
I use browserify to bundle my script.js into bundle.js (which helps me with 'require' which the browser does not recognize), however when I try to access variables or functions defined in the script.js, now bundle.js I get e.g. Uncaught ReferenceError: xx is not defined
Any help? Or am I not allowed to use node packages on client side html/scripts?
edit: did a bit of googling and came upon this guy who says I should use XHR / AJAX "require" is not defined error- javascript
edit2: window.xx = xx seems to be a temporary solution
Correct me if I'm wrong, because I have not used Browserify (I've used other bundling/processing tools), but to my understanding, Browserify bundles up all the code from the original source file into a bundle.js file. Within that bundle.js is a scope that is not accessible from the outside (unless specifically exported perhaps, but that's another story).
So for example:
// source.js
var test = require('./some-file-with-an-empty-object.js');
// Pseudo code example of bundle.js
;(function(){
var test = {};
})();
Because of this IIFE, the scope var test is limitted and not accessible to external scripts or window.
So in short, do what you need to do within your source file, not outside of it.

I got error when link external library with javascript

I installed the color-convert library via npm but the browser shows me an error message
Uncaught ReferenceError: require is not defined home.js:134
at HTMLButtonElement.<anonymous> (home.js:134)
at HTMLButtonElement.dispatch (jquery-3.4.0.js:5233)
at HTMLButtonElement.elemData.handle (jquery-3.4.0.js:5040)
JS
var convert = require('color-convert'); // this is line 134
alert(convert.hex.lab('DEADBF'));
I think there is a problem with paths?
require() isn't a function provided by your browser, and is more of a sign that this source code is a common JS module.
In order to use a common JS module, you first need to run your source through a program that bundles the source, sorta replacing each require('other_module') with the source of the other module, producing a single Javascript source file which can included in your frontend HTML.
Two examples of bundlers are browserify and webpack.

Why do dependency problems cause javascript code not to run properly when within a Meteor project?

I have been trying to port HTML5 with js code into Meteor. I'm having Javascript dependency problems.
The code can be found at: https://github.com/cwilso/Audio-Input-Effects
I created a new basically empty meteor project (which runs fine) and then added all of the js files from the repo above (which also runs fine on its own).
In order to make sure that the load order was correct, I renamed all the js files using numeric prefixes so that they would definitely be in the same order that they are loaded in the github repo. Looking forward to Meteor coming up with a better solution to this particular issue. I made a local copy of one js file that was otherwise loaded from a url in the repo.
In order to try to initialize the js I also added a file hello.js:
if (Meteor.isClient) {
Meteor.startup(function () {
// code to run on server at startup
initAudio;
});
}
When meteor runs and I look in the console, I get the following errors:
Uncaught TypeError: o3djs.provide is not a function (120_shader.js)
Uncaught ReferenceError: initAudio is not defined (hello.js)
Uncaught ReferenceError: Matrix4x4 is not defined (110_visualizer.js)
Thank you for your help.
I was able to resolve this issue by putting all of the js source files into a single js file in the correct order.
Anybody still wanting information regarding meteor load order, Scotch.io wrote an update to the official docs which clears it up somewhat.
https://github.com/meteor/meteor/commit/a5bdf481dfece9ebc57107d71be478f9b48cbd1e

NodeJS import error

I'm trying to use the resemblejs library (http://huddle.github.io/Resemble.js/) to compare two images. However, when I have created a directory and added resemblejs as a dependency, but when I try to run the following:
nodejs test.js, I get the following error
var api = resemble(fileData).onComplete(function(data){
^
ReferenceError: resemble is not defined
I've never used NodeJS before, so it might be something really simple. Any ideas?
You can't get directly node module instance from it. You can check this issue on its repo. node-resemble is an Node port of Resemble.js.
Hope it will be useful for you.

Categories