Seeking assistance with formatting output for OpenCPU and igraph

Here is my adjacency array data:

var g = [[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]

The code I am using in OpenCPU is as follows:

            ocpu.call("centralization.closeness", {graph: g}, function(res){
            // console.log(ocpu.seturl(res.output[0]));
            $http.get("//public.opencpu.org/"+res.output[0]+"/json").success(function(data) {
                console.log(data);
            });
        });

However, I am encountering an error message which says:

OpenCPU error HTTP 400 Not a graph object

In call: centralization.closeness(graph = g)

Answer №1

centralization.closeness function requires a graph object instead of an array

Here's a suggestion:

  • First, convert the array to an adjacency matrix
  • Then, create a graph from the matrix using graph_from_adjacency_matrix function
  • Finally, pass the resulting graph to the centralization.closeness function

NOTE: You can find the solution here

var graph = [
  [0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1],
  [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1],
  [1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  [0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0],
  [1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0],
  [1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1],
  [0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1],
  [0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1],
  [0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
  [1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1],
  [1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1],
  [1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1],
  [1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0],
  [1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0]
];

//Setting CORS to call the igraph package
ocpu.seturl("https://public.opencpu.org/ocpu/library/igraph/R");

var graphSession;

$('#output').text(graph.toString());

$('#adjMatrix').click(function() {
  ocpu.call("graph_from_adjacency_matrix", {
    adjmatrix: graph,
    mode: 'directed',
    weighted: true
  }, function(session) {
    graphSession = session;
    //Retrieve session console asynchronously
    graphSession.getConsole(function(outtxt) {
      $("#output").text(outtxt);
      $("#centralize").prop('disabled', false);
    });
  }).fail(function() {
    alert("Error: " + req.responseText);
  });
});

$('#centralize').click(function() {
  var centralizeReq = ocpu.call("centralization.closeness", {
    graph: graphSession,
    mode: "all",
    normalized: true
  }, function(centralizeSession) {
    centralizeSession.getConsole(function(outtxt) {
      $("#output").text(outtxt);
    });
  }).fail(function() {
    alert("Error: " + req.responseText);
  });

});
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://cdn.opencpu.org/opencpu-0.4.js"></script>

<div>
  <textarea name="" id="output" cols="60" rows="10"></textarea>
  <br />
  <button id="adjMatrix">Graph From Adj</button>
  <button id="centralize" disabled>Centralize</button>
</div>

For more examples on utilizing OpenCPU, check this link out: http://jsfiddle.net/user/opencpu/fiddles/

Answer №2

Can you clarify your expectations for OpenCPU? The current code being executed is as follows:

library(igraph)
g <- jsonlite::fromJSON('[[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]]')
igraph::centralization.closeness(g)

If this code runs in R, it will result in the same error. You must create a wrapper function to convert the matrix into a compatible format for centralization.closeness.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Could not locate module: The package path ./react is not exported from the package in E:NextAppportfolio_website-mainportfolio_website-main ode_modules ext-auth

I am encountering an issue while trying to import SessionProvider from Next-Auth. The error message that is being displayed is: "Module not found: Package path ./react is not exported from package E:\NextApp\portfolio_website-main\port ...

undefined reference to $

I'm currently working on a JavaScript project that involves using key events to display alphabets on the screen. However, I've encountered an error and need some assistance. <!DOCTYPE html> <html lang="en"> <head> <met ...

Is there a way to switch an element across various pages within Ionic 3?

Is it possible to exchange information between two pages using logic? I have successfully implemented a checklist, but I am unsure how to add a success/error icon next to the Room name on the 'verifyplace.html' page after invoking the goToNextPa ...

Using Angular.JS to iterate over a nested JSON array using ng-repeat

I am currently working on a People service that utilizes $resource. I make a call to this service from a PeopleCtrl using People.query() in order to retrieve a list of users from a json api. The returned data looks like this: [ { "usr_id" : "1" ...

Creating a Visual Presentation with Background Image Transition in HTML, CSS, and JavaScript

Seeking assistance in setting up a basic background image slideshow on my website. I have been unable to locate a suitable tutorial online, so I'm reaching out here for guidance. HTML: <body> <h3>Welcome!</h1> <p>Lorem i ...

"How can you enhance the performance of JavaScript and CSS in a Chrome Extension without using exclude_matches/globs or excluding domains

I have been in the process of creating a Chrome Extension, and unfortunately, when I tried to make it work on specific URLs, I encountered an issue. While Chrome has options like exclude_matches and exclude_globs for this purpose, there seems to be a bug i ...

Difficulty Encountered in Rendering Component Using setTimeout Function

Having trouble figuring out why my component, enclosed in a setTimeout function, is not appearing on the DOM: const ContentMain = Component({ getInitialState() { return {rendered: false}; }, componentDidMount() { this.setStat ...

Passing a JavaScript variable to a URL parameter can be achieved by

Can anyone advise me on passing JavaScript string variables and displaying them in the URL? For instance, let's say the URL is "xyc.com". Upon populating a variable (a), I wish for that variable to appear in the address bar as URL = "xyc.com/?a=". Cur ...

Unit testing in AngularJS involves the process of attaching data from the $q.resolve() function to an object

I am currently evaluating a service that relies on another service for API calls, which I refer to as the data service. The data service has already been tested separately, so I have created a basic implementation with placeholder functions; data is then r ...

Vue error: Uncaught promise rejection - RangeError: The computed value update has exceeded the maximum call stack size

My computed code snippet: computed: { display: { get() { return this.display }, set(newValue) { this.display = newValue } } }, Attempting to update the computed value from a function in ...

The useRouter() function doesn't seem to be successfully navigating to the main landing page

"use client" import { useState } from 'react'; import {auth} from '../../firebase-config' import {createUserWithEmailAndPassword} from 'firebase/auth' import { useRouter } from 'next/router'; const SignUp = ...

Switching a conditional className to a styled component

Currently, I am in the process of converting my project from plain CSS to styled components using styled-components. So far, I have successfully converted all of my components except for one. I have looked at various examples on Stack Overflow but none of ...

Creating a stunning display of six video textures on a cube through the power of three.js

My current project involves working with a video that has been segmented into six parts. Here is a screenshot of the input video. The goal is to project these 6 videos onto the six faces of a cube using JavaScript: <!DOCTYPE html> <html> &l ...

Is it possible to save jQuery plugin initialization settings for future use?

Is it possible to save a default set of settings for a lightbox jQuery plugin, including options and callback functions, in a variable or array that can be referenced later in different contexts with varying configurations? For example, could I initially ...

The protection ensured by employing an extensive hash in the query string for the recognition of a changing document

I am currently developing a small web application that creates exercise program printouts. The concept is simple - a user like a personal trainer can craft an exercise regimen and send it to their client via email using a unique link. http://www.myurl ...

determine the vertical dimension of the horizontal scrollbar

I ran into an issue where I needed to determine the height of a horizontal scrollbar. This question and answer recommended using the clientHeight property to calculate the difference. Unfortunately, this method no longer works as shown here: https://jsfid ...

Angular is programmed to detect any alterations

Upon detecting changes, the NgOnChanges function triggers an infinite service call to update the table, a situation that currently perplexes me. Any assistance on this matter would be greatly appreciated. Many thanks. The TableMultiSortComponent functions ...

Connected Date Selector

I am new to using Bootstrap and attempting to implement a linked datepicker with the following requirements: Display only the date (not the time). The default selected date on page load should be today's date for both the Datepicker and the text ...

Enhancing React Performance: Storing Component Identity in Redux State

Can I safely pass this to a Redux action creator from a component defined using React.createClass? In my code, I have created the following reducer: const unsavedChangesProtectionReducer = handleActions({ [ENABLE_UNSAVED_CHANGES_PROTECTION]: (unsaved ...

How to insert a row above the header in an Excel sheet using JavaScript within

i am using excel js to export json data to excel. The json data is successfully exported to the sheet, but now I need to add a row that provides details of the sheet above the header text. for more details please refer image the code is shown below: i ...