Git Product home page Git Product logo

Comments (10)

jjallaire avatar jjallaire commented on July 24, 2024 1

The data argument can be anything you want it to be. See the documentation here on custom data structures: https://rstudio.github.io/r2d3/articles/data_conversion.html

from r2d3.

shotchki avatar shotchki commented on July 24, 2024

Thanks for the link. However, r2d3 doesn't appear to be interpreting the data as I'd expect. I shall try to provide a minimum reproducible example:

I've included text files with the contents of my dataframe (data.txt), the js.script file (network4_js.txt) and a short R script with my data_to_json function and r2d3 command (r2d3_github.txt). When I run these in RStudio, the viewer returns a blank screen.

What I would expect is to get a layout of rectangles with blue nodes, as I get when I open an html file containing the "same" d3 (network4_html.txt). Please note, var data is copy and pasted from the R studio console by printing data_to_json(data).

Any ideas why the two don't generate the same output?

data.txt
network4_js.txt
r2d3_github.txt
network4_html.txt

from r2d3.

jjallaire avatar jjallaire commented on July 24, 2024

There's probably a JavaScript error happening somewhere along the line. Try using the browser debugging tools to run that down.

from r2d3.

shotchki avatar shotchki commented on July 24, 2024

I don't really know how to do that, but I've attached an image of the Web Inspector in case it gives you any clues.

rstudio_viewer_web_inspector

Interestingly, if I set viewer = browser, I can get the output in Chrome to match that of the html, but I've noticed its not right; the data isn't filtered out into nodes and links in the .js script - everything is plotted as a node. I think this is a symptom of me trying to mung two tidy dataframes together to get it through the data argument, and then split that into two arrays in the .js script. Ideally, I'd put both dataframes in the data argument, and they'd come out as separate arrays that I can reference in a .js script; guidance on how to do this would be very much appreciated.

from r2d3.

emilmahler avatar emilmahler commented on July 24, 2024

@shotchki did you solve this? I am having the same problem with a graph network.

from r2d3.

shotchki avatar shotchki commented on July 24, 2024

I'm afraid not. I put that project on the back burner in the hopes I'd get some more guidance/inspiration. Currently thinking about re-writing it in igraph & visNetwork.

from r2d3.

agricolamz avatar agricolamz commented on July 24, 2024

I had the same problem, but I think I figured something out.

Here is an .Rmd that works for me. I hope it helps.

image

from r2d3.

shotchki avatar shotchki commented on July 24, 2024

Thank you for your comment. Unfortunately, it 's not using two variables from the same dataframe, but using multiple dataframes that's the issue.

from r2d3.

jlfsjunior avatar jlfsjunior commented on July 24, 2024

@shotchki In network4_js.txt:

svg.selectAll(".line")
   .data(links)
   .enter()
   .append("line")
   .attr("x1", function(d) { return d.source.x })
   .attr("y1", function(d) { return d.source.y })
   .attr("x2", function(d) { return d.target.x })
   .attr("y2", function(d) { return d.target.y })
   .style("stroke", "rgb(6,120,155)");

d.source and d.target are values, not nested objects. Perhaps the best alternative in your case is to create the nested object that you need in R using lists and pass it as data. Something like:

data <- list(
  nodes = list(
    list(name = "K", value = 10, color = "#fff", ...),
    list(...),
    ...
  ),
  links = list(
    list(source = list(x = 1, y = 2), target(x = 3, y = 4), value = 10), 
    list(source = list(x = 21, y = 22), target(x = 23, y = 24), value = 20), 
    ...
  )
)

Then you can just refer to them in js as data.nodes and data.links (no need to filter).

from r2d3.

nvelden avatar nvelden commented on July 24, 2024

@shotchki

Very late reply but I tried your solution and I noticed that it only works if you use your own data_to_json function:

    data_to_json <- function(data) {
        jsonlite::toJSON(data, dataframe = "rows", auto_unbox = FALSE, rownames = TRUE)
    } 

 data <- list("df1" = df1, "df2" = df2)

 r2d3(
            data = data_to_json(data),
            script = "graph.js"
        )

Then you can reference using data.df1 and data.df2

from r2d3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.