Code Monkey home page Code Monkey logo

d3r's Introduction

CRAN_Status_Badge R-CMD-check

Installing d3r

d3r is on CRAN, so install with install.packages("d3r") or for the absolute latest use devtools.

devtools::install_github("timelyportfolio/d3r")

d3 Dependency Functions

d3r makes d3.js dependency injection in R easy with d3_dep_v3(), d3_dep_v4(), d3_dep_v5(), d3_dep_v6(), and the newest d3_dep_v7(). These functions work well with htmltools::tags.

library(htmltools)
library(d3r)

# check web developer tools to see d3 is available
browsable(
  attachDependencies(
    tagList(),
    d3_dep_v7()
  )
)

# or include directly in a taglist; I prefer this method.
browsable(
  tagList(
    h1("I have d3 version ", span(id="version")),
    tags$script("d3.select('#version').text(d3.version)"),
    d3_dep_v7()
  )
)

Also, I will commit to keeping d3r up-to-date with d3.js, so you’ll no longer need multiple copies of d3.js for your htmlwidgets. If you are a htmlwidget author, you will no longer need to worry every time d3.js gets a new release. See treebar lines for an example of using d3r with your htmlwidget.

d3 Hierarchy from data.frame

Building d3.js hierarchies can be very difficult. d3r::d3_nest() will convert table and data.frame to a nested d3.js hierarchy ready for work with d3-hierarchy.

d3_nest(as.data.frame(Titanic))

As another example, let’s go from treemap to d3.js.

library(treemap)
library(d3r)

d3_nest(
  treemap::random.hierarchical.data(),
  value_cols = "x"
)

d3 Hierarchy from partykit / rpart

rpart and similar objects in R are very difficult to convert but make perfect subjects for d3 hierarchical layouts. d3_party helps convert these objects for easy usage with d3.js.

#devtools::install_github("timelyportfolio/d3treeR")

library(d3treeR)
library(d3r)

# example from ?rpart
data("kyphosis", package="rpart")
rp <-  rpart::rpart(
  Kyphosis ~ Age + Number + Start,
  data = kyphosis
)

# get the json hierarchy
d3_party(tree=rp)

# interactive plot with d3treeR
d3tree2(
  d3_party(tree=rp),
  celltext="description",
  valueField="n"
)

d3 Network from igraph

igraph to d3.js network of nodes and links is a very common conversion. d3r::d3_igraph will do this for you.

library(igraph)
library(d3r)

d3_igraph(igraph::watts.strogatz.game(1, 50, 4, 0.05))

Todo

I have a whole lot of ideas. Please let me know yours, and let’s make this package great.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

d3r's People

Contributors

beemyfriend avatar jennybc avatar npmcdn-to-unpkg-bot avatar timelyportfolio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

d3r's Issues

add igraph to d3 converter

While talking with @ttriche on Twitter, I remembered a helpful function would be to convert igraph to a "standard" d3.js network. I have some preliminary code that will require testing and iteration.

handle NA

I made a bad assumption, so NA are not treated properly. As a simple case,

d3r::d3_nest(
  data.frame(
    level1 = rep("a",3),
    level2 = c(NA,"a.1","a.2"),
    value = 1:3
  ),
  value_cols = "value"
)

image

In my mind proper behavior would be NA should not be nested as children. In the above example, value: 1 should not be in children, so should be promoted a level.

Problems with Tidyr

Hi, I have a Issue with Tidyr nest_() was deprecated in tidyr 1.0.0 and is now defunct and the package use nest in d3r_party function. Could you change this please, thanks.

convert to `tidyfast`

Generally it seems most trees are well within the limits of d3r::d3_nest but faster with less dependencies in this case might be better. Look into converting to tidyfast.

provide option for json in d3_nest

Currently, d3_nest returns a data.frame/tibble. Often, we want the JSON representation of the hierarchy, but unfortunately the default behavior of jsonlite::toJSON does not give us what d3.js expects. Should we provide an argument to return the proper JSON representation or should it be a separate function?

d3_widget function

I had an idea for an extension to this package that I wanted to run by you. The general notion would be to create a generic d3 htmlwidget that could be used for including an arbitrary d3 script as a widget. I was further thinking that we could build some authoring support for this in RStudio so that "Sourcing" the widget script would result in it rendering in the Viewer pane.

Here is generally what I am thinking:

A d3_widget() function that would work something like this:

d3_widget(d3_nest(as.data.frame(Titanic)), "titanic.js")

Under the hood this would convert the "titanic.js" script into a proper htmlwidget. This means that it would package up the data specified in the first argument within the x variable and then call the code in "titanitc.js" within the render method. Implementing resize generically might be a bit tricky but it seems like if educated folks to write their scripts using some conventions and/or helper functions this could be done.

So what would "titanic.js" look like? I think it can have arbitrary JS code and have references to two variables that would be bound externally (i.e. by the wrapper we create):

x (for full access to the htmlwidget options) and data (a convenience accessor for x.data which would point to e.g. the data created by d3_nest().

To provide support for rapid iterative development, we could allow the user to put a special comment at the top of the script that indicates R code to execute to prime the data when it's previewed in RStudio, e.g.

// !preview d3_nest(as.data.frame(Titanic))

The counterpoint to all of this would be that users should just write a full htmlwidget when they want to use d3, but it seems like there is an awful lot of scaffolding required and we don't in that case have the quick preview/iteration we'd have for this.

Does this seem like a potentially good idea or are there some subtleties I'm missing? If this is something we could work on relatively soon we could add the requisite features to RStudio IDE in time for the next release.

cc @rich-iannone @jcheng5

d3_nest() fails with dev tidyr

library(d3r)

titanic_df <- Titanic %>% 
  data.frame() %>% 
  select(Class,Age,Survived,Sex,Freq)

d3_nest(titanic_df, value_cols="Freq", root="titanic")
#> Error: No tidyselect variables were registered

Would you mind taking a look? It's probably because I've accidentally broken some aspect of nest(), but I can't quite figure out what the code is supposed to be doing

use name instead of id (BREAKING!)

As I integrate d3_nest with more functions, I realize that many of the examples use id to imply uniqueness and name without implicit uniqueness. Rework id to name to avoid this potential confusion. Later, I will open a different issue to allow custom key for name and children.

d3r nesting order for SearchTree widget

#Doesnt give the value column in the leaves when branch fully grown
Titanic%>%as.data.frame

#Does give the value column in the leaves when branch fully grown
Titanic%>%data.frame%>%mutate(value=NA)%>%distinct

#Current Call
  #d3_nest doesnt nest in the correct order for ggtree.js making the links float to the last open parent
ggtree(
  list(
    root = d3r::d3_nest(
      Titanic%>%data.frame%>%mutate(value=NA)%>%distinct,
      value_cols = "Freq"
    ),
    layout = "collapse"
  ),
  name = "id",
  value = "colname"
)

#Call with old df2tree function
  #df2tree nests in the correct order for ggtree.js giving a unique parent for each child
ggtree(
  list(
    root=df2tree(Titanic%>%data.frame%>%mutate(value=NA)%>%distinct),
    layout = "collapse"
  ),
  name = "name",
  value = "value"
)
#Compare different nesting orders visually
jsonedit(d3r::d3_nest(
  Titanic%>%data.frame%>%mutate(value=NA)%>%distinct,
  value_cols = "Freq",
  json = F
))

d3nest

jsonedit(df2tree(Titanic%>%data.frame%>%mutate(value=NA)%>%distinct))

df2treenest

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.