Skip to content

Commit

Permalink
renamed js log to cyjshiny_log to break conflict with igvShiny.R
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-shannon committed May 27, 2022
1 parent afcf807 commit 2e3a944
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 47 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: cyjShiny
Title: Cytoscape.js Shiny Widget (cyjShiny)
Version: 1.0.34
Date: 2022-01-16
Version: 1.0.35
Date: 2022-05-27
Authors@R: c(person("Omar", "Shah", role = c("ctb")),
person("Paul", "Shannon", email = "[email protected]", role = c("aut")),
person("Augustin", "Luna", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5709-371X")))
Expand All @@ -27,4 +27,4 @@ Collate:
'graphsToJSON.R'
'cyjShiny.R'
VignetteBuilder: knitr
RoxygenNote: 7.1.1
RoxygenNote: 7.2.0
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
default:
@echo targets: basic[roxy install] build check

basic: roxy install

all: roxy install build check
Expand All @@ -6,7 +9,7 @@ buildCheck: build check

roxy:
R -e "devtools::document()"

vig:
R -e "devtools::build_vignettes()"

Expand Down
36 changes: 36 additions & 0 deletions inst/demos/tiny.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
library(shiny)
library(cyjShiny)
library(htmlwidgets)
library(graph)
library(jsonlite)
#----------------------------------------------------------------------------------------------------
tbl.nodes <- data.frame(id=c("A", "B", "C"),
type=c("kinase", "TF", "glycoprotein"),
lfc=c(-3, 1, 1),
count=c(0, 0, 0),
stringsAsFactors=FALSE)

tbl.edges <- data.frame(source=c("A", "B", "C"),
target=c("B", "C", "A"),
interaction=c("phosphorylates", "synthetic lethal", "unknown"),
stringsAsFactors=FALSE)

graph.json <- toJSON(dataFramesToJSON(tbl.edges, tbl.nodes), auto_unbox=TRUE)
#----------------------------------------------------------------------------------------------------
ui = shinyUI(fluidPage(

cyjShinyOutput('cyjShiny'),
width=10
)
)
#----------------------------------------------------------------------------------------------------
server = function(input, output, session)
{
output$cyjShiny <- renderCyjShiny({
cyjShiny(graph=graph.json, layoutName="cola")
})

} # server
#----------------------------------------------------------------------------------------------------
runApp(shinyApp(ui=ui, server=server), port=9999)

86 changes: 43 additions & 43 deletions inst/htmlwidgets/cyjShiny.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var defaultStyle = [{selector: 'node', css: {
];
//----------------------------------------------------------------------------------------------------
var executionMode = "devel";
const log = function(msg)
const cyjshiny_log = function(msg)
{
if(executionMode == "devel")
console.log(msg);
Expand All @@ -42,36 +42,36 @@ HTMLWidgets.widget({
type: 'output',

factory: function(el, allocatedWidth, allocatedHeight) {
log("---- entering factory, initial dimensions: " + allocatedWidth + ", " + allocatedHeight);
cyjshiny_log("---- entering factory, initial dimensions: " + allocatedWidth + ", " + allocatedHeight);
var cyj;
return {
renderValue: function(x, instance) {
log("---- ~/github/cyjsShiny/inst/browserCode/src/cyjShiny.js, renderValue");
log(x);
cyjshiny_log("---- ~/github/cyjsShiny/inst/browserCode/src/cyjShiny.js, renderValue");
cyjshiny_log(x);
var data = JSON.parse(x.graph);
var layoutName = x.layoutName;
var style = JSON.parse(x.style.json);
if (style == "default style")
style = defaultStyle;
// log(data);
// cyjshiny_log(data);
var cyDiv = el;
cyj = cytoscape({
container: cyDiv,
elements: data.elements,
layout: {name: layoutName},
style: style, //defaultStyle,
ready: function(){
log("cyjShiny cyjs ready");
log("cyjShiny widget, initial dimensions: " + allocatedWidth + ", " + allocatedHeight)
cyjshiny_log("cyjShiny cyjs ready");
cyjshiny_log("cyjShiny widget, initial dimensions: " + allocatedWidth + ", " + allocatedHeight)
$("#cyjShiny").height(allocatedHeight)
$("#cyjShiny").width(allocatedWidth)
var cyj = this;
window.cyj = this; // terrible hack. but gives us a simple way to call cytosacpe functions
if (style != null) {
cyj.style(style.json);
}
log("small cyjs network ready, with " + cyj.nodes().length + " nodes.");
log(" initial widget dimensions: " +
cyjshiny_log("small cyjs network ready, with " + cyj.nodes().length + " nodes.");
cyjshiny_log(" initial widget dimensions: " +
$("#cyjShiny").width() + ", " +
$("#cyjShiny").height());

Expand All @@ -81,7 +81,7 @@ HTMLWidgets.widget({
if(typeof(evt.target.id) != "undefined"){
id = evt.target.id();
}
console.log("tapped: ", id)
cyjshiny_log("tapped: ", id)
Shiny.setInputValue("selection", id, {priority: "event"});

})
Expand All @@ -92,10 +92,10 @@ HTMLWidgets.widget({
}) // cytoscape()
}, // renderValue
resize: function(width, height) {
console.log("widget resize");
cyjshiny_log("widget resize");
var parentWidth = $("#cyjShiny").parent().width()
var parentHeight = $("#cyjShiny").parent().height()
console.log("widget resize, parentWidth: " + parentWidth + " height: " + parentHeight);
cyjshiny_log("widget resize, parentWidth: " + parentWidth + " height: " + parentHeight);
$("#cyjShiny").width(parentWidth)
$("#cyjShiny").height(parentHeight)
},
Expand All @@ -113,23 +113,23 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("doLayout", function(mes
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("getNodePositions", function(message){

log("--- entering getNodePositions, customMessangeHandler")
cyjshiny_log("--- entering getNodePositions, customMessangeHandler")
var tbl = JSON.stringify(self.cyj.nodes().map(function(n){return{id: n.id(),
x: n.position().x,
y: n.position().y}}));
log(tbl)
cyjshiny_log(tbl)
Shiny.setInputValue("tbl.nodePositions", tbl, {priority: "event"});
})

//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setNodePositions", function(message){

log("--- entering setNodePositions, customMessangeHandler")
log(message.tbl)
cyjshiny_log("--- entering setNodePositions, customMessangeHandler")
cyjshiny_log(message.tbl)

var tbl = message.tbl; // JSON.parse(message.tbl)

console.log("calling setPosition map");
cyjshiny_log("calling setPosition map");
tbl.map(function(e){
var tag="[id='" + e.id + "']";
self.cyj.$(tag).position({x: e.x, y:e.y});
Expand All @@ -155,14 +155,14 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("addGraph", function(mes
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("redraw", function(message){

log("redraw requested");
cyjshiny_log("redraw requested");
self.cyj.style().update();
})

//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setNodeAttributes", function(message){

log("setNodeAttributes requested")
cyjshiny_log("setNodeAttributes requested")

var nodeIDs = message.nodes;
var attributeName = message.attribute;
Expand All @@ -177,7 +177,7 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setNodeAttributes", fun
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setEdgeAttributes", function(message){

log("setEdgeAttributes requested")
cyjshiny_log("setEdgeAttributes requested")

var attributeName = message.attributeName;
var sourceNodes = message.sourceNodes;
Expand All @@ -187,11 +187,11 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setEdgeAttributes", fun

for(var i=0; i < sourceNodes.length; i++){
var id = sourceNodes[i] + "-(" + interactions[i] + ")-" + targetNodes[i];
log("edge id: " + id)
cyjshiny_log("edge id: " + id)
var edge = self.cyj.getElementById(id)
log(edge)
cyjshiny_log(edge)
if(edge != undefined){
log("setting edge " + attributeName + " to " + values[i])
cyjshiny_log("setting edge " + attributeName + " to " + values[i])
edge.data({[attributeName]: values[i]})
}
} // for i
Expand All @@ -200,7 +200,7 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("setEdgeAttributes", fun
//----------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("selectNodes", function(message){

log("selectNodes requested (16 apr 2020, 835a) : " + message);
cyjshiny_log("selectNodes requested (16 apr 2020, 835a) : " + message);

var nodeIDs = message;

Expand All @@ -214,7 +214,7 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("selectNodes", function(
filterStrings.push(s);
} // for i

log("filtersStrings, joined: " + filterStrings.join());
cyjshiny_log("filtersStrings, joined: " + filterStrings.join());

var nodesToSelect = window.cyj.nodes(filterStrings.join());
nodesToSelect.select()
Expand All @@ -223,83 +223,83 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("selectNodes", function(
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("clearSelection", function(message){

log("clearSelection requested: " + message);
cyjshiny_log("clearSelection requested: " + message);
self.cyj.filter("node:selected").unselect();

})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("showAll", function(message){

log("showAll requested: " + message);
cyjshiny_log("showAll requested: " + message);
self.cyj.nodes().show()
self.cyj.edges().show()

})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("invertSelection", function(message){

log("invertSelection requested: " + message);
cyjshiny_log("invertSelection requested: " + message);
var currentlySelected = self.cyj.filter("node:selected");
self.cyj.nodes().select();
currentlySelected.unselect();
})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("getCurrentSelection", function(message){

log("--- currentSelection requested ");
cyjshiny_log("--- currentSelection requested ");
var currentlySelected = self.cyj.filter("node:selected");
Shiny.setInputValue("currentSelection", currentlySelected, {priority: "event"});

})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("hideSelection", function(message){

log("hideSelection requested: " + message);
cyjshiny_log("hideSelection requested: " + message);
self.cyj.filter("node:selected").hide();

})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("getSelectedNodes", function(message){

log("getSelectedNodes requested: " + message);
cyjshiny_log("getSelectedNodes requested: " + message);
var value = self.cyj.filter("node:selected")
.map(function(node) {
return(node.data().id)})
//return {id: node.data().id, label: node.data().label}})

log(self.cyj.filter("node:selected"));
log(value)
cyjshiny_log(self.cyj.filter("node:selected"));
cyjshiny_log(value)
Shiny.setInputValue("selectedNodes", value, {priority: "event"});

});
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("sfn", function(message){

log("sfn requested: " + message);
cyjshiny_log("sfn requested: " + message);
self.cyj.nodes(':selected').neighborhood().nodes().select();

})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("fitSelected", function(message){

log("fitSelected requested");
cyjshiny_log("fitSelected requested");
var padding = message.padding;

var selectedNodes = self.cyj.filter("node:selected");

if(selectedNodes.length == 0){
log("no nodes currently selected")
cyjshiny_log("no nodes currently selected")
}
else{
log("fitSelected, with padding " + padding);
cyjshiny_log("fitSelected, with padding " + padding);
self.cyj.fit(selectedNodes, padding)
}
})
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("fit", function(message){
log("fit requested: ");
cyjshiny_log("fit requested: ");
var padding = message.padding;
log(" padding: " + padding)
cyjshiny_log(" padding: " + padding)
self.cyj.fit(padding);
});
//------------------------------------------------------------------------------------------------------------------------
Expand All @@ -312,7 +312,7 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("fit", function(message)
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("loadJSONNetwork", function(message) {

window.cyj.remove(window.cyj.elements());
log("--- loadJSONNetwork")
cyjshiny_log("--- loadJSONNetwork")
window.msg = message;
var graph = JSON.parse(message.json)
window.cyj.add(graph.elements);
Expand All @@ -322,9 +322,9 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("loadJSONNetwork", funct
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("loadStyle", function(message) {

log("loading style");
cyjshiny_log("loading style");
//var styleSheet = message.json;
log(message)
cyjshiny_log(message)
var style = JSON.parse(message.json)
if(style == "default style")
style = defaultStyle;
Expand All @@ -334,7 +334,7 @@ if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("loadStyle", function(me
//------------------------------------------------------------------------------------------------------------------------
if(HTMLWidgets.shinyMode) Shiny.addCustomMessageHandler("savePNGtoFile", function(message){

log("savePNGtoFile: " + message);
cyjshiny_log("savePNGtoFile: " + message);
var pngJSON = JSON.stringify(window.cyj.png());
Shiny.setInputValue("pngData", pngJSON, {priority: "event"});

Expand Down

0 comments on commit 2e3a944

Please sign in to comment.