Rcommunicating-with-js

Published by onesixx on

library("shiny")
library("tidyverse")
library("r2d3")

ui <- fluidPage(
  verbatimTextOutput("selected"),
  d3Output("d3")
)

server <- function(input, output) {
  output$d3 <- renderD3({
    r2d3(
      c(0.3, 0.6, 0.8, 0.95, 0.40),
      options=list(color='orange'),
      script = "bar.js"
    )
  })
  output$selected <- renderText({
    bar_number <- req(input$bar_clicked) 
    bar_number %>% as.numeric()
  })
}

shinyApp(ui, server)
var barHeight = Math.ceil(height / data.length);
//console.log(barHeight);

var bars = r2d3.svg.selectAll('rect')
  .data(r2d3.data)
  .enter().append('rect')
    .attr('width',  function(d){ return d * width; })
    .attr('height', barHeight)
    .attr('y',      function(d, i){ return i * barHeight; })
    .attr('fill',   options.color)
    
    .attr("d",   function(d) { return d; })
    .on("click", function(){
      Shiny.setInputValue( 
        id="bar_clicked", value=d3.select(this).attr("d"),
        {priority: "event"}
        );
      console.log(d);
    });
bars.exit().remove();  
https://shiny.rstudio.com/articles/communicating-with-js.html
Categories: ggplot2

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x