rstudio / r2d3

R Interface to D3 Visualizations
https://rstudio.github.io/r2d3
Other
516 stars 105 forks source link

using cola #43

Open ghost opened 6 years ago

ghost commented 6 years ago

Hi

I would like to use the cola(R): https://github.com/timelyportfolio/colaR package to layout my force network like: http://ialab.it.monash.edu/webcola/examples/smallworldwithgroups.html

and then present the network using shiny. My app.R looks like this:

libs <- c("r2d3","jsonlite") loaded <- sapply(libs, function(x){ library(x, character.only=TRUE) })

ui <- shinyUI(fluidPage( useShinyjs(), titlePanel( "Network Browser" ), mainPanel( d3Output("d3") )

))

server <- shinyServer(function(input, output,session) {

output$d3 <- renderD3({ r2d3(data = jsonlite::read_json("miserables.json"), d3_version = 4, script = "forcegraph.js") })

})

shinyApp(ui = ui, server = server)

I have installed colaR and tried to add this part to the forcegraph.js:

var d3cola = cola.d3adaptor() .linkDistance(120) .avoidOverlaps(true) .size([width, height]);

But it complains that

Error: cola is not defined in forcegraph.js#11:14 ReferenceError: cola is not defined

Any advice would be great, Thanks!

timelyportfolio commented 6 years ago

If I understand correctly, I think you will just need to add the cola.js dependency to the ui/fluidPage. Will you let me know if something like the below works?

ui <- tagList(
  shinyUI(fluidPage(
    useShinyjs(),
    titlePanel(
      "Network Browser"
    ),
    mainPanel(
      d3Output("d3")
    ),
    htmlDependency(
      name = "cola",
      version = "3.1.3",
      src = c(file = system.file("htmlwidgets/lib/WebCola", package="colaR"),
      script = "cola.min.js"
    )
))

Also, colaR has not been updated to d3v4 even though cola (js) has been, so you will need d3_version=3 in r2d3. I am happy to update if you have interest.

ghost commented 6 years ago

Hi

Slight typo? in your suggestion:

ghost commented 6 years ago

Hi

Slight change to your suggestion but the same error persists: Error: cola is not defined in forcegraph.js#11:14 ReferenceError: cola is not defined

library("r2d3") library("jsonlite") library("colaR") library("htmltools") library("shiny") library(shinyjs)

ui <- tagList( shinyUI(fluidPage( useShinyjs(), titlePanel( "Network Browser" ), mainPanel( d3Output("d3") ), htmlDependency( name = "cola", version = "3.1.3", src = c(file = system.file("/home/shiny/webapps/d3_test/colaR/inst/htmlwidgets/lib/WebCola")), package="colaR", script = "cola.min.js" ) )))

Define server logic required to process and plot data

server <- shinyServer(function(input, output,session) {

output$d3 <- renderD3({ r2d3(data = jsonlite::read_json("miserables.json"), d3_version = 3, script = "forcegraph.js") })

})

Run the application

shinyApp(ui = ui, server = server)

The forcegraph.js i am using is as attached which is based on https://bl.ocks.org/mbostock/4063570

forcegraph.txt

Thank you!