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

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.

Hi @xiaoni,

Welcome to the RStudio community!

To answer your questions, the extendShinyjs function is used "for running arbitrary code" shinyjs. Loading scripts isn't possible using this method. Instead, you can use the functions includeScript() or tags$script(src = "path/to/js/"). These functions are included in the shiny package.

For example, if you were using the standalone D3 file in a shiny app, here's how you would load the scripts and use in a separate js file. (I would also recommend saving copies into the www folder).

Directory Structure

my-shiny-app /
    - ui.R
    - server.R
    - www/
        - d3v5.min.js
        - index.js

ui.R

ui <- tagList(
    tags$svg(id = "box"),
    tags$script(src = "d3v5.min.js"),
    tags$script(src = "index.js")
)

index.js

# d3 is available since it's loaded before
const elem = d3.select("box");
# do something ...

Hi @dcruvolo,

I understood that this can be used if I needed to call just 1 javascript file. However, what I wanted to do is to load a module. Example: This node.js module is what I wanted to use.

Is there a way for me to use this module in R?
In JS, I just needed the var = require(google-play-scraper). But in R, this is not working.

Perhaps you could develop a htmlwidget to provide r bindings for the JavaScript.

https://www.htmlwidgets.org/

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.

How are you planning on using this package in a shiny app?

I experimented a bit with this example. My initial thinking was that this package needed to be written using a application bundler. I restructured the project to use node, parceljs, but I ran into a CORS issue when running in the browser. After reading through the issues, it appears the google-play-scrapper is designed to run in a node environment rather than in browser (see issues 274 and 362). To get this to work, you will likely need to setup your own backend outside of shiny as you will need to create several APIs that process the request(s).

It might be worth looking into supplementary methods to shiny as I do not think you can create your own APIs in a shiny server. The plumber package might be a good option for processing requests in the R environment. This would involve a bit of javascript though. Another option, might be using express. Although this is a pure js backend framework and it might not be ideal.

If you just care about scraping the data there may be more direct R ways to query the api.

Yes. I am only concerned with the scraping of data specifically the user reviews of apps that are publicly available. After I scraped the data, I will use R to to analyze it. I have checked appfigures but I don't have the username, password from App Figures (App Intelligence, ASO Tools, and App Analytics for mobile app and game developers from Appfigures). The analytics price is a bit pricey as well. :frowning: Anyways, thank you for the suggestion.

Basically the output of this package is my input for some analysis I am going to do with R. I guess this cannot be helped. I might just scrape data and save it as csv file in JS. Then, R will just read the csv file. Anyway, thanks for your help.

I was thinking you were using this in a Shiny app as indicated by the tags. Using the JS library on its own is much easier. Here are a few things you will need to get started.

  1. Install node. If you do not have node on your machine, download the latest installer and run. You can check to see if node is installed by running node -v in a terminal.

  2. Create a new directory and create a package.json file. Running npm init will bring up a series of prompts for entering information about your project. You can skip over these questions.

mkdir my-project
cd my-project
npm init
  1. Install the google-play-scrapper package.
npm install google-play-scrapper
  1. Create a new javascript file for running the google play scraper.
touch app.js
  1. In the app.js file, write your JS code for scrapping data.
const gplay = require("google-play-scraper");
gplay.app({appId: 'com.google.android.apps.translate'})
  .then(console.log, console.log);
  1. To run, type the following command in the terminal.
node app.js

Running the script will print out the results in the terminal window. If you want to save them to file, use the npm package csv-writer or fast-csv.

2 Likes

Yes. I have done this already. Thank you so much for your help @dcruvolo

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.