JohnCoene / sigmajs

Σ sigma.js for R
http://sigmajs.john-coene.com
Other
72 stars 7 forks source link

Setting node attributes #15

Closed SaharanAbhishek closed 4 years ago

SaharanAbhishek commented 4 years ago

How can I assign each node a unique label of my choice and unique x and y coordinates. Means creating a number of node objects and pushing same to nodes list.

Same I am able to do in javascript..... g.nodes.push({ id: data.nodes[i].id, label: labelName, x: xAxisVal, y: yAxisVal, size: .1 color: nodeColor, });

JohnCoene commented 4 years ago

This is all in the Get Started guide.

# generate data using convenience functions
nodes <- sg_make_nodes()
edges <- sg_make_edges(nodes)

sigmajs() %>%
  sg_nodes(nodes, id, size, color, x = x, y = y) %>%
  sg_edges(edges, id, source, target)
SaharanAbhishek commented 4 years ago

Suppose I have below dataframe for nodes:

Node ID xAxis yAxis Name City 1 0 10 20 John New York 2 1 11 21 Peter Paris 3 2 12 22 Tom London 4 3 13 23 Sam Los Angeles 5 4 14 24 Jack Tokyo

for edges

Edge Source Target 1 0 1 2 0 3 3 1 4 4 1 2 5 2 3 6 3 1 7 4 4

In javascript case (I have JSON instead of dataframe in this case), first I will loop through all nodes and create object for each node with attribute and then push same to nodes array, same for edges.

Now want to achive same through R.

Thanks for last response.

Actually my requirement is to loop each row in dataframe and use row values as label, coordinates etc.

JohnCoene commented 4 years ago

Hi Saharan,

You do so with the snippet of code I shared.

# generate data using convenience functions
nodes <- sg_make_nodes()
edges <- sg_make_edges(nodes)

sigmajs() %>%
  sg_nodes(nodes, id, size, color, x = x, y = y) %>%
  sg_edges(edges, id, source, target)

There is no need to explicitly loop over the data.frame. Is there a reason you want to add those nodes and edges one by one?

SaharanAbhishek commented 4 years ago

Thanks a lot, was not aware how to use data frame, just started with R.

One more thing, I am able to draw graph now, but unable to change edge color, its taking default node color.

Have tried defaultEdgeColor in sg_settings but didn't worked.

Also I need to change each edge size/thickness/color based upon weight. Just like nodes

SaharanAbhishek commented 4 years ago

Just like each node have size, color options: eg: sg_nodes(nodes, id, size, color)

can we have same for each edge: eg: sg_edges(edges, id, source, target)

JohnCoene commented 4 years ago

You will find this in this part of the official documentation

You need

    nodes <- sg_make_nodes() 
    edges <- sg_make_edges(nodes) 

    sigmajs() %>% 
      sg_nodes(nodes, id, size, color) %>% 
      sg_edges(edges, id, source, target) %>% 
      sg_settings(edgeColor = "default", defaultEdgeColor = "red") 

You can change edgeColor to source or target. For the edge size, pass size like you do for nodes

SaharanAbhishek commented 4 years ago

Thanks for the help :)

SaharanAbhishek commented 4 years ago

Sorry again distrubing you...

Now getting issue in rendering images on web page... below code is working fine when running through "Run App" in RStudio.... but images does not rendered when using Rscript through terminal "Rscript app.R".

tabPanel( img( src = "logo.png",
align = "center", height="50%", width="50%" ) ),

JohnCoene commented 4 years ago

Where is logo.png located?

SaharanAbhishek commented 4 years ago

Where is logo.png located?

In www folder and .R file is just outside that folder

in console logs, in Response Headers, content type is showing Content-Type: text/html; charset=UTF-8 insted of image/png

JohnCoene commented 4 years ago

I think RStudio manages this for you, used outside of RStudio you must specify the asset location explicitly at the top of your .R file (before ui and server).

library(shiny)
addResourcePath("www", "www")

Then change logo.png to www/logo.png, see ?addResourcePath for more details.

SaharanAbhishek commented 4 years ago

it worked... thanks again for quick response :)