1

I am currently having a hard time using r2d3 with shiny. The problem lays in the sourcing of the script file. Which works if the shiny app is run locally, but not when using shiny-server. My understanding so far is that js files should be stored under the www/dir and sourced without specifing www. However the behaviour is inconsistent if I use the same code on the shiny server[only d33 renders, other divs are empty or Error: barchart is not defined or www is not defined] vs running it locally [all three plots show].

Consider following minimal reproducible example:

test
├── server.R
├── ui.R
└── www
    └── barchart.js

1 directory, 3 files

barchart.js example from r2d3 package

// !preview r2d3 data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)

var barHeight = Math.floor(height / data.length);

svg.selectAll('rect')
  .data(data)
  .enter().append('rect')
    .attr('width', function(d) { return d * width; })
    .attr('height', barHeight)
    .attr('y', function(d, i) { return i * barHeight; })
    .attr('fill', 'steelblue');

server.R

function(input, output,session) {
    data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)
    output$d31 <- renderD3({
      r2d3( data = data,
            script = "www/barchart.js",d3_version = "5")
    }) 
    output$d32 <- renderD3({
      r2d3( data = data,
            script = "./barchart.js",d3_version = "5")
    }) 
    
    output$d33 <- renderD3({
      r2d3( data = data,
            script = system.file("examples/barchart.js", package = "r2d3"),d3_version = "5")
    }) 
    
}


ui.R:

library(shiny)
library(r2d3)

shinyUI(
  fluidPage(
            h1("D3 www/barchart.js"),
            d3Output("d31"),
            h1("D3 ./barchart.js"),
            d3Output("d32"),
            h1("D3 system.file"),
            d3Output("d33"),

            
))

On the server the two first plot result in empty divs, the thrid method using system file however workes:

enter image description here

i tried sourcing using tags$head(tags$script(src="barchart.js")) which eliminates the not found error but still doesnt show the barplot

How can i source the plot file without creating a package and using system.file??

user12256545
  • 2,755
  • 4
  • 14
  • 28

0 Answers0