Giter VIP home page Giter VIP logo

d3scatter's Introduction

d3scatter

Simple d3.js-based scatter plot htmlwidget based on Mike Bostock's example, with support added for updating data and brushing. Don't take this library too seriously, it's just intended as a testing ground for cross-widget communications.

Installation

devtools::install_github("rstudio/crosstalk")
devtools::install_github("jcheng5/d3scatter")

Examples

Linked brushing

library(htmltools)
library(d3scatter)

sd <- SharedData$new(iris)

browsable(tagList(
  d3scatter(sd, ~Petal.Width, ~Petal.Length, ~Species),
  d3scatter(sd, ~Sepal.Width, ~Sepal.Length, ~Species)
))

Updating data, using Shiny

library(shiny)
library(d3scatter)

ui <- fluidPage(
  d3scatterOutput("scatter1", height = 400),
  d3scatterOutput("scatter2", height = 400)
)

server <- function(input, output, session) {
  jitter_by <- 0.1
  jittered <- reactive({
    on.exit(invalidateLater(1000))
    iris$Sepal.Length <- jitter(iris$Sepal.Length, amount = jitter_by)
    iris$Sepal.Width <- jitter(iris$Sepal.Width, amount = jitter_by)
    iris$Petal.Length <- jitter(iris$Petal.Length, amount = jitter_by)
    iris$Petal.Width <- jitter(iris$Petal.Width, amount = jitter_by)
    SharedData$new(iris)
  })
  output$scatter1 <- renderD3scatter({
    d3scatter(jittered(),
      ~Sepal.Length, ~Sepal.Width,
      ~toupper(Species),
      x_lim = ~grDevices::extendrange(iris$Sepal.Length, f = jitter_by),
      y_lim = ~grDevices::extendrange(iris$Sepal.Width, f = jitter_by)
    )
  })
  output$scatter2 <- renderD3scatter({
    d3scatter(jittered(),
      ~Petal.Length, ~Petal.Width,
      ~toupper(Species),
      x_lim = ~grDevices::extendrange(iris$Petal.Length, f = jitter_by),
      y_lim = ~grDevices::extendrange(iris$Petal.Width, f = jitter_by)
    )
  })
}

shinyApp(ui, server)

Linked brushing between d3scatter and ggplot2, using Shiny

library(shiny)
library(d3scatter)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
  d3scatterOutput("scatter1", height = 300),
  plotOutput("plot1", height = 300, brush = brushOpts("brush", direction = "x"))
)

server <- function(input, output, session) {
  sd <- crosstalk::SharedData$new(iris %>% add_rownames(), "rowname")
  
  output$scatter1 <- renderD3scatter({
    d3scatter(sd,
      ~Sepal.Length, ~Sepal.Width,
      ~toupper(Species)
    )
  })

  output$plot1 <- renderPlot({
    df <- sd$data(TRUE)
    df$selected_ <- factor(df$selected_, levels = c(TRUE, FALSE))
    if (any(is.na(df$selected_))) {
      ggplot(df, aes(x = Species)) + geom_bar()
    } else {
      ggplot(df, aes(x = Species, alpha = selected_)) + geom_bar() +
        scale_alpha_manual(values = c(1.0, 0.2)) +
        guides(alpha = FALSE)
    }
  })
  observeEvent(input$brush, {
    df <- brushedPoints(sd$data(FALSE), input$brush, allRows = TRUE)
    selected <- row.names(df)[df$selected_]
    str(selected)
    sd$selection(selected)
  }, ignoreNULL = FALSE)
}

shinyApp(ui, server)

d3scatter's People

Contributors

jcheng5 avatar

Stargazers

 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

d3scatter's Issues

d3scatter wont install

I am trying to install d3scatter,and Im running into this error.

> devtools::install_github("jcheng5/d3scatter")
Downloading GitHub repo jcheng5/d3scatter@master
from URL https://api.github.com/repos/jcheng5/d3scatter/zipball/master
Installing d3scatter
Downloading GitHub repo hadley/lazyeval@master
from URL https://api.github.com/repos/hadley/lazyeval/zipball/master
Error: running command '"C:/PROGRA~1/R/R-3.4.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD config CC' had status 127

It seems like Im the only one who is experiencing this. I dont know, but I dont get whats happening. Is this package still maintained?

My sessionInfo():

R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.2 tools_3.4.2 

d3scatter won't install

When I run the following to install d3
devtools::install_github("jcheng5/d3scatter")
I get the error message:
Installation failed: Timeout was reached: Connection timed out after 10984 milliseconds

Does anyone know what I am doing wrong. Also cannot install d3scatter directly as a package library.

Crosstalk ?

Seems really interesting, but doesn't work at the moment. Is crosstalk still on private repo ? Or is there another repository for crosstalk setup ?

Bad interaction of selection and filter

If I have a d3scatter displaying a cloud of points and filter some of them, the selection acts as though they are still there, i.e. if I select the region containing hidden points it acts as though I've selected something even though nothing is visible in the selection box.

For example, run this code:

library(htmltools)
library(d3scatter)
library(crosstalk)
xy <- SharedData$new(data.frame(x=rnorm(100), y=rnorm(100)))
w1 <- d3scatter(xy, ~x, ~y, width = 300)
w2 <- filter_slider("x", "x", xy, ~x)
browsable(tagList(w1,w2))

then use the slider to hide all the points on the right, then use a selection box there and
the remaining points are dimmed as if something was selected.

Edited to add: in this example, if I later move the slider back to show everything, you can see that indeed the hidden points were selected. This is consistent behaviour, but I think it is undesirable from a UI point of view. On the other hand, I'm not sure what should happen when points are revealed: since they are appearing in the brush, they should be selected, but do I really want to send selection messages when I am manipulating the filter?

Ability to define scatter colors

d3scatter with filters (using bscols) is extremely close to showing what I need without shiny.
The only thing I am having an issue with is the ability, if it exists, to define the color to use for a certain value of the key.

This statement gives me the proper graph that has all the ability I need to filter but the static color of blue, orange and green don't visually work for the 3 status fields that I want them to represent.

d3scatter(shared_demo, ~Age.at.Report, ~Pensionable_Service, x_label = "Age", y_label = "YOS", color=~Status)

If I change the "color=~Status" to "color=c("#474d56","#64c3e8","#fcb334")" I lose the ability to see the data properly.

Any suggestions on how I might be able to use the d3scatter with crosstalk and get the colors the way I want them?
Thanks

This is a snippet of how I am using this:
bscols(widths = c(3,NA),
list(
filter_checkbox("OplGroup", "Discipline", shared_demo, ~OplGroup, inline = TRUE),
filter_checkbox("FIR", "FIR", shared_demo, ~FIR, inline = TRUE),
filter_checkbox("Specialty.Type", "Unit Type", shared_demo, ~Specialty.Type, inline = TRUE),
filter_checkbox("Status", "Status", shared_demo, ~Status, inline = TRUE),
filter_checkbox("Age.Group", "Age Band", shared_demo, ~Age.Group, inline = TRUE),
filter_checkbox("YOS.Group", "YOS Band", shared_demo, ~YOS.Group, inline = TRUE),
filter_select("Unit", "Unit", shared_demo, ~Unit, multiple = TRUE)
),
d3scatter(shared_demo, ~Age.at.Report, ~Pensionable_Service, x_label = "Age", y_label = "YOS", color=~Status)
#d3scatter(shared_demo, ~Age.at.Report, ~Pensionable_Service, x_label = "Age", y_label = "YOS", color = c("#474d56","#64c3e8","#fcb334"))
)

d3scatter errors if data is a matrix

If my data is in a matrix, d3scatter can't plot it:

library(d3scatter)
xy <- matrix(rnorm(20), ncol = 2, dimnames = list(NULL, c("x", "y")))
d3scatter(xy, ~x, ~y)

# Error in eval(value[[2]], data, environment(value)) : 
# numeric 'envir' arg not of length one

I get the same error if I put xy in a shared data object, i.e.

library(crosstalk)
sxy <- SharedData$new(xy)
d3scatter(sxy, ~x, ~y)

A fix is data <- as.data.frame(data) early in d3scatter(), but this should probably be done conditionally, and I'm not sure what the condition should be.

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.