网址:www/cytojs.js
$(document).ready(function()。{
shining.addCustomMessageHandler(“加载JSON”,
功能(消息){
//var expjson=json.stringify(message.elements_json);
//var style json=json.stringify(message.style_json);
var expjson=message.elements_json;
//var style json=message.style_json;
var cy=细胞形态({
容器:document.getElementByID(“cy”),
布局:{
name:'预设',
拟合:真
}
元素:expjson,
样式:[
{
selector:'节点',
样式:{
'背景色':'666',
“label”:“数据(id)”。
}
}
{
selector:'边缘',
样式:{
“宽度”:3,
'线条颜色':'ccc',
'目标箭头颜色':'ccc',
“目标箭头形状”:“三角形”
}
}
]
(});
(});
(});
对于这个例子,一旦我按下按钮,我希望网络被渲染并出现在应用程序中。当我运行这个并在我的Web浏览器(chrome)中打开开发人员工具控制台时,我会得到一堆错误:
为了完整起见,这里有一小段
。[
{
“数据”:。{
“ac_uniprot”:“Q02535”,
“gene”:“id3”,
“role”:“整体”,
“ID”:0,
“父”:0
}
“group”:“节点”
}
{
“数据”:。{
“parent_ac_uniprot”:“Q02535”,
“role”:“起止”,
“ID”:1,
“父”:0,
“label”:“0”
}
“group”:“节点”,
“位置”:。{
“Y”:225.0,
“X”:742.0
}
}
{
“数据”:。{
“parent_ac_uniprot”:“Q02535”,
“role”:“起止”,
“ID”:2,
“父”:0,
“label”:“119”
}
“group”:“节点”,
“位置”:。{
“Y”:225.0,
“X”:860.0
}
}
{
“数据”:。{
“parent_ac_uniprot”:“Q02535”,
“父”:0,
“color”:“cdbe70”,
“label”:“hlh”,
“role”:“域”,
“ID”:3
}
“group”:“节点”
}
{
“位置”:。{
“Y”:225.0,
“X”:784.0
}
“数据”:。{
“parent_ac_uniprot”:“Q02535”,
“role”:“dom_pos”,
“ID”:4,
“父”:3,
“label”:“42”
}
“group”:“节点”
}
]
当我手动进入并将JSON文件更改为在idproperty周围加引号时,它可以工作,但这显然不是一个实际的解决方案。我对JavaScript比较陌生,我已经用尽了所有我能想到的方法来让它工作。很明显,这是JSON格式的问题…可能是Shining的session$sendcustomeMessage().i s not encoding the objects as needed by cytoscape.js,but I do not know how to solve this.here)然后我想使用闪亮的应用程序读取文件,并呈现网络可视化。到目前为止,这个产生错误的工作示例:
应用程序
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(rcytoscapejs)
library(DT)
library(jsonlite)
ui <- navbarPage("Shiny Protein Visualization using Cytoscape.js",
tabPanel("Network",
sidebarLayout(
sidebarPanel(
actionButton("imprtJSON", "JSON Test (not working)"),
width=3),
mainPanel(
rcytoscapejsOutput("cy", height="600px")
)
)
),
tags$head(tags$script(src="cyjs.js"))
)
server <- function(input, output, session) {
observeEvent(input$imprtJSON, {
# message <-
# message$elements_json <- toJSON(message$elements_json)
# message$style_json <- toJSON(message$style_json)
# browser()
session$sendCustomMessage("loadJSON",
message = list(alertMessage = "Testing sending JSON object from R to JS",
elements_json = read_json("data/graph_elements0044.json")))
})
}
# Run the application
shinyApp(ui = ui, server = server)
网址:www/cytojs.js
$(document).ready(function() {
Shiny.addCustomMessageHandler("loadJSON",
function(message) {
//var expJson = JSON.stringify(message.elements_json);
//var styleJson = JSON.stringify(message.style_json);
var expJson = message.elements_json;
//var styleJson = message.style_json;
var cy = cytoscape({
container: document.getElementById('cy'),
layout: {
name: 'preset',
fit: true
},
elements: expJson,
style: [
{
selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{
selector: 'edge',
style: {
'width': 3,
'line-color': '#ccc',
'target-arrow-color': '#ccc',
'target-arrow-shape': 'triangle'
}
}
]
});
});
});
对于这个例子,一旦我按下按钮,我希望网络被渲染并出现在应用程序中。当我运行这个程序并在我的Web浏览器(chrome)中打开开发人员工具控制台时,我会得到一堆错误:
为了完整起见,下面是graph_elements0044.json
正在加载的文件:
[
{
"data": {
"ac_uniprot": "Q02535",
"gene": "ID3",
"role": "whole",
"id": 0,
"parent": 0
},
"group": "nodes"
},
{
"data": {
"parent_ac_uniprot": "Q02535",
"role": "start-end",
"id": 1,
"parent": 0,
"label": "0"
},
"group": "nodes",
"position": {
"y": 225.0,
"x": 742.0
}
},
{
"data": {
"parent_ac_uniprot": "Q02535",
"role": "start-end",
"id": 2,
"parent": 0,
"label": "119"
},
"group": "nodes",
"position": {
"y": 225.0,
"x": 860.0
}
},
{
"data": {
"parent_ac_uniprot": "Q02535",
"parent": 0,
"color": "#CDBE70",
"label": "HLH",
"role": "domain",
"id": 3
},
"group": "nodes"
},
{
"position": {
"y": 225.0,
"x": 784.0
},
"data": {
"parent_ac_uniprot": "Q02535",
"role": "dom_pos",
"id": 4,
"parent": 3,
"label": "42"
},
"group": "nodes"
}
]
当我手动进入并更改JSON文件时,在id
属性,它有效,但这显然不是一个实际的解决方案。我对JavaScript比较陌生,我已经用尽了所有我能想到的方法来让它工作。很明显这是JSON格式的东西…可能是闪亮的session$sendCustomeMessage()
不是根据cytoscape.js的需要对对象进行编码,但我不知道如何解决这个问题。