Comments (10)
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.
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.
There's probably a JavaScript error happening somewhere along the line. Try using the browser debugging tools to run that down.
from r2d3.
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.
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.
@shotchki did you solve this? I am having the same problem with a graph network.
from r2d3.
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.
I had the same problem, but I think I figured something out.
Here is an .Rmd that works for me. I hope it helps.
from r2d3.
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.
@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.
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)
- forcegraph.js using container = "canvas" not working
- Data passed to multiple charts HOT 1
- Render r2d3 output in rmarkdown document with `rmarkdown::knitr()` function
- Request: Add support for version 7 of D3
- No guide on importing SVG
- Pass info from D3 back to [R] HOT 1
- Error adding multiple dependencies HOT 1
- Debugging: Rstudio D3 Preview with Full R Code HOT 1
- Why does the image produced from r2d3 is static?
- Implementing D3 6.0 HOT 4
- Question: r2d3 still maintained? HOT 2
- Inproper data join after input change HOT 1
- Width and Height parameters don't seem to work
- Quick questions about width and height HOT 1
- Move `master` branch to `main` HOT 1
- D3 preview in RStudio does not render imported SVG
- Cleanup the README formatting
- "publishing.html" documentation article has mis-rendered R code blocks
- Brush selection lost after page resize
- issues when rendering an r2d3-based package's plots in shiny
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from r2d3.